in reply to Shotgun.pl - Shoots Holes in Files

Excuse me. I have a question regarding this line:

die("Target file must be plain text!") unless -f $O{target};

I thought that -f tests if a file is a plain file. In other words if it's not a directory or some other weird thing that you might encounter in a Linux file system. I was also told that -f tests if a file exists. If it doesn't, it returns false. If it's a directory, it returns false. If it's a real file that has byte contents, whether it's binary or plain text, it will return true. Am I correct?

Replies are listed 'Best First'.
Re^2: Shotgun.pl - Shoots Holes in Files (Files References)
by eyepopslikeamosquito (Archbishop) on Jan 30, 2024 at 04:51 UTC

    harangzsolt33, further to ++Fletch's spot-on reply, just a bit more detail, in case you find it useful.

    From perldoc -X file test, note that the -T/-B file test operators (to tell if a file is an ASCII or UTF-8 text file or not) is done via a heuristic guess as follows:

    The first block or so of the file is examined to see if it is valid UTF-8 that includes non-ASCII characters. If so, it's a -T file. Otherwise, that same portion of the file is examined for odd characters such as strange control codes or characters with the high bit set. If more than a third of the characters are strange, it's a -B file; otherwise it's a -T file.

    Also, any file containing a zero byte in the examined portion is considered a binary file. (If executed within the scope of a use locale which includes LC_CTYPE, odd characters are anything that isn't a printable nor space in the current locale.) If -T or -B is used on a filehandle, the current IO buffer is examined rather than the first block. Both -T and -B return true on an empty file, or a file at EOF when testing a filehandle. Because you have to read a file to do the -T test, on most occasions you want to use a -f against the file first, as in: next unless -f $file && -T $file.

    See also:

    References added later

    • perlfaq5 - Files and Formats (perldoc)
    • perldata - Perl data types (perldoc)
    • functions - Built-in functions (perldoc) - see "Functions for filehandles, files, or directories" and "Input and output functions"

    👁️🍾👍🦟
Re^2: Shotgun.pl - Shoots Holes in Files
by jwkrahn (Abbot) on Jan 30, 2024 at 04:33 UTC

    <Ed McMahon voice>YOU ARE CORRECT SIR!</Ed McMahon voice>

    -T FH says whether a file is "text" and -B FH says whether a file is "binary".

    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker
Re^2: Shotgun.pl - Shoots Holes in Files
by Fletch (Bishop) on Jan 30, 2024 at 01:58 UTC

    One could RTFM (-X) and also experiment to find out . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^2: Shotgun.pl - Shoots Holes in Files
by BlueSquare23 (Novice) on Jan 30, 2024 at 19:12 UTC
    Good catch! Yeah I guess since I left && -T $O{target} off of that plain text check line, you can technically use this to shoot binaries as well. Works great too, completely destroys them!
    » cp /usr/bin/ls . » ./ls double_reload.wav ls mag.txt pump.wav reload.wav shotgun.pl sh +ot.wav wall.txt » ./shotgun.pl -reload -type pump -load buck -verbose -target ls Loading shot 0 Loading shot 1 Loading shot 2 Loading shot 3 Loading shot 4 Shotgun reloaded! { "pump" : { "num_rounds" : 5, "load" : "buck" } } POW! POW! POW! POW! POW! » diff ls /usr/bin/ls Binary files ls and /usr/bin/ls differ » ./ls Segmentation fault (core dumped)
    Unfortunately, of course the pattern is not exactly visible in the same way as shooting a -T (ASCII or UTF-8) plain text file. But the destructive capabilities are still there!