| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# qdirstat-cache-writer - script to write QDirStat cache files from cron jobs |
| 4 |
# |
| 5 |
# QDirStat can read its information from cache files. This is a lot faster than |
| 6 |
# reading all the directories in a directory tree and obtaining detailed |
| 7 |
# information (size, type, last modification time) for each file and directory |
| 8 |
# with the opendir() / readdir() and lstat() system calls for each individual |
| 9 |
# file and directory. |
| 10 |
# |
| 11 |
# QDirStat can also write those cache files ("Write Cache File..." from the |
| 12 |
# "File" menu), but the whole point of cache files is being able to do that in |
| 13 |
# the background when the user does not have to wait for it - like in a cron |
| 14 |
# job running in the middle of the night. QDirStat itself cannot be used to do |
| 15 |
# that because it is a KDE program and thus an X program that needs access to |
| 16 |
# an X display - which cron does not provide. |
| 17 |
# |
| 18 |
# This is what this Perl script is for. |
| 19 |
# |
| 20 |
# Usage: |
| 21 |
# qdirstat-cache-writer [-lvdhi] <directory> [<cache-file-name>] |
| 22 |
# |
| 23 |
# If not specified, <cache-file-name> defaults to ".qdirstat.cache.gz" |
| 24 |
# in <directory>. |
| 25 |
# |
| 26 |
# If <cache-file-name> ends with ".gz", it will be compressed with gzip. |
| 27 |
# qdirstat can read gzipped and plain text cache files. |
| 28 |
# |
| 29 |
# -l long format - always add full path, even for plain files |
| 30 |
# -1 file format 1.0 without UID/GID/permissions |
| 31 |
# -2 (default) file format 2.0 with UID/GID/permissions |
| 32 |
# -m scan mounted filesystems (cross filesystem boundaries) |
| 33 |
# -i ignore directory own size. Useful if the filesystem reports |
| 34 |
# the subtree size as the size of the directory (like CephFS). |
| 35 |
# -v verbose |
| 36 |
# -d debug |
| 37 |
# -h help (usage message) |
| 38 |
# |
| 39 |
# Author: Stefan Hundhammer <[email protected]> |
| 40 |
# |
| 41 |
# This script is freeware. Fuck the lawyers and their legalese bullshit. |
| 42 |
# All they ever contribute to software is those legalese headers that are just |
| 43 |
# in the way of people working with the code. |
| 44 |
|
| 45 |
|
| 46 |
# TO DO: |
| 47 |
# |
| 48 |
# - ensure to use UTF-8 |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
use strict; |
| 53 |
use English; |
| 54 |
use Getopt::Std; |
| 55 |
use Fcntl ':mode'; |
| 56 |
use Encode; |
| 57 |
use Cwd; |
| 58 |
use URI::Escape qw(uri_escape); |
| 59 |
use vars qw( $opt_l $opt_1 $opt_2 $opt_m $opt_v $opt_d $opt_h $opt_i); |
| 60 |
|
| 61 |
|
| 62 |
# Forward declarations. |
| 63 |
|
| 64 |
sub main(); |
| 65 |
|
| 66 |
|
| 67 |
# Global variables. |
| 68 |
|
| 69 |
my $long_format = 0; |
| 70 |
my $with_uid_gid_perm = 1; |
| 71 |
my $scan_mounted = 0; |
| 72 |
my $verbose = 0; |
| 73 |
my $debug = 0; |
| 74 |
my $ignore_dir_own_size = 0; |
| 75 |
|
| 76 |
my $default_cache_file_name = ".qdirstat.cache.gz"; |
| 77 |
|
| 78 |
my $toplevel_dev_no = undef; |
| 79 |
my $toplevel_dev_name = undef; |
| 80 |
my $unsafe_chars = "\x00-\x20%"; |
| 81 |
|
| 82 |
|
| 83 |
# Call the main function and exit. |
| 84 |
# DO NOT enter any other code outside a sub - |
| 85 |
# any variables would otherwise be global. |
| 86 |
|
| 87 |
|
| 88 |
main(); |
| 89 |
exit 0; |
| 90 |
|
| 91 |
|
| 92 |
#----------------------------------------------------------------------------- |
| 93 |
|
| 94 |
|
| 95 |
sub main() |
| 96 |
{ |
| 97 |
# Extract command line options. |
| 98 |
# This will set a variable opt_? for any option, |
| 99 |
# e.g. opt_v if option '-v' is passed on the command line. |
| 100 |
|
| 101 |
getopts('l12mvdhi'); |
| 102 |
|
| 103 |
usage() if $opt_h; |
| 104 |
$long_format = 1 if $opt_l; |
| 105 |
$with_uid_gid_perm = 0 if $opt_1; |
| 106 |
$with_uid_gid_perm = 1 if $opt_2; |
| 107 |
$scan_mounted = 1 if $opt_m; |
| 108 |
$verbose = 1 if $opt_v; |
| 109 |
$debug = 1 if $opt_d; |
| 110 |
$ignore_dir_own_size = 1 if $opt_i; |
| 111 |
|
| 112 |
# One or two parameters are required |
| 113 |
# (yes, Perl does weird counting) |
| 114 |
usage() if $#ARGV < 0 || $#ARGV > 1; |
| 115 |
|
| 116 |
my $toplevel_dir = shift @ARGV; |
| 117 |
$toplevel_dir = Cwd::abs_path( $toplevel_dir ); |
| 118 |
|
| 119 |
my $cache_file_name; |
| 120 |
|
| 121 |
if ( $#ARGV < 0 ) # No more command line arguments? |
| 122 |
{ |
| 123 |
$cache_file_name = $toplevel_dir . "/" . $default_cache_file_name; |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
$cache_file_name = shift @ARGV; |
| 128 |
} |
| 129 |
|
| 130 |
write_cache_file( $toplevel_dir, $cache_file_name ); |
| 131 |
compress_file( $cache_file_name ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
#----------------------------------------------------------------------------- |
| 136 |
|
| 137 |
|
| 138 |
# Write a QDirStat cache. |
| 139 |
# |
| 140 |
# Parameters: |
| 141 |
# $toplevel_dir |
| 142 |
# $cache_file_name |
| 143 |
|
| 144 |
sub write_cache_file() |
| 145 |
{ |
| 146 |
my ( $toplevel_dir, $cache_file_name ) = @_; |
| 147 |
my $start_time = time(); |
| 148 |
|
| 149 |
open( CACHE, ">" . $cache_file_name ) or die "Can't open $cache_file_name"; |
| 150 |
binmode( CACHE, ":bytes" ); |
| 151 |
write_cache_header(); |
| 152 |
write_cache_tree( $toplevel_dir ); |
| 153 |
|
| 154 |
my $elapsed = time() - $start_time; |
| 155 |
my ( $sec, $min, $hours ) = gmtime( $elapsed ); |
| 156 |
printf CACHE "# Elapsed time: %d:%02d:%02d\n", $hours, $min, $sec; |
| 157 |
|
| 158 |
close( CACHE ); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
#----------------------------------------------------------------------------- |
| 163 |
|
| 164 |
|
| 165 |
# Compress a file if its extension is ".gz". |
| 166 |
# |
| 167 |
# Parameters: |
| 168 |
# $file_name |
| 169 |
|
| 170 |
sub compress_file() |
| 171 |
{ |
| 172 |
my ( $file_name ) = @_; |
| 173 |
|
| 174 |
if ( $file_name =~ /.*\.gz$/ ) |
| 175 |
{ |
| 176 |
my $uncompressed_name = $file_name; |
| 177 |
$uncompressed_name =~ s/\.gz$//; # Cut off ".gz" extension |
| 178 |
rename( $file_name, $uncompressed_name ); |
| 179 |
logf( "Compressing $file_name" ); |
| 180 |
system( "gzip $uncompressed_name" ); |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
#----------------------------------------------------------------------------- |
| 187 |
|
| 188 |
|
| 189 |
# Write the cache file header |
| 190 |
# |
| 191 |
# Parameters: |
| 192 |
# --- |
| 193 |
|
| 194 |
sub write_cache_header() |
| 195 |
{ |
| 196 |
my $version = $with_uid_gid_perm ? "2.0" : "1.0"; |
| 197 |
|
| 198 |
print CACHE <<"EOF"; |
| 199 |
[qdirstat $version cache file] |
| 200 |
# Generated by qdirstat-cache-writer |
| 201 |
# Do not edit! |
| 202 |
# |
| 203 |
EOF |
| 204 |
|
| 205 |
if ( $with_uid_gid_perm ) |
| 206 |
{ |
| 207 |
print CACHE <<'EOF'; |
| 208 |
# Type path size uid gid perm. mtime <optional fields> |
| 209 |
|
| 210 |
EOF |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
print CACHE <<'EOF'; |
| 215 |
# Type path size mtime <optional fields> |
| 216 |
|
| 217 |
EOF |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
#----------------------------------------------------------------------------- |
| 223 |
|
| 224 |
|
| 225 |
# Write cache entries for a directory tree. |
| 226 |
# |
| 227 |
# Parameters: |
| 228 |
# $dir Starting directory |
| 229 |
|
| 230 |
sub write_cache_tree($); # Need prototype for calling recursively |
| 231 |
|
| 232 |
sub write_cache_tree($) |
| 233 |
{ |
| 234 |
my ( $dir ) = @_; |
| 235 |
logf( "Reading $dir" ); |
| 236 |
|
| 237 |
my @files; |
| 238 |
my @subdirs; |
| 239 |
|
| 240 |
my $success = opendir( DIR, $dir ); |
| 241 |
|
| 242 |
if ( ! $success ) |
| 243 |
{ |
| 244 |
my $msg = "Can't open $dir: $ERRNO\n"; |
| 245 |
print CACHE "# $msg\n"; |
| 246 |
logf( $msg ); |
| 247 |
|
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
my $entry; |
| 252 |
|
| 253 |
while ( $entry = readdir( DIR ) ) |
| 254 |
{ |
| 255 |
if ( $entry ne "." and |
| 256 |
$entry ne ".." ) |
| 257 |
{ |
| 258 |
my $full_path = $dir . "/" . $entry; |
| 259 |
|
| 260 |
if ( -d $full_path && |
| 261 |
! -l $full_path ) |
| 262 |
{ |
| 263 |
push @subdirs, $entry; |
| 264 |
} |
| 265 |
else |
| 266 |
{ |
| 267 |
push @files, $entry; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
closedir( DIR ); |
| 273 |
|
| 274 |
if ( write_dir_entry( $dir ) ) |
| 275 |
{ |
| 276 |
my $file; |
| 277 |
|
| 278 |
foreach $file ( @files ) |
| 279 |
{ |
| 280 |
write_file_entry( $dir, $file ); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
my $subdir; |
| 285 |
|
| 286 |
foreach $subdir ( @subdirs ) |
| 287 |
{ |
| 288 |
write_cache_tree( $dir . "/" . $subdir ); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
#----------------------------------------------------------------------------- |
| 295 |
|
| 296 |
|
| 297 |
# Write a cache entry for a directory. |
| 298 |
# |
| 299 |
# If the device of this directory is not the same as the toplevel device |
| 300 |
# (i.e., if this is a mount point and thus filesystem boundaries would be |
| 301 |
# crossed) only a comment line is written and an error value '0' is returned |
| 302 |
# unless the "-m" command line option was used. |
| 303 |
# |
| 304 |
# Parameters: |
| 305 |
# $dir directory |
| 306 |
# |
| 307 |
# Return value: |
| 308 |
# 1 OK to continue |
| 309 |
# 0 don't continue, filesystem boundary would be crossed |
| 310 |
|
| 311 |
sub write_dir_entry() |
| 312 |
{ |
| 313 |
my ( $dir ) = @_; |
| 314 |
my @lstat_result = lstat( $dir ); |
| 315 |
|
| 316 |
if ( scalar @lstat_result == 0 ) # Empty array -> lstat() failed |
| 317 |
{ |
| 318 |
my $msg = "lstat() failed for $dir"; |
| 319 |
print CACHE "# $msg\n"; |
| 320 |
logf( $msg ); |
| 321 |
|
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
my ( $dev_no, |
| 326 |
$ino, |
| 327 |
$mode, |
| 328 |
$links, |
| 329 |
$uid, |
| 330 |
$gid, |
| 331 |
$rdev, |
| 332 |
$size, |
| 333 |
$atime, |
| 334 |
$mtime, |
| 335 |
$ctime, |
| 336 |
$blksize, |
| 337 |
$blocks ) = @lstat_result; |
| 338 |
|
| 339 |
$size = 0 if $ignore_dir_own_size; # issue #281 |
| 340 |
|
| 341 |
$dir =~ s://+:/:g; # Replace multiple // with one |
| 342 |
my $escaped_dir = uri_escape( $dir, $unsafe_chars ); |
| 343 |
|
| 344 |
# Write cache file entry for this directory (even if it's a mount point) |
| 345 |
|
| 346 |
printf CACHE "D %-30s", $escaped_dir; |
| 347 |
print CACHE "\t$size"; |
| 348 |
write_uid_gid_perm( $uid, $gid, $mode ); |
| 349 |
printf CACHE "\t0x%x\n", $mtime; |
| 350 |
|
| 351 |
if ( ! defined( $toplevel_dev_no ) ) |
| 352 |
{ |
| 353 |
$toplevel_dev_no = $dev_no; |
| 354 |
$toplevel_dev_name = device_name( $dir ); |
| 355 |
print CACHE "# Device: $toplevel_dev_name\n\n"; |
| 356 |
} |
| 357 |
|
| 358 |
if ( $dev_no == $toplevel_dev_no || $scan_mounted ) |
| 359 |
{ |
| 360 |
return 1; |
| 361 |
} |
| 362 |
|
| 363 |
my $dev_name = device_name( $dir ); |
| 364 |
my $fs_boundary = $dev_name ne $toplevel_dev_name; |
| 365 |
my $msg; |
| 366 |
|
| 367 |
if ( $fs_boundary ) |
| 368 |
{ |
| 369 |
$msg = "Filesystem boundary at mount point $dir on device $dev_name"; |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
$msg = "Mount point $dir is still on the same device $dev_name"; |
| 374 |
} |
| 375 |
|
| 376 |
print CACHE "# $msg\n\n"; |
| 377 |
logf( $msg ); |
| 378 |
|
| 379 |
return ! $fs_boundary; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
#----------------------------------------------------------------------------- |
| 384 |
|
| 385 |
|
| 386 |
# Get the device name where a directory is on from the 'df' command. |
| 387 |
# |
| 388 |
# Parameters: |
| 389 |
# $dir directory |
| 390 |
# |
| 391 |
# Return value: |
| 392 |
# device name ("/dev/sda3", "/dev/system/root") |
| 393 |
|
| 394 |
sub device_name() |
| 395 |
{ |
| 396 |
my ( $dir ) = @_; |
| 397 |
|
| 398 |
my @df_output = `df "$dir" 2>/dev/null`; |
| 399 |
return "<unknown>" if scalar @df_output < 1; |
| 400 |
|
| 401 |
shift @df_output; # Remove header line |
| 402 |
my ( $line ) = @df_output; |
| 403 |
my ( $device_name ) = split( '\s+', $line ); |
| 404 |
deb( "Directory $dir is on device $device_name" ); |
| 405 |
|
| 406 |
return $device_name; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
#----------------------------------------------------------------------------- |
| 411 |
|
| 412 |
|
| 413 |
# Write a cache entry for a plain file (or other non-directory i-node) |
| 414 |
# |
| 415 |
# Parameters: |
| 416 |
# $dir directory |
| 417 |
# $name file name (without path) |
| 418 |
|
| 419 |
sub write_file_entry() |
| 420 |
{ |
| 421 |
my ( $dir, $name ) = @_; |
| 422 |
my @lstat_result = lstat( $dir . "/" . $name ); |
| 423 |
|
| 424 |
if ( scalar @lstat_result == 0 ) # Empty array -> lstat() failed |
| 425 |
{ |
| 426 |
my $msg = "lstat() failed for $dir/$name"; |
| 427 |
print CACHE "# $msg\n"; |
| 428 |
logf( $msg ); |
| 429 |
|
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
my ( $dev, |
| 434 |
$ino, |
| 435 |
$mode, |
| 436 |
$links, |
| 437 |
$uid, |
| 438 |
$gid, |
| 439 |
$rdev, |
| 440 |
$size, |
| 441 |
$atime, |
| 442 |
$mtime, |
| 443 |
$ctime, |
| 444 |
$blksize, |
| 445 |
$blocks ) = @lstat_result; |
| 446 |
|
| 447 |
my $type = "F"; |
| 448 |
|
| 449 |
if ( S_ISREG ( $mode ) ) { $type = "F"; } |
| 450 |
elsif ( S_ISLNK ( $mode ) ) { $type = "L"; } |
| 451 |
elsif ( S_ISBLK ( $mode ) ) { $type = "BlockDev"; } |
| 452 |
elsif ( S_ISCHR ( $mode ) ) { $type = "CharDev"; } |
| 453 |
elsif ( S_ISFIFO( $mode ) ) { $type = "FIFO"; } |
| 454 |
elsif ( S_ISSOCK( $mode ) ) { $type = "Socket"; } |
| 455 |
|
| 456 |
print CACHE "$type"; |
| 457 |
|
| 458 |
$name = uri_escape( $name, $unsafe_chars ); |
| 459 |
|
| 460 |
if ( $long_format ) |
| 461 |
{ |
| 462 |
$dir = uri_escape( $dir, $unsafe_chars ); |
| 463 |
my $full_path = $dir . "/" . $name; |
| 464 |
$full_path =~ s://+:/:g; # Replace multiple // with one |
| 465 |
|
| 466 |
printf CACHE "\t%-30s", $full_path; |
| 467 |
} |
| 468 |
else |
| 469 |
{ |
| 470 |
printf CACHE "\t%-24s", $name; |
| 471 |
} |
| 472 |
|
| 473 |
print CACHE "\t$size"; |
| 474 |
write_uid_gid_perm( $uid, $gid, $mode ); |
| 475 |
printf CACHE "\t0x%x", $mtime; |
| 476 |
|
| 477 |
print CACHE "\tblocks: $blocks" if $blocks > 0 && $blocks * 512 < $size; # Sparse file? |
| 478 |
print CACHE "\tlinks: $links" if $links > 1; |
| 479 |
|
| 480 |
print CACHE "\n"; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
#----------------------------------------------------------------------------- |
| 485 |
|
| 486 |
|
| 487 |
# Write the fields for uid, gid, permissions if $with_uid_gid_perm is set |
| 488 |
# |
| 489 |
# Parameters: |
| 490 |
# $uid numeric user ID |
| 491 |
# $gid numeric group ID |
| 492 |
# $mode numeric file mode (possibly including type) |
| 493 |
|
| 494 |
sub write_uid_gid_perm() |
| 495 |
{ |
| 496 |
return unless $with_uid_gid_perm; |
| 497 |
|
| 498 |
my ( $uid, $gid, $mode ) = @_; |
| 499 |
my $perm = $mode & 07777; |
| 500 |
|
| 501 |
printf CACHE "\t%d %d 0%3o", $uid, $gid, $perm; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
#----------------------------------------------------------------------------- |
| 506 |
|
| 507 |
|
| 508 |
# Log a message to stdout if verbose mode is set |
| 509 |
# (command line option '-v'). |
| 510 |
# |
| 511 |
# Parameters: |
| 512 |
# Messages to write (any number). |
| 513 |
|
| 514 |
sub logf() |
| 515 |
{ |
| 516 |
my $msg; |
| 517 |
|
| 518 |
if ( $verbose ) |
| 519 |
{ |
| 520 |
foreach $msg( @_ ) |
| 521 |
{ |
| 522 |
print $msg . " "; |
| 523 |
} |
| 524 |
|
| 525 |
$OUTPUT_AUTOFLUSH = 1; # inhibit buffering |
| 526 |
print "\n"; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
#----------------------------------------------------------------------------- |
| 532 |
|
| 533 |
|
| 534 |
# Log a debugging message to stdout if debug mode is set |
| 535 |
# (command line option '-d'). |
| 536 |
# |
| 537 |
# Parameters: |
| 538 |
# Messages to write (any number). |
| 539 |
|
| 540 |
sub deb() |
| 541 |
{ |
| 542 |
my $msg; |
| 543 |
|
| 544 |
if ( $debug ) |
| 545 |
{ |
| 546 |
foreach $msg( @_ ) |
| 547 |
{ |
| 548 |
print $msg . " "; |
| 549 |
} |
| 550 |
|
| 551 |
$OUTPUT_AUTOFLUSH = 1; # inhibit buffering |
| 552 |
print "\n"; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
#----------------------------------------------------------------------------- |
| 558 |
|
| 559 |
|
| 560 |
# Print usage message and abort program. |
| 561 |
# |
| 562 |
# Parameters: |
| 563 |
# --- |
| 564 |
|
| 565 |
sub usage() |
| 566 |
{ |
| 567 |
die <<"USAGE-END"; |
| 568 |
|
| 569 |
qdirstat-cache-writer - script to write QDirStat cache files from cron jobs |
| 570 |
|
| 571 |
QDirStat can read its information from cache files. This is a lot faster than |
| 572 |
reading all the directories in a directory tree and obtaining detailed |
| 573 |
information (size, type, last modification time) for each file and directory |
| 574 |
with the opendir() / readdir() and lstat() system calls for each individual |
| 575 |
file and directory. |
| 576 |
|
| 577 |
QDirStat can also write those cache files (\"Write Cache File...\" from the |
| 578 |
\"File\" menu), but the whole point of cache files is being able to do that in |
| 579 |
the background when the user does not have to wait for it - like in a cron |
| 580 |
job running in the middle of the night. QDirStat itself cannot be used to do |
| 581 |
that because it is a KDE program and thus an X program that needs access to |
| 582 |
an X display - which cron does not provide. |
| 583 |
|
| 584 |
This is what this Perl script is for. |
| 585 |
|
| 586 |
Usage: |
| 587 |
$0 [-ldvhi] <directory> [<cache-file-name>] |
| 588 |
|
| 589 |
If not specified, <cache-file-name> defaults to \"$default_cache_file_name\" |
| 590 |
in <directory>. |
| 591 |
|
| 592 |
If <cache-file-name> ends with \".gz\", it will be compressed with gzip. |
| 593 |
qdirstat can read gzipped and plain text cache files. |
| 594 |
|
| 595 |
-l long format - always add full path, even for plain files |
| 596 |
-1 file format 1.0 without UID/GID/permissions |
| 597 |
-2 (default) file format 2.0 with UID/GID/permissions |
| 598 |
-m scan mounted filesystems (cross filesystem boundaries) |
| 599 |
-i ignore directory own size. Uuseful if the filesystem reports |
| 600 |
the subtree size as the size of the directory (like CephFS). |
| 601 |
-v verbose |
| 602 |
-d debug |
| 603 |
-h help (this usage message) |
| 604 |
|
| 605 |
USAGE-END |
| 606 |
|
| 607 |
} |