Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages
No::Worries::Stat(3) User Contributed Perl Documentation No::Worries::Stat(3)

No::Worries::Stat - stat() handling without worries

  use No::Worries::Stat qw(*);
  @stat = stat($path) or die;
  printf("type is %s\n", stat_type($stat[ST_MODE]));
  printf("size is %d\n", $stat[ST_SIZE]);
  printf("user can read\n") if $stat[ST_MODE] & S_IRUSR;
  # make sure "/bin/ls" is owned by root and has the right permissions
  stat_ensure("/bin/ls", user => "root", mode => 0755);
  # make sure "/var/log" is not group or world writable
  stat_ensure("/var/log", mode => "-022");
  # idem but using the S_* constants
  stat_ensure("/var/log", mode => "-" . (S_IWGRP|S_IWOTH));

This module eases file status handling by providing convenient constants and functions to get, set and manipulate file status information. All the functions die() on error.

This module provides the following constants to ease access to stat() fields (none of them being exported by default):
ID of device containing file
inode number
number of hard links
user ID of owner
group ID of owner
device ID (if special file)
total size, in bytes
time of last access
time of last modification
time of last status change
blocksize for filesystem I/O
number of 512B blocks allocated
In addition, it also optionally exports all the ":mode" constants from Fcntl.
This way, all the stat() related constants can be imported in a uniform way.

This module provides the following functions (none of them being exported by default):
given the file mode ("ST_MODE" field), return the file type as a string; possible return values are: "block device", "character device", "directory", "door", "event port", "network file", "pipe", "plain file", "socket", "symlink", "unknown" and "whiteout".
stat_ensure(PATH[, OPTIONS])
make sure the given path has the expected file "status" (w.r.t. stat()) and call chown(), chmod() or utime() if needed, returning the number of changes performed; supported options:
"user": expected user name or uid
"group": expected group name or gid
"mode": expected mode specification (see below)
"mtime": expected modification time
"follow": follow symbolic links (default is to skip them)
"callback": code to be executed before changing something (see below)
The "mode" option of stat_ensure() can be given:
an absolute value like 0755, meaning that mode must be equal to it
a list of bits that must be set, e.g. "+0111" for "executable for all"
a list of bits that must be clear, e.g. "-022" for not writable by group or other
Note: the number after "+" or "-" will be interpreted as being octal only if it starts with "0". You should therefore use "+0111" or "+".oct(111) to enable the executable bits but not "+111" which is the same as "+0157".
The "callback" option of stat_ensure() will receive the given path and a string describing what is about to be changed. It must return true to tell stat_ensure() to indeed perform the changes.
Here is for insatnce how a "noaction" option could be implemented:
  sub noaction ($$) {
      my($path, $change) = @_;
      printf("did not change %s of %s\n", $change, $path);
  foreach my $path (@paths) {
      stat_ensure($path, user => "root", mode => 0755, callback => \&noaction);

Fcntl, No::Worries.

Lionel Cons <>
Copyright (C) CERN 2012-2017
2017-08-01 perl v5.28.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.