Method: Train::Extras::Stat.bsd_stat

Defined in:
lib/train/extras/stat.rb

.bsd_stat(shell_escaped_path, backend, follow_symlink) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/train/extras/stat.rb', line 62

def self.bsd_stat(shell_escaped_path, backend, follow_symlink)
  # From stat man page on FreeBSD:
  # z       The size of file in bytes (st_size).
  # p       File type and permissions (st_mode).
  # u, g    User ID and group ID of file's owner (st_uid, st_gid).
  # a, m, c, B
  #         The time file was last accessed or modified, or when the
  #         inode was last changed, or the birth time of the inode
  #         (st_atime, st_mtime, st_ctime, st_birthtime).
  #
  # The special output specifier S may be used to indicate that the
  # output, if applicable, should be in string format.  May be used
  # in combination with:
  #      ...
  #      gu      Display group or user name.
  lstat = follow_symlink ? ' -L' : ''
  res = backend.run_command(
    "stat#{lstat} -f '%z\n%p\n%Su\n%u\n%Sg\n%g\n%a\n%m' "\
    "#{shell_escaped_path}")

  return {} if res.exit_status != 0

  fields = res.stdout.split("\n")
  return {} if fields.length != 8

  tmask = fields[1].to_i(8)

  {
    type:  find_type(tmask),
    mode:  tmask & 07777,
    owner: fields[2],
    uid:   fields[3].to_i,
    group: fields[4],
    gid:   fields[5].to_i,
    mtime: fields[7].to_i,
    size:  fields[0].to_i,
    selinux_label: fields[8],
  }
end