CFEngine: new filestat() function
  2013-04-04

This is an implementation of the system stat() function, which makes it easy to retrieve file metadata like filesize, mode, modification time and so on.

No need to fork an additional stat process anymore :)

Example reproduced below:

body common control
{
      bundlesequence => { run };
}

bundle agent run
{
  vars:
      "f" string => "/etc/passwd";
      "fields" slist => splitstring("size,gid,uid,ino,nlink,ctime,atime,mtime,mode,modeoct,permstr,permoct,type,devno,dev_minor,dev_major", ",", 999);
      "stat[$(fields)]" string => filestat($(f), $(fields));
  reports:
    cfengine::
      "$(f) $(fields) = $(stat[$(fields)])";
}

Output:

R: /etc/passwd size = 2157
R: /etc/passwd gid = 0
R: /etc/passwd uid = 0
R: /etc/passwd ino = 8269902
R: /etc/passwd nlink = 1
R: /etc/passwd ctime = 1359415340
R: /etc/passwd atime = 1363224601
R: /etc/passwd mtime = 1359415340
R: /etc/passwd mode = 33188
R: /etc/passwd modeoct = 100644
R: /etc/passwd permstr = -rw-r--r--
R: /etc/passwd permoct = 644
R: /etc/passwd type = regular file
R: /etc/passwd devno = 2049
R: /etc/passwd dev_minor = 1
R: /etc/passwd dev_major = 8

Moreover, {c,a,m}time fields can be transformed into human-readable form thanks to the also recently merged strftime() function:

body common control
{
      bundlesequence => { run };
}

bundle agent run
{
  vars:
      "f" string => "/tmp/foo";
      "fields" slist => splitstring("ctime,atime,mtime", ",", 999);
      "stat[$(fields)]" string => strftime("localtime", "%F - %T", filestat($(f), $(fields)));
  reports:
    cfengine::
      "$(f) $(fields) $(stat[$(fields)])";
}

Output:

R: /tmp/foo ctime 2013-04-04 - 23:41:47
R: /tmp/foo atime 2013-04-04 - 23:41:36
R: /tmp/foo mtime 2013-04-04 - 23:41:47