|
|
#41 |
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
I rewrote the 'watchfile' script so it will also automatically watch the resource forks of the files you specify if you use the "-rsrc" option.
This version of the script also better handles the case of files getting created & deleted. E.g. to watch all the files under ~/Library/Preferences including their resource forks, you would use the command: watchfile -rsrc ~/Library/Preferences/* It only prints info on the resource forks if they are of non-zero size (i.e. if they exist) I also added a "-beep" option. You would use this option if you want it to make a beep whenever a change is reported. E.g: watchfile -rsrc -beep ~/Library/Preferences/* Here's the script: Code:
#!/usr/bin/perl
# watchfile:
# This script prints info about changes to the file(s) specified
# as command-line arguments.
# Options:
# By default, the script checks on the file(s) every 10 seconds.
# If you want to specify a different time interval, use the "-interval" option.
# By default, the script does not report on access-time changes.
# If you want to be notified of access-time changes, use the "-atime" option
# If you want the script to calculate and compare an MD5 digest for each file,
# use the "-md5" option. This requires the Perl module Digest::MD5
# which is not installed by default. Note that access-time changes will not
# be reported if the "-md5" option is used since the file is accessed
# in order to compute the MD5 digest.
# By default, the script does not report on resource-fork changes.
# If you want to be notified of resource-fork changes, use the "-rsrc" option.
# If you want the script to make a beep when there is a change,
# use the "-beep" option.
# Copyright 2006, Cameron Hayne. All Rights Reserved.
# This program is free software. You may copy or
# redistribute it under the same terms as Perl itself.
# Created: September 2003 Cameron Hayne (macdev@hayne.net)
# Modified: February 2006 to support resource forks
use strict;
use warnings;
use File::stat;
# function declarations
sub usage_error();
sub handle_command_line_options();
sub print_msg($$$);
sub get_file_info($);
sub determine_changes($$);
sub calc_md5($);
# global variables
my $interval = 10; # time (in seconds) between checks
my $report_atime = 0; # whether to report on access time changes
my $report_rsrc = 0; # whether to report on resource forks as well
my $want_md5 = 0; # whether an md5 digest is to be computed for each file
my $want_beep = 0; # whether to make a beep when reporting changes
my @filenames; # names of files to be watched
$| = 1; # enable autoflushing of output so this works better when piped
handle_command_line_options();
MAIN:
{
my %fileinfo; # file info hashed by filename
foreach my $filename (@filenames)
{
my $info = get_file_info($filename);
$fileinfo{$filename} = $info;
if ($info->{seen})
{
print_msg($filename, $info, "Stored info");
}
else
{
print_msg($filename, $info, "Non-existent");
}
}
print "------------------------------------------------\n";
while (1)
{
my $changes = 0;
foreach my $filename (@filenames)
{
my $prev_info = $fileinfo{$filename};
my $info = get_file_info($filename);
my $mods = determine_changes($prev_info, $info);
if ($mods)
{
print_msg($filename, $info, "Changed: $mods");
$changes += 1;
}
$fileinfo{$filename} = $info;
}
if ($want_beep && $changes > 0)
{
print "\a"; # make a beep
}
sleep $interval;
}
}
#----- FUNCTIONS -----
sub usage_error()
{
my $options = "[-interval n] [-atime] [-md5] [-rsrc] [-beep]";
print STDERR "usage: watchfile $options filename(s)\n";
exit(1);
}
sub handle_command_line_options()
{
use Getopt::Long;
GetOptions(
'interval=i' => \$interval,
'atime' => \$report_atime,
'md5' => \$want_md5,
'rsrc' => \$report_rsrc,
'beep' => \$want_beep,
) or usage_error();
usage_error() unless scalar(@ARGV) >= 1; # must give a filename
@filenames = @ARGV;
# check validity of option values
die "interval must be positive\n" if $interval <= 0;
if ($want_md5)
{
# check that the MD5 module is installed
unless (eval "require Digest::MD5")
{
warn "Couldn't load Digest::MD5 - md5 digests will not be used\n";
$want_md5 = 0;
}
# Calculating the MD5 digest accesses the file
# so there is no point in reporting on atime
$report_atime = 0 if $want_md5;
# doing MD5 of directories is not supported
foreach my $file (@filenames)
{
if (-d $file)
{
warn "MD5 of directories is not supported\n";
last; # give the warning only once
}
}
}
}
sub print_msg($$$)
{
my ($filename, $info, $msg) = @_;
my $date = localtime(time());
print "$date: $filename: $msg\n";
if ($info->{seen}) # file exists
{
system("/bin/ls -ld \"$filename\"");
if ($report_rsrc && $info->{rsrc}->size > 0)
{
my $rsrc_filename = "$filename/..namedfork/rsrc";
system("/bin/ls -ld \"$rsrc_filename\"");
}
}
}
sub get_file_info($)
{
my ($filename) = @_;
my $info;
if (-e $filename) # check that the file exists
{
$info->{seen} = 1;
$info->{data} = stat($filename)
or die "Can't stat file $filename: $!";
if ($want_md5)
{
$info->{data_md5} = calc_md5($filename);
}
if ($report_rsrc)
{
my $rsrc_filename = "$filename/..namedfork/rsrc";
$info->{rsrc} = stat($rsrc_filename)
or die "Can't stat file $rsrc_filename: $!";
if ($want_md5)
{
$info->{rsrc_md5} = calc_md5($rsrc_filename);
}
}
}
else # file doesn't exist
{
$info->{seen} = 0;
$info->{data} = undef;
$info->{rsrc} = undef;
}
return $info;
}
# returns a string specifying what changed
sub determine_changes($$)
{
my ($prev_info, $info) = @_;
return 0 unless defined($prev_info);
return 0 if ($prev_info->{seen} == 0 and $info->{seen} == 0);
my $mods = "";
my $newly_created = ($prev_info->{seen} == 0 and $info->{seen} == 1);
my $newly_deleted = ($prev_info->{seen} == 1 and $info->{seen} == 0);
if ($newly_created)
{
$mods .= "file created";
}
elsif ($newly_deleted)
{
$mods .= "file deleted";
}
my $prev_data = $prev_info->{data};
my $data = $info->{data};
if (defined($prev_data) and defined($data))
{
# see 'man stat' for the meaning of these fields
$mods .= "ino " if ($data->ino != $prev_data->ino);
$mods .= "mode " if ($data->mode != $prev_data->mode);
$mods .= "nlink " if ($data->nlink != $prev_data->nlink);
$mods .= "uid " if ($data->uid != $prev_data->uid);
$mods .= "gid " if ($data->gid != $prev_data->gid);
$mods .= "size " if ($data->size != $prev_data->size);
if ($want_md5)
{
my $prev_data_md5 = $prev_info->{data_md5};
my $data_md5 = $info->{data_md5};
$mods .= "md5 " if ($data_md5 ne $prev_data_md5);
}
if ($report_atime)
{
$mods .= "atime " if ($data->atime != $prev_data->atime);
}
$mods .= "mtime " if ($data->mtime != $prev_data->mtime);
$mods .= "ctime " if ($data->ctime != $prev_data->ctime);
}
my $prev_rsrc = $prev_info->{rsrc};
my $rsrc = $info->{rsrc};
if (defined($prev_rsrc) and defined($rsrc))
{
$mods .= "rsrc-size " if ($rsrc->size != $prev_rsrc->size);
if ($want_md5)
{
my $prev_rsrc_md5 = $prev_info->{rsrc_md5};
my $rsrc_md5 = $info->{rsrc_md5};
$mods .= "rsrc-md5 " if ($rsrc_md5 ne $prev_rsrc_md5);
}
}
return $mods;
}
sub calc_md5($)
{
my ($filename) = @_;
if (-d $filename)
{
# doing MD5 on a directory is not supported
return "unsupported"; # we need to return something
}
$filename =~ s#^(\s)#./$1#; # protect against leading whitespace
open(FILE, "< $filename\0")
or die "Unable to open file \"$filename\": $!\n";
binmode(FILE); # just in case we're on Windows!
my $md5 = Digest::MD5->new->addfile(*FILE)->hexdigest;
close(FILE);
return $md5;
}
__________________
hayne.net/macosx.html Last edited by hayne; 02-16-2006 at 11:53 PM. |
|
|
|
|
|
#42 |
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
Took a break.
Did this in terminal: ls -l /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes"/..namedfork/rsrc Got this back: -rwxrwxrwx 1 robertki robertki 61215 Feb 17 21:16 /users/robertkinleyjohnson/documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes/..namedfork/rsrc Is the -rwxrwxrwx what I'm looking for? I.e. the path of the fork is?: /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes"/-rwxrwxrwx |
|
|
|
|
|
#43 | |||||||||||||||||||||||
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
That shows that the file "/users/robertkinleyjohnson/documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes" has a resource fork which is 61215 bytes in size. The -rwxrwxrwx is the permissions. This is explained in the Unix FAQ. So I presume that this resource fork is holding one of your custom icons.
__________________
hayne.net/macosx.html |
|||||||||||||||||||||||
|
|
|
|
|
#44 | |||||||||||||||||||
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
Ah, the thing we'd be shooting for then would be to Watchfile the ls -l /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes"/..namedfork/rsrc for changes from 61215 b. I put your updated watchfile code in place of the old code via a text editor and saved it as FebWatchfile under my home folder. ls -l /users/robertkinleyjohnson/febwatchfile/..namedfork/rsrc yields -rw-r--r-- 1 robertki robertki 0 Feb 17 21:07.... So next in Terminal I'll need to chmod u=x /users/robertkinleyjohnson/febwatchfile to change -rw-r--r-- to -rwx-r--r--. Right? (I think). And then do I: % watchfile -rsrc -beep /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes"/*? And then mess around with the Excel file Test Notes to try to make it lose its icon. And I'll here a beep and Terminal/febwatchfile will list a change and hopefully it will correlate to something(s) I can narrow down to for a cause? Hmm, I might just be on the cusp of some understanding. Scary, huh? Let me know if my assumptions and syntax are right; then I'll Terminalize the thing and see what we get. Thanks. |
|||||||||||||||||||
|
|
|
|
|
#45 | |||||||||||||||||||||||
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
Almost correct. Just omit the "/*" at the end. "Test Notes" is a file, not a folder, so you just want to watch that one file. The "*" is used as a wildcard when you want to specify multiple files with one command.
__________________
hayne.net/macosx.html |
|||||||||||||||||||||||
|
|
|
|
|
#46 | |||||||||||||||||||
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
Oh right, I remember. Thanks for looking over my work. I'll report when I find anything. Ciao |
|||||||||||||||||||
|
|
|
|
|
#47 |
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
I must have missed a step.
Last login: Sat Feb 18 21:10:04 on ttyp1 Welcome to Darwin! Upstairs-Computer:~ robertkinleyjohnson$ chmod u=x /users/robertkinleyjohnson/febwatchfile Upstairs-Computer:~ robertkinleyjohnson$ febwatchfile -rsrc -beep /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes" -bash: febwatchfile: command not found So first I double checked the mode change: Upstairs-Computer:~ robertkinleyjohnson$ ls -l /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes"/..namedfork/rsrc -rwxrwxrwx 1 robertki robertki 0 Feb 18 22:51 /users/robertkinleyjohnson/documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes/..namedfork/rsrc That looks like it took right. Retried: Upstairs-Computer:~ robertkinleyjohnson$ febwatchfile -rsrc -beep /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes" Got -bash: febwatchfile: command not found again. Google to Wikipedia tells me that bash is only the Bourne-Again SHell. So... I wonder if you will tell me that that is referring to the fact that most bash commands are runnable straight out the box but that some special bash-language-only commands have to be specially labelled as such. And what's gong on is that Terminal/bash thinks febwatchfile is such a command that it's finding no definition for. Anyway, do you see here what I did wrong? |
|
|
|
|
|
#48 | ||||||||||||||||||||||||||||||||||||||||||
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
Actually you should have done: chmod u+x instead of chmod u=x
Besides the problem due to the above mistake, you probably are having the problem related to the execution PATH not including your current folder. There is a section of the Unix FAQ that talks about the "command not found" problem. But first, fix up the problem with permissions by doing this: Code:
chmod u=rwx /Users/robertkinleyjohnson/febwatchfile Code:
./febwatchfile -rsrc -beep /Users/robertkinleyjohnson/Documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes"
__________________
hayne.net/macosx.html |
||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#49 |
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
Last login: Sun Feb 19 00:20:38 on ttyp1
Welcome to Darwin! Upstairs-Computer:~ robertkinleyjohnson$ chmod u=rwx /Users/robertkinleyjohnson/febwatchfile Upstairs-Computer:~ robertkinleyjohnson$ ./febwatchfile -rsrc -beep /Users/robertkinleyjohnson/Documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes" #: bad interpreter: No such file or directory Thought the two spaces between chmod u=rwx and /Users/robertkinleyjohnson/febwatchfile from your instruction in the code block above might have been the problem. Upstairs-Computer:~ robertkinleyjohnson$ chmod u=rwx /Users/robertkinleyjohnson/febwatchfile Upstairs-Computer:~ robertkinleyjohnson$ ./febwatchfile -rsrc -beep /Users/robertkinleyjohnson/Documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes" #: bad interpreter: No such file or directory Upstairs-Computer:~ robertkinleyjohnson$ Nope, guess not. Took some status readings: ls /Users/robertkinleyjohnson/febwatchfile /Users/robertkinleyjohnson/febwatchfile (Good) Upstairs-Computer:~ robertkinleyjohnson$ ls /Users/robertkinleyjohnson/Documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes" /Users/robertkinleyjohnson/Documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes (also good) Upstairs-Computer:~ robertkinleyjohnson$ ls -l /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes"/..namedfork/rsrc -rwxrwxrwx 1 robertki robertki 0 Feb 19 03:52 /users/robertkinleyjohnson/documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes/..namedfork/rsrc The permissions look unchanged. Also I notice the size of the resource fork is zero now instead of 61215 bytes. The custom icon is still there. I'll restart after I post to see if somehow the icon fork changed but isn't effecting the icon till after a restart. |
|
|
|
|
|
#50 | |||||||||||||||||||
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
Yep. Interesting and good to know before it confounded the data. The fork can change earlier than the icon stops being displayed by the finder. Weird. |
|||||||||||||||||||
|
|
|
|
|
#51 | ||||||||||||||||||||||||||||||||||||||||||
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
This indicates that something is wrong with the script. I'm suspecting that it wasn't saved with the correct (Unix-style) line endings. (This is covered in the Unix FAQ) What text editor did you use to create the script file?
I suspect that the Finder caches the custom icon. I.e. it stores it in its own private memory after reading it from the resource fork the first time. That would explain why you are still seeing the custom icon even though the resource fork has now been zeroed.
__________________
hayne.net/macosx.html |
||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#52 |
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
If you are still having trouble getting that script to run, please show us the results from the following command (copy & paste it into a Terminal window, press Return, then copy & paste the results back here):
vis -w /Users/robertkinleyjohnson/febwatchfile | cut -c 1-80
__________________
hayne.net/macosx.html |
|
|
|
|
|
#53 | |||||||||||||||||||
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
TextEdit. I'll try Word and save it as text only format. |
|||||||||||||||||||
|
|
|
|
|
#54 |
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
Made new Word file, traded old febwatchfile out, new in.
Last login: Sun Feb 19 10:29:53 on console Welcome to Darwin! Upstairs-Computer:~ robertkinleyjohnson$ chmod u+x /users/robertkinleyjohnson/febwatchfile Get to chmod right from the beginnin this time. Upstairs-Computer:~ robertkinleyjohnson$ ./febwatchfile -rsrc -beep /Users/robertkinleyjohnson/Documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes" #: bad interpreter: No such file or directory Upstairs-Computer:~ robertkinleyjohnson$ Upstairs-Computer:~ robertkinleyjohnson$ vis -w /Users/robertkinleyjohnson/febwatchfile | cut -c 1-80 #!/usr/bin/perl\^M\^M#\040watchfile:\^M#\040This\040script\040prints\040info\040 Upstairs-Computer:~ robertkinleyjohnson$ Today 01:50 PM The Today 01:50 PM surprised me. The other stuff didn't surpise me any. I just don't know what it means period. |
|
|
|
|
|
#55 |
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
Upstairs-Computer:~ robertkinleyjohnson$ ls -l /users/robertkinleyjohnson/febwatchfile
-rwxr--r-- 1 robertki robertki 8224 Feb 19 14:00 /users/robertkinleyjohnson/febwatchfile Upstairs-Computer:~ robertkinleyjohnson$ |
|
|
|
|
|
#56 | |||||||||||||||||||||||
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
I don't understand how TextEdit can be doing this - I thought it always saved using Unix line endings. Who knows what Word uses - don't use Word for this sort of thing. But the above result shows that your script file is using traditional Macintosh line endings (that won't work). You can investigate this later. For the moment, just use the one-line Perl script that is mentioned in the line-endings section of the above Unix FAQ. I.e. run the command: /usr/bin/perl -pi~ -e 's/\r\n?/\n/g' /Users/robertkinleyjohnson/febwatchfile And then try running the script again.
__________________
hayne.net/macosx.html |
|||||||||||||||||||||||
|
|
|
|
|
#57 |
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
usr/bin/perl -pi~ -e 's/\r\n?/\n/g' /Users/robertkinleyjohnson/febwatchfile
Upstairs-Computer:~ robertkinleyjohnson$ vis -w /Users/robertkinleyjohnson/febwatchfile | cut -c 1-80 #!/usr/bin/perl\^J\^J#\040watchfile:\^J#\040This\040script\040prints\040info\040 The ^J#'s changed to M's. ./febwatchfile -rsrc -beep /Users/robertkinleyjohnson/Documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes" Sun Feb 19 14:36:19 2006: /Users/robertkinleyjohnson/Documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes: Stored info -rwxrwxrwx 1 robertki robertki 3130880 Feb 19 14:28 /Users/robertkinleyjohnson/Documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes ------------------------------------------------ Looks like it's running to me. I suppose it will report changes under the ------------------------------------------------ And that now if I wanted to check on the fork to record t-zero data (which I do), that should use a second Terminal window (so I will). Good thing I checked. It'd already set back to zero. I'll add the icon back. And that should also let me see and hear some data. |
|
|
|
|
|
#58 |
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
------------------------------------------------
Sun Feb 19 14:45:39 2006: /Users/robertkinleyjohnson/Documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes: Changed: mtime ctime rsrc-size -rwxrwxrwx 1 robertki robertki 3130880 Feb 19 14:45 /Users/robertkinleyjohnson/Documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes -rwxrwxrwx 1 robertki robertki 61215 Feb 19 14:45 /Users/robertkinleyjohnson/Documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes/..namedfork/rsrc The data showed, but that's one quiet beep. I did notice an oddity with the beep timing though. Before when adding an icon via Get Info I noticed the icon changed in the finder immediately upon paste, BEFORE I even closed the the Get Info window. The beep though came only after I closed it just now. I'll be in touch. And thanks again, I know a lot more Terminal now AND that't due to all the extra time you put in. Thanks. |
|
|
|
|
|
#59 | ||||||||||||||||||||||||||||||||||||||||||
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
The volume of the beep is controlled by your system preferences.
Note that (read the comments at the top of the script) the script only checks on the files every 10 seconds by default. You can change this default by using the "-interval" option. E.g. add "-interval 1" to check every second
__________________
hayne.net/macosx.html |
||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#60 |
|
Triple-A Player
Join Date: Oct 2005
Posts: 179
|
Saved the open Excel file:
Sun Feb 19 14:55:20 2006: /Users/robertkinleyjohnson/Documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes: Changed: mtime ctime rsrc-size -rwxrwxrwx 1 robertki robertki 3130880 Feb 19 14:55 /Users/robertkinleyjohnson/Documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes Did a ls -l /users/robertkinleyjohnson/documents/"Games Upstairs"/"Psychic Game"/"Terminal Icon Testing"/"Test Notes"/..namedfork/rsrc -rwxrwxrwx 1 robertki robertki 0 Feb 19 14:55 /users/robertkinleyjohnson/documents/Games Upstairs/Psychic Game/Terminal Icon Testing/Test Notes/..namedfork/rsrc That was immediate on saving. It's losing the icon very fast EVERY time I save and that's a little new so I'm thinking it may be "in the mood" already. And so our data may be unreliable. I'm going to restart, re-icon, take the fork data, and start febwatchfile again. Ciao. |
|
|
|
![]() |
|
|