| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <errno.h> |
| 22 |
#include <dirent.h> |
| 23 |
#include <fnmatch.h> |
| 24 |
#include <limits.h> |
| 25 |
#include <stdint.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <time.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
|
| 32 |
#include "files.h" |
| 33 |
#include "logging.h" |
| 34 |
#include "messages.h" |
| 35 |
#include "string-macros.h" |
| 36 |
#include "system.h" |
| 37 |
#include "xalloc.h" |
| 38 |
#include "xasprintf.h" |
| 39 |
|
| 40 |
static int deep = 0; |
| 41 |
|
| 42 |
static void |
| 43 |
files_data_init (struct files_types **filecount) |
| 44 |
{ |
| 45 |
if (NULL == *filecount) |
| 46 |
{ |
| 47 |
struct files_types *t; |
| 48 |
t = xmalloc (sizeof (struct files_types)); |
| 49 |
*filecount = t; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static int |
| 54 |
files_filematch (const char *pattern, const char *name) |
| 55 |
{ |
| 56 |
if (pattern != NULL) |
| 57 |
{ |
| 58 |
int status = fnmatch (pattern, name, 0); |
| 59 |
if (status != 0) |
| 60 |
return FNM_NOMATCH; |
| 61 |
} |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static bool |
| 67 |
files_is_hidden (const char *filename) |
| 68 |
{ |
| 69 |
return (filename[0] == '.'); |
| 70 |
} |
| 71 |
|
| 72 |
static bool |
| 73 |
files_check_age (int64_t age, time_t now, time_t filemtime) |
| 74 |
{ |
| 75 |
time_t mtime = now; |
| 76 |
|
| 77 |
if (age == 0) |
| 78 |
return true; |
| 79 |
|
| 80 |
if (age < 0) |
| 81 |
mtime += age; |
| 82 |
else |
| 83 |
mtime -= age; |
| 84 |
|
| 85 |
return (((age < 0) && (filemtime > mtime)) || |
| 86 |
((age > 0) && (filemtime < mtime))); |
| 87 |
} |
| 88 |
|
| 89 |
static bool |
| 90 |
files_check_size (int64_t size, off_t filesize) |
| 91 |
{ |
| 92 |
off_t abs_size; |
| 93 |
|
| 94 |
if (size == 0) |
| 95 |
return true; |
| 96 |
|
| 97 |
if (size < 0) |
| 98 |
abs_size = (off_t) ((-1) * size); |
| 99 |
else |
| 100 |
abs_size = (off_t) size; |
| 101 |
|
| 102 |
return (((size < 0) && (filesize < abs_size)) || |
| 103 |
((size > 0) && (filesize > abs_size))); |
| 104 |
} |
| 105 |
|
| 106 |
int |
| 107 |
files_filecount (const char *dir, unsigned int flags, |
| 108 |
int64_t age, int64_t size, const char *pattern, |
| 109 |
struct files_types **filecount) |
| 110 |
{ |
| 111 |
int status; |
| 112 |
DIR *dirp; |
| 113 |
time_t now; |
| 114 |
|
| 115 |
errno = 0; |
| 116 |
if ((dirp = opendir (dir)) == NULL) |
| 117 |
{ |
| 118 |
dbg ("(e) cannot open %s (%s)\n", dir, strerror (errno)); |
| 119 |
return -1; |
| 120 |
} |
| 121 |
|
| 122 |
files_data_init (filecount); |
| 123 |
now = time (NULL); |
| 124 |
|
| 125 |
|
| 126 |
for (;;) |
| 127 |
{ |
| 128 |
char abs_path[PATH_MAX]; |
| 129 |
struct dirent *dp; |
| 130 |
struct stat statbuf; |
| 131 |
bool is_hidden, age_match, size_match; |
| 132 |
errno = 0; |
| 133 |
|
| 134 |
if ((dp = readdir (dirp)) == NULL) |
| 135 |
{ |
| 136 |
if (errno != 0) |
| 137 |
plugin_error (STATE_UNKNOWN, errno, "readdir() failure"); |
| 138 |
else |
| 139 |
break; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if (STREQ (dp->d_name, ".") || STREQ (dp->d_name, "..")) |
| 144 |
continue; |
| 145 |
|
| 146 |
is_hidden = files_is_hidden (dp->d_name); |
| 147 |
if (!(flags & FILES_INCLUDE_HIDDEN) && is_hidden) |
| 148 |
continue; |
| 149 |
|
| 150 |
snprintf (abs_path, sizeof (abs_path), "%s/%s", dir, dp->d_name); |
| 151 |
|
| 152 |
status = lstat (abs_path, &statbuf); |
| 153 |
if (status != 0) |
| 154 |
plugin_error (STATE_UNKNOWN, errno, "lstat (%s) failed", abs_path); |
| 155 |
|
| 156 |
if (S_IFDIR == (statbuf.st_mode & S_IFMT)) |
| 157 |
{ |
| 158 |
dbg ("(%d) %s (%sdirectory)\n", deep, abs_path, |
| 159 |
is_hidden ? "hidden " : ""); |
| 160 |
if (flags & FILES_RECURSIVE) |
| 161 |
{ |
| 162 |
char *subdir = xasprintf ("%s/%s", dir, dp->d_name); |
| 163 |
if (!(flags & FILES_REGULAR_ONLY)) |
| 164 |
{ |
| 165 |
if (0 == files_filematch (pattern, dp->d_name)) |
| 166 |
{ |
| 167 |
(*filecount)->directory++; |
| 168 |
(*filecount)->total++; |
| 169 |
if (is_hidden) |
| 170 |
(*filecount)->hidden++; |
| 171 |
} |
| 172 |
dbg ("(%d) --> #%lu\n", deep, |
| 173 |
(unsigned long)(*filecount)->total); |
| 174 |
} |
| 175 |
|
| 176 |
deep++; |
| 177 |
dbg ("+ recursive call of files_filecount for %s\n", subdir); |
| 178 |
files_filecount (subdir, flags, age, size, |
| 179 |
pattern, filecount); |
| 180 |
dbg ("(%d) --> #%lu\n", deep, |
| 181 |
(unsigned long)(*filecount)->total); |
| 182 |
free (subdir); |
| 183 |
continue; |
| 184 |
} |
| 185 |
if (flags & FILES_REGULAR_ONLY) |
| 186 |
continue; |
| 187 |
} |
| 188 |
|
| 189 |
if (0 != files_filematch (pattern, dp->d_name)) |
| 190 |
{ |
| 191 |
dbg ("(%d) %s does not match the pattern\n", deep, abs_path); |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
switch (statbuf.st_mode & S_IFMT) |
| 196 |
{ |
| 197 |
default: |
| 198 |
dbg ("(%d) %s (unknown file)\n", deep, abs_path); |
| 199 |
(*filecount)->unknown++; |
| 200 |
if (flags & FILES_IGNORE_UNKNOWN) |
| 201 |
continue; |
| 202 |
break; |
| 203 |
case S_IFBLK: |
| 204 |
case S_IFCHR: |
| 205 |
case S_IFIFO: |
| 206 |
case S_IFSOCK: |
| 207 |
dbg ("(%d) %s (special file)\n", deep, abs_path); |
| 208 |
(*filecount)->special_file++; |
| 209 |
if (flags & FILES_REGULAR_ONLY) |
| 210 |
continue; |
| 211 |
break; |
| 212 |
case S_IFLNK: |
| 213 |
dbg ("(%d) %s (symlink)\n", deep, abs_path); |
| 214 |
if (flags & (FILES_IGNORE_SYMLINKS | FILES_REGULAR_ONLY)) |
| 215 |
continue; |
| 216 |
(*filecount)->symlink++; |
| 217 |
break; |
| 218 |
case S_IFREG: |
| 219 |
age_match = files_check_age (age, now, statbuf.st_mtime); |
| 220 |
size_match = files_check_size (size, statbuf.st_size); |
| 221 |
dbg ("(%d) %s (%s file), touched %.2f days ago (%s)" |
| 222 |
" with size %lu bytes (%s)\n" |
| 223 |
, deep |
| 224 |
, abs_path |
| 225 |
, is_hidden ? "hidden" : "regular" |
| 226 |
, (long)(now - statbuf.st_mtime) / 60.0 / 60.0 / 24.0 |
| 227 |
, age_match ? "match" : "skip" |
| 228 |
, (unsigned long) statbuf.st_size |
| 229 |
, size_match ? "match" : "skip"); |
| 230 |
if (!age_match) |
| 231 |
continue; |
| 232 |
if (!size_match) |
| 233 |
continue; |
| 234 |
|
| 235 |
(*filecount)->regular_file++; |
| 236 |
if (is_hidden) |
| 237 |
(*filecount)->hidden++; |
| 238 |
break; |
| 239 |
} |
| 240 |
|
| 241 |
if (files_filematch (pattern, dp->d_name) != 0) |
| 242 |
continue; |
| 243 |
|
| 244 |
(*filecount)->total++; |
| 245 |
dbg ("(%d) --> #%lu\n", deep, (unsigned long)(*filecount)->total); |
| 246 |
} |
| 247 |
|
| 248 |
dbg ("- return #%lu (%s)\n", (unsigned long)(*filecount)->total, dir); |
| 249 |
deep--; |
| 250 |
closedir (dirp); |
| 251 |
return 0; |
| 252 |
} |