Skip to content

Commit

Permalink
Teaching GNUFile methods to follow symlinks
Browse files Browse the repository at this point in the history
On Linux systems, if File.mode is used on a file that's a symlink,
it will return 0x777 because that's the "mode" of the symlink but mode
is pretty meaningless for a symlink.  By doing `stat -L ...` the command
will automatically resolve symlinks.
  • Loading branch information
John Pfuntner authored and philpep committed Feb 15, 2024
1 parent 525bde4 commit d2bcd9f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions testinfra/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,38 +223,38 @@ def get_module_class(cls, host):
class GNUFile(File):
@property
def user(self):
return self.check_output("stat -c %%U %s", self.path)
return self.check_output("stat -Lc %%U %s", self.path)

@property
def uid(self):
return int(self.check_output("stat -c %%u %s", self.path))
return int(self.check_output("stat -Lc %%u %s", self.path))

@property
def group(self):
return self.check_output("stat -c %%G %s", self.path)
return self.check_output("stat -Lc %%G %s", self.path)

@property
def gid(self):
return int(self.check_output("stat -c %%g %s", self.path))
return int(self.check_output("stat -Lc %%g %s", self.path))

@property
def mode(self):
# Supply a base of 8 when parsing an octal integer
# e.g. int('644', 8) -> 420
return int(self.check_output("stat -c %%a %s", self.path), 8)
return int(self.check_output("stat -Lc %%a %s", self.path), 8)

@property
def mtime(self):
ts = self.check_output("stat -c %%Y %s", self.path)
ts = self.check_output("stat -Lc %%Y %s", self.path)
return datetime.datetime.fromtimestamp(float(ts))

@property
def size(self):
return int(self.check_output("stat -c %%s %s", self.path))
return int(self.check_output("stat -Lc %%s %s", self.path))

@property
def inode(self):
return int(self.check_output("stat -c %%i %s", self.path))
return int(self.check_output("stat -Lc %%i %s", self.path))

@property
def md5sum(self):
Expand Down

0 comments on commit d2bcd9f

Please sign in to comment.