Create a TarInfo object from the result of os.stat or equivalent on an existing file. The file is either named by 'name', or specified as a file object 'fileobj' with a file descriptor. If given, 'arcname' specifies an alternative name for the file in the
(self, name=None, arcname=None, fileobj=None)
| 2159 | return [tarinfo.name for tarinfo in self.getmembers()] |
| 2160 | |
| 2161 | def gettarinfo(self, name=None, arcname=None, fileobj=None): |
| 2162 | """Create a TarInfo object from the result of os.stat or equivalent |
| 2163 | on an existing file. The file is either named by 'name', or |
| 2164 | specified as a file object 'fileobj' with a file descriptor. If |
| 2165 | given, 'arcname' specifies an alternative name for the file in the |
| 2166 | archive, otherwise, the name is taken from the 'name' attribute of |
| 2167 | 'fileobj', or the 'name' argument. The name should be a text |
| 2168 | string. |
| 2169 | """ |
| 2170 | self._check("awx") |
| 2171 | |
| 2172 | # When fileobj is given, replace name by |
| 2173 | # fileobj's real name. |
| 2174 | if fileobj is not None: |
| 2175 | name = fileobj.name |
| 2176 | |
| 2177 | # Building the name of the member in the archive. |
| 2178 | # Backward slashes are converted to forward slashes, |
| 2179 | # Absolute paths are turned to relative paths. |
| 2180 | if arcname is None: |
| 2181 | arcname = name |
| 2182 | drv, arcname = os.path.splitdrive(arcname) |
| 2183 | arcname = arcname.replace(os.sep, "/") |
| 2184 | arcname = arcname.lstrip("/") |
| 2185 | |
| 2186 | # Now, fill the TarInfo object with |
| 2187 | # information specific for the file. |
| 2188 | tarinfo = self.tarinfo() |
| 2189 | tarinfo._tarfile = self # To be removed in 3.16. |
| 2190 | |
| 2191 | # Use os.stat or os.lstat, depending on if symlinks shall be resolved. |
| 2192 | if fileobj is None: |
| 2193 | if not self.dereference: |
| 2194 | statres = os.lstat(name) |
| 2195 | else: |
| 2196 | statres = os.stat(name) |
| 2197 | else: |
| 2198 | statres = os.fstat(fileobj.fileno()) |
| 2199 | linkname = "" |
| 2200 | |
| 2201 | stmd = statres.st_mode |
| 2202 | if stat.S_ISREG(stmd): |
| 2203 | inode = (statres.st_ino, statres.st_dev) |
| 2204 | if not self.dereference and statres.st_nlink > 1 and \ |
| 2205 | inode in self.inodes and arcname != self.inodes[inode]: |
| 2206 | # Is it a hardlink to an already |
| 2207 | # archived file? |
| 2208 | type = LNKTYPE |
| 2209 | linkname = self.inodes[inode] |
| 2210 | else: |
| 2211 | # The inode is added only if its valid. |
| 2212 | # For win32 it is always 0. |
| 2213 | type = REGTYPE |
| 2214 | if inode[0]: |
| 2215 | self.inodes[inode] = arcname |
| 2216 | elif stat.S_ISDIR(stmd): |
| 2217 | type = DIRTYPE |
| 2218 | elif stat.S_ISFIFO(stmd): |