1No::Worries::Stat(3) User Contributed Perl Documentation No::Worries::Stat(3)
2
3
4
6 No::Worries::Stat - stat() handling without worries
7
9 use No::Worries::Stat qw(*);
10
11 @stat = stat($path) or die;
12 printf("type is %s\n", stat_type($stat[ST_MODE]));
13 printf("size is %d\n", $stat[ST_SIZE]);
14 printf("user can read\n") if $stat[ST_MODE] & S_IRUSR;
15
16 # make sure "/bin/ls" is owned by root and has the right permissions
17 stat_ensure("/bin/ls", user => "root", mode => 0755);
18 # make sure "/var/log" is not group or world writable
19 stat_ensure("/var/log", mode => "-022");
20 # idem but using the S_* constants
21 stat_ensure("/var/log", mode => "-" . (S_IWGRP|S_IWOTH));
22
24 This module eases file status handling by providing convenient
25 constants and functions to get, set and manipulate file status
26 information. All the functions die() on error.
27
29 This module provides the following constants to ease access to stat()
30 fields (none of them being exported by default):
31
32 "ST_DEV"
33 ID of device containing file
34
35 "ST_INO"
36 inode number
37
38 "ST_MODE"
39 protection
40
41 "ST_NLINK"
42 number of hard links
43
44 "ST_UID"
45 user ID of owner
46
47 "ST_GID"
48 group ID of owner
49
50 "ST_RDEV"
51 device ID (if special file)
52
53 "ST_SIZE"
54 total size, in bytes
55
56 "ST_ATIME"
57 time of last access
58
59 "ST_MTIME"
60 time of last modification
61
62 "ST_CTIME"
63 time of last status change
64
65 "ST_BLKSIZE"
66 blocksize for filesystem I/O
67
68 "ST_BLOCKS"
69 number of 512B blocks allocated
70
71 In addition, it also optionally exports all the ":mode" constants from
72 Fcntl.
73
74 This way, all the stat() related constants can be imported in a uniform
75 way.
76
78 This module provides the following functions (none of them being
79 exported by default):
80
81 stat_type(MODE)
82 given the file mode ("ST_MODE" field), return the file type as a
83 string; possible return values are: "block device", "character
84 device", "directory", "door", "event port", "network file", "pipe",
85 "plain file", "socket", "symlink", "unknown" and "whiteout".
86
87 stat_ensure(PATH[, OPTIONS])
88 make sure the given path has the expected file "status" (w.r.t.
89 stat()) and call chown(), chmod() or utime() if needed, returning
90 the number of changes performed; supported options:
91
92 • "user": expected user name or uid
93
94 • "group": expected group name or gid
95
96 • "mode": expected mode specification (see below)
97
98 • "mtime": expected modification time
99
100 • "follow": follow symbolic links (default is to skip them)
101
102 • "callback": code to be executed before changing something (see
103 below)
104
105 The "mode" option of stat_ensure() can be given:
106
107 NUMBER
108 an absolute value like 0755, meaning that mode must be equal to it
109
110 +NUMBER
111 a list of bits that must be set, e.g. "+0111" for "executable for
112 all"
113
114 -NUMBER
115 a list of bits that must be clear, e.g. "-022" for not writable by
116 group or other
117
118 Note: the number after "+" or "-" will be interpreted as being octal
119 only if it starts with "0". You should therefore use "+0111" or
120 "+".oct(111) to enable the executable bits but not "+111" which is the
121 same as "+0157".
122
123 The "callback" option of stat_ensure() will receive the given path and
124 a string describing what is about to be changed. It must return true to
125 tell stat_ensure() to indeed perform the changes.
126
127 Here is for insatnce how a "noaction" option could be implemented:
128
129 sub noaction ($$) {
130 my($path, $change) = @_;
131
132 printf("did not change %s of %s\n", $change, $path);
133 return(0);
134 }
135 foreach my $path (@paths) {
136 stat_ensure($path, user => "root", mode => 0755, callback => \&noaction);
137 }
138
140 Fcntl, No::Worries.
141
143 Lionel Cons <http://cern.ch/lionel.cons>
144
145 Copyright (C) CERN 2012-2019
146
147
148
149perl v5.36.0 2022-07-22 No::Worries::Stat(3)