[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: not wanting to delete somebody's home directory



On 7/5/19 8:24 AM, mick crane wrote:
hello, I doing some code in perl and I'm not very good at it. code
makes some images and saves them to a directory. If the directory
doesn't exist it gets made and if it does exist all the files in it
get deleted before putting some new ones in. I'm thinking that if I
ever give it to somebody it's possible they might call the directory "~/" or something and end up deleting all their files, which I'd like
to avoid. How would that best be avoided ? Is that something to do
with chroot which I don't know anything about.

On 7/5/19 8:32 AM, mick crane wrote:
after they get made I rename them all in consecutive order and if there's other files in there the number order will get messed up.

On 7/5/19 8:38 AM, mick crane wrote:> I  guess the safest thing would be
to keep making new directories called
the current time and only delete things by hand but still would be
interested if there is a solution.

On 7/5/19 10:39 AM, mick crane wrote:
I'm incrementing the number by the loop and some software sees 2 as bigger that 10 or something like this. I can probably get around that
by adding to a very large number in the loop and calling them that. I
don't mind deleting them by hand but if I have to run program
several times to tweak what images look like it slows things down. I
was interested though if there was some instruction to put at the
top of the file to make perl think that the directory the file was in
was "/"

On 7/5/19 10:56 AM, mick crane wrote:
Ah Ok, I could assert a directory and then the output directory make that a subdirectory. so if they typed "~/" "./images_out/~/" would
cause an error ?

On 7/5/19 11:59 AM, mick crane wrote:
a directory called "~/" might be tricky to delete ?

Here are some ideas regarding what to do if files already exist in the destination directory:

1.  Prompt the user before deleting each file.

2.  Prompt the user once before deleting all files.

3. Provide "--yes" and "--no" command line options for pre-answering prompts.

4.  Provide a "--force" option that deletes all files without prompting.


If you are serious about programming in Perl, you should buy the Perl Cookbook:

http://shop.oreilly.com/product/9780596003135.do

https://www.oreilly.com/library/view/perl-cookbook/1565922433/ch09s03.html

https://www.oreilly.com/library/view/perl-cookbook/1565922433/ch15s02.html


A demo script and command line session follow.


David


2019-07-05 13:17:45 dpchrist@tinkywinky ~/sandbox/perl
$ cat prompt-delete-file.pl
#!perl
# $Header: /var/local/cvs/dpchrist/sandbox/perl/prompt-delete-file.pl,v 1.3 2019/07/05 20:17:44 dpchrist Exp $
# Demonstrate command-line option and interactive prompt
# by David Paul Christensen dpchrist@holgerdanske.com
# Public Domain

use strict;
use warnings;
use File::Slurp;
use Getopt::Long;

sub prompt
{
    print "@_? ";
    return <STDIN>;
}

### main
{
    my $f = $0 . '.tmp';
    my %opt;

    GetOptions \%opt, 'force';

    unless (-f $f) {
	warn "Writing $f\n";
	write_file $f, "hello, world!\n";
    }

    if ($opt{force} || prompt("Remove '$f' (y/N)") =~ /y/i) {
	warn "Unlinking $f\n";
	unlink $f or die "unlink $f: $!";
    }
}

2019-07-05 13:17:59 dpchrist@tinkywinky ~/sandbox/perl
$ perl prompt-delete-file.pl
Writing prompt-delete-file.pl.tmp
Remove 'prompt-delete-file.pl.tmp' (y/N)?

2019-07-05 13:18:08 dpchrist@tinkywinky ~/sandbox/perl
$ cat prompt-delete-file.pl.tmp
hello, world!

2019-07-05 13:18:13 dpchrist@tinkywinky ~/sandbox/perl
$ perl prompt-delete-file.pl
Remove 'prompt-delete-file.pl.tmp' (y/N)? y
Unlinking prompt-delete-file.pl.tmp

2019-07-05 13:18:19 dpchrist@tinkywinky ~/sandbox/perl
$ cat prompt-delete-file.pl.tmp
cat: prompt-delete-file.pl.tmp: No such file or directory

2019-07-05 13:18:21 dpchrist@tinkywinky ~/sandbox/perl
$ perl prompt-delete-file.pl --force
Writing prompt-delete-file.pl.tmp
Unlinking prompt-delete-file.pl.tmp



Reply to: