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