Diff -- /var/www/vhosts/3dshawn.com/site1/_file_scan.pl
Diff
/var/www/vhosts/3dshawn.com/site1/_file_scan.pl
modified on local at 2026-07-12 00:02:09
Added
+23
lines
Removed
-4
lines
Context
264
unchanged
Blobs
from 2b862b37ab7e
to 41cc7dc8579b
to 41cc7dc8579b
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | #!/usr/bin/perl |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- filesystem scanner |
| 4 | 4 | # |
| 5 | 5 | # Cron entry point (NOT a CGI). Reads FILE_MONITOR_SETTINGS, walks each |
| 6 | 6 | # configured path, and captures every changed file since the previous |
| 7 | 7 | # tick. Optimizations: |
| 8 | 8 | # |
| 9 | 9 | # * mtime-first check -- cheap stat() every file, hash only when |
| 10 | 10 | # mtime differs vs. the last captured version. Turns a 10 GB |
| 11 | 11 | # codebase scan from minutes into seconds. |
| 12 | 12 | # * Content-addressable BLOB_STORE dedup -- if the file's content |
| 13 | 13 | # bounces back to a previous state, zero new bytes stored. |
| 14 | 14 | # * gzip compression on stored snapshots. |
| 15 | 15 | # * Timestamp-only change suppression (per Shawn's own note file) |
| 16 | 16 | # -- files whose only change is mtime (content-identical) get |
| 17 | 17 | # flagged is_ts_only=1 so the UI can filter noise. |
| 18 | 18 | # * Per-scan max_file_size and file_type_filter honored. |
| 19 | 19 | # |
| 20 | 20 | # Config: /etc/drift_sense/drift_sense.conf (via MODS::Config). |
| 21 | 21 | # Log: /var/log/drift_sense/file_scan.log (via cron redirect). |
| 22 | 22 | #====================================================================== |
| 23 | 23 | use strict; |
| 24 | 24 | use warnings; |
| 25 | 25 | use lib '/var/www/vhosts/3dshawn.com/site1'; |
| 26 | 26 | use POSIX (); |
| 27 | 27 | use Digest::SHA qw(sha256_hex); |
| 28 | 28 | use Compress::Zlib qw(compress); |
| 29 | 29 | use File::Find (); |
| 30 | 30 | use MODS::Config; |
| 31 | 31 | use MODS::DBConnect; |
| 32 | 32 | |
| 33 | 33 | $| = 1; |
| 34 | 34 | my $ts = POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime); |
| 35 | 35 | |
| 36 | 36 | my $cfg = MODS::Config->new; |
| 37 | 37 | my $db = MODS::DBConnect->new; |
| 38 | 38 | my $dbh = $db->db_connect or die "[$ts] cannot connect to DriftSense storage DB\n"; |
| 39 | 39 | |
| 40 | 40 | # ---- Config: global max-file-size default ---- |
| 41 | 41 | my $default_max_size = int($cfg->settings('max_file_size_bytes') || 10_485_760); |
| 42 | ||
| 43 | # ---- Global scan settings (SCAN_GLOBALS singleton) ----------------- | |
| 44 | # Applied UNIVERSALLY across every monitor so operators can set | |
| 45 | # "always ignore .swp, node_modules/, etc." in one place. | |
| 46 | my $g_row = $db->db_readwrite($dbh, q~ | |
| 47 | SELECT global_ignore_list, global_file_type_filter, global_max_file_size_bytes | |
| 48 | FROM SCAN_GLOBALS WHERE id = 1 LIMIT 1 | |
| 49 | ~, __FILE__, __LINE__); | |
| 50 | my $global_ignore = ($g_row && $g_row->{global_ignore_list}) // ''; | |
| 51 | my $global_ftypes = ($g_row && $g_row->{global_file_type_filter}) // ''; | |
| 52 | my $global_max_size = ($g_row && $g_row->{global_max_file_size_bytes}) || 0; | |
| 53 | $default_max_size = $global_max_size if $global_max_size; | |
| 42 | 54 | |
| 43 | 55 | # ---- Fetch active file monitors ---- |
| 44 | 56 | my @scans = $db->db_readwrite_multiple($dbh, q~ |
| 45 | 57 | SELECT file_monitor_list_id, scan_path, ignore_list, file_type_filter, |
| 46 | 58 | max_file_size_bytes, server_id, container_target_id, scan_name |
| 47 | 59 | FROM FILE_MONITOR_SETTINGS |
| 48 | 60 | WHERE status = 1 |
| 49 | 61 | ~, __FILE__, __LINE__); |
| 50 | 62 | |
| 51 | 63 | unless (@scans) { |
| 52 | 64 | print "[$ts] no active file monitors, exiting\n"; |
| 53 | 65 | exit 0; |
| 54 | 66 | } |
| 55 | 67 | |
| 56 | 68 | my $total_added = 0; |
| 57 | 69 | my $total_modified = 0; |
| 58 | 70 | my $total_deleted = 0; |
| 59 | 71 | my $total_ts_only = 0; |
| 60 | 72 | |
| 61 | 73 | foreach my $scan (@scans) { |
| 62 | 74 | my $path = $scan->{scan_path} or next; |
| 63 | 75 | unless (-d $path) { |
| 64 | 76 | print "[$ts] scan #$scan->{file_monitor_list_id} '$scan->{scan_name}': path missing ($path)\n"; |
| 65 | 77 | next; |
| 66 | 78 | } |
| 67 | 79 | |
| 80 | # ---- Ignore patterns: UNION of global + per-monitor ------------ | |
| 68 | 81 | my %ignore_pats; |
| 69 | if ($scan->{ignore_list}) { | |
| 70 | for my $ig (split /[,\n]+/, $scan->{ignore_list}) { | |
| 82 | for my $src ($global_ignore, ($scan->{ignore_list} // '')) { | |
| 83 | next unless length $src; | |
| 84 | for my $ig (split /[,\n]+/, $src) { | |
| 71 | 85 | $ig =~ s/^\s+|\s+$//g; |
| 72 | 86 | $ignore_pats{$ig} = _pattern_to_regex($ig) if length $ig; |
| 73 | 87 | } |
| 74 | 88 | } |
| 75 | 89 | |
| 90 | # ---- File-type filter: per-monitor wins if set, else global --- | |
| 76 | 91 | my %type_filter; |
| 77 | if ($scan->{file_type_filter} && length $scan->{file_type_filter}) { | |
| 78 | for my $ext (split /,/, $scan->{file_type_filter}) { | |
| 92 | my $eff_ftypes = length($scan->{file_type_filter} // '') | |
| 93 | ? $scan->{file_type_filter} | |
| 94 | : $global_ftypes; | |
| 95 | if ($eff_ftypes && length $eff_ftypes) { | |
| 96 | for my $ext (split /,/, $eff_ftypes) { | |
| 79 | 97 | $ext =~ s/^\s+|\s+$//g; |
| 98 | next unless length $ext; | |
| 80 | 99 | $ext = ".$ext" unless $ext =~ /^\./; |
| 81 | 100 | $type_filter{lc $ext} = 1; |
| 82 | 101 | } |
| 83 | 102 | } |
| 84 | 103 | |
| 85 | 104 | my $max_size = $scan->{max_file_size_bytes} || $default_max_size; |
| 86 | 105 | |
| 87 | 106 | # ---- Snapshot of what we captured in the previous tick, keyed by |
| 88 | 107 | # file_name, for delete-detection + mtime-baseline lookup. |
| 89 | 108 | my $prev_sth = $dbh->prepare(qq~ |
| 90 | 109 | SELECT fc.file_name, fc.blob_sha, fc.status, fc.date_time |
| 91 | 110 | FROM FILE_CHANGES fc |
| 92 | 111 | WHERE fc.file_monitor_list_id = $scan->{file_monitor_list_id} |
| 93 | 112 | AND fc.file_changes_id IN ( |
| 94 | 113 | SELECT MAX(inner_fc.file_changes_id) |
| 95 | 114 | FROM FILE_CHANGES inner_fc |
| 96 | 115 | WHERE inner_fc.file_monitor_list_id = $scan->{file_monitor_list_id} |
| 97 | 116 | GROUP BY inner_fc.file_name |
| 98 | 117 | ) |
| 99 | 118 | ~); |
| 100 | 119 | my %prev; |
| 101 | 120 | if ($prev_sth && $prev_sth->execute) { |
| 102 | 121 | while (my $r = $prev_sth->fetchrow_hashref) { |
| 103 | 122 | $prev{$r->{file_name}} = $r; |
| 104 | 123 | } |
| 105 | 124 | $prev_sth->finish; |
| 106 | 125 | } |
| 107 | 126 | |
| 108 | 127 | # ---- Walk + capture ---- |
| 109 | 128 | my %seen; |
| 110 | 129 | File::Find::find({ |
| 111 | 130 | no_chdir => 1, |
| 112 | 131 | wanted => sub { |
| 113 | 132 | my $f = $File::Find::name; |
| 114 | 133 | return if -d $f; |
| 115 | 134 | return unless -f $f; |
| 116 | 135 | |
| 117 | 136 | # Ignore patterns |
| 118 | 137 | for my $pat (keys %ignore_pats) { |
| 119 | 138 | my $re = $ignore_pats{$pat}; |
| 120 | 139 | if ($f =~ $re) { return; } |
| 121 | 140 | } |
| 122 | 141 | |
| 123 | 142 | # File type filter |
| 124 | 143 | if (%type_filter) { |
| 125 | 144 | my $ext_ok = 0; |
| 126 | 145 | for my $e (keys %type_filter) { |
| 127 | 146 | if (lc(substr($f, -length($e))) eq $e) { $ext_ok = 1; last; } |
| 128 | 147 | } |
| 129 | 148 | return unless $ext_ok; |
| 130 | 149 | } |
| 131 | 150 | |
| 132 | 151 | my @st = stat $f; |
| 133 | 152 | return unless @st; |
| 134 | 153 | return if $st[7] > $max_size; # skip huge files |
| 135 | 154 | |
| 136 | 155 | $seen{$f} = 1; |
| 137 | 156 | my $mtime = $st[9]; |
| 138 | 157 | my $prev_row = $prev{$f}; |
| 139 | 158 | |
| 140 | 159 | # mtime-first optimization: skip hashing if mtime hasn't advanced |
| 141 | 160 | # (very common case for unchanged files). |
| 142 | 161 | if ($prev_row && $prev_row->{status} ne 'deleted') { |
| 143 | 162 | my $prev_mtime = _parse_mysql_datetime($prev_row->{date_time}); |
| 144 | 163 | return if $prev_mtime && $prev_mtime == $mtime; |
| 145 | 164 | } |
| 146 | 165 | |
| 147 | 166 | # Read + hash |
| 148 | 167 | my $content = _slurp($f); |
| 149 | 168 | return unless defined $content; |
| 150 | 169 | my $sha = sha256_hex($content); |
| 151 | 170 | |
| 152 | 171 | # Same content as previous capture? timestamp-only change. |
| 153 | 172 | my $is_ts_only = 0; |
| 154 | 173 | my $status = 'added'; |
| 155 | 174 | if ($prev_row && $prev_row->{blob_sha}) { |
| 156 | 175 | if ($prev_row->{blob_sha} eq $sha) { |
| 157 | 176 | $is_ts_only = 1; |
| 158 | 177 | $status = 'modified'; # mtime touched but bytes identical |
| 159 | 178 | } else { |
| 160 | 179 | $status = 'modified'; |
| 161 | 180 | } |
| 162 | 181 | } |
| 163 | 182 | |
| 164 | 183 | # Persist blob (content-addressable, dedup) |
| 165 | 184 | _store_blob($db, $dbh, $sha, $content); |
| 166 | 185 | |
| 167 | 186 | # Insert change row |
| 168 | 187 | my $q_file = $dbh->quote($f); |
| 169 | 188 | my $q_sha = $dbh->quote($sha); |
| 170 | 189 | my $q_stat = $dbh->quote($status); |
| 171 | 190 | my $uid = $st[4]; my $gid = $st[5]; |
| 172 | 191 | my $perm = sprintf('%04o', $st[2] & 07777); |
| 173 | 192 | $db->db_readwrite($dbh, qq~ |
| 174 | 193 | INSERT INTO FILE_CHANGES |
| 175 | 194 | (file_monitor_list_id, server_id, container_target_id, |
| 176 | 195 | file_name, blob_sha, is_ts_only, date_time, status, |
| 177 | 196 | uid, gid, permissions) |
| 178 | 197 | VALUES |
| 179 | 198 | ($scan->{file_monitor_list_id}, $scan->{server_id}, |
| 180 | 199 | ~ . ($scan->{container_target_id} ? $scan->{container_target_id} : 'NULL') . qq~, |
| 181 | 200 | $q_file, $q_sha, $is_ts_only, |
| 182 | 201 | FROM_UNIXTIME($mtime), $q_stat, |
| 183 | 202 | '$uid', '$gid', '$perm') |
| 184 | 203 | ~, __FILE__, __LINE__); |
| 185 | 204 | |
| 186 | 205 | if ($is_ts_only) { $total_ts_only++; } |
| 187 | 206 | elsif ($status eq 'added') { $total_added++; } |
| 188 | 207 | else { $total_modified++; } |
| 189 | 208 | }, |
| 190 | 209 | }, $path); |
| 191 | 210 | |
| 192 | 211 | # ---- Delete detection ---- |
| 193 | 212 | foreach my $f (keys %prev) { |
| 194 | 213 | next if $seen{$f}; |
| 195 | 214 | next if $prev{$f}->{status} eq 'deleted'; # already marked deleted |
| 196 | 215 | my $q_file = $dbh->quote($f); |
| 197 | 216 | $db->db_readwrite($dbh, qq~ |
| 198 | 217 | INSERT INTO FILE_CHANGES |
| 199 | 218 | (file_monitor_list_id, server_id, file_name, status, date_time) |
| 200 | 219 | VALUES |
| 201 | 220 | ($scan->{file_monitor_list_id}, $scan->{server_id}, |
| 202 | 221 | $q_file, 'deleted', NOW()) |
| 203 | 222 | ~, __FILE__, __LINE__); |
| 204 | 223 | $total_deleted++; |
| 205 | 224 | } |
| 206 | 225 | |
| 207 | 226 | # Update last_scanned stamp |
| 208 | 227 | $db->db_readwrite($dbh, qq~ |
| 209 | 228 | UPDATE FILE_MONITOR_SETTINGS |
| 210 | 229 | SET last_scanned = NOW() |
| 211 | 230 | WHERE file_monitor_list_id = $scan->{file_monitor_list_id} |
| 212 | 231 | ~, __FILE__, __LINE__); |
| 213 | 232 | } |
| 214 | 233 | |
| 215 | 234 | $db->db_disconnect($dbh); |
| 216 | 235 | |
| 217 | 236 | print "[$ts] file scan complete: +$total_added added, ~$total_modified modified, " |
| 218 | 237 | . "-$total_deleted deleted, $total_ts_only ts-only\n" |
| 219 | 238 | if ($total_added + $total_modified + $total_deleted + $total_ts_only) > 0; |
| 220 | 239 | |
| 221 | 240 | exit 0; |
| 222 | 241 | |
| 223 | 242 | #--------------------------------------------------------------------- |
| 224 | 243 | sub _slurp { |
| 225 | 244 | my $path = shift; |
| 226 | 245 | open(my $fh, '<:raw', $path) or return undef; |
| 227 | 246 | local $/; my $c = <$fh>; close $fh; |
| 228 | 247 | return $c; |
| 229 | 248 | } |
| 230 | 249 | |
| 231 | 250 | sub _pattern_to_regex { |
| 232 | 251 | my $pat = shift; |
| 233 | 252 | # Simple glob-ish -> regex conversion |
| 234 | 253 | my $re = quotemeta $pat; |
| 235 | 254 | $re =~ s/\\\*/.*/g; |
| 236 | 255 | $re =~ s/\\\?/./g; |
| 237 | 256 | return qr/$re/; |
| 238 | 257 | } |
| 239 | 258 | |
| 240 | 259 | sub _parse_mysql_datetime { |
| 241 | 260 | my $dt = shift; |
| 242 | 261 | return 0 unless $dt && $dt =~ /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})$/; |
| 243 | 262 | require Time::Local; |
| 244 | 263 | return eval { Time::Local::timelocal($6, $5, $4, $3, $2 - 1, $1 - 1900) } || 0; |
| 245 | 264 | } |
| 246 | 265 | |
| 247 | 266 | #--------------------------------------------------------------------- |
| 248 | 267 | sub _store_blob { |
| 249 | 268 | my ($db, $dbh, $sha, $content) = @_; |
| 250 | 269 | my $existing = $db->db_readwrite($dbh, |
| 251 | 270 | "SELECT blob_sha FROM BLOB_STORE WHERE blob_sha = " . $dbh->quote($sha) . " LIMIT 1", |
| 252 | 271 | __FILE__, __LINE__); |
| 253 | 272 | if ($existing && $existing->{blob_sha}) { |
| 254 | 273 | $db->db_readwrite($dbh, |
| 255 | 274 | "UPDATE BLOB_STORE SET ref_count = ref_count + 1 WHERE blob_sha = " . $dbh->quote($sha), |
| 256 | 275 | __FILE__, __LINE__); |
| 257 | 276 | return; |
| 258 | 277 | } |
| 259 | 278 | my $gz = compress($content) // ''; |
| 260 | 279 | my $sth = $dbh->prepare(qq~ |
| 261 | 280 | INSERT INTO BLOB_STORE (blob_sha, content_gz, size_uncompressed, size_compressed, ref_count, first_seen_at) |
| 262 | 281 | VALUES (?, ?, ?, ?, 1, NOW()) |
| 263 | 282 | ~); |
| 264 | 283 | if ($sth) { |
| 265 | 284 | $sth->execute($sha, $gz, length($content), length($gz)); |
| 266 | 285 | $sth->finish; |
| 267 | 286 | } |
| 268 | 287 | } |