Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #210810

Re: not wanting to delete somebody's home directory

From David Christensen <dpchrist@holgerdanske.com>
Newsgroups linux.debian.user
Subject Re: not wanting to delete somebody's home directory
Date 2019-07-05 22:30 +0200
Message-ID <ygD0e-4pB-3@gated-at.bofh.it> (permalink)
References <ygyjU-1Ef-5@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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

Back to linux.debian.user | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

not wanting to delete somebody's home directory mick crane <mick.crane@gmail.com> - 2019-07-05 17:30 +0200
  Re: not wanting to delete somebody's home directory Greg Wooledge <wooledg@eeg.ccf.org> - 2019-07-05 17:30 +0200
    Re: not wanting to delete somebody's home directory mick crane <mick.crane@gmail.com> - 2019-07-05 17:40 +0200
      Re: not wanting to delete somebody's home directory The Wanderer <wanderer@fastmail.fm> - 2019-07-05 17:40 +0200
        Re: not wanting to delete somebody's home directory Greg Wooledge <wooledg@eeg.ccf.org> - 2019-07-05 17:50 +0200
          Re: not wanting to delete somebody's home directory The Wanderer <wanderer@fastmail.fm> - 2019-07-05 18:20 +0200
            Re: not wanting to delete somebody's home directory Greg Wooledge <wooledg@eeg.ccf.org> - 2019-07-05 19:00 +0200
              Re: not wanting to delete somebody's home directory The Wanderer <wanderer@fastmail.fm> - 2019-07-05 19:30 +0200
      Re: not wanting to delete somebody's home directory mick crane <mick.crane@gmail.com> - 2019-07-05 17:40 +0200
  Re: not wanting to delete somebody's home directory songbird <songbird@anthive.com> - 2019-07-05 18:40 +0200
    Re: not wanting to delete somebody's home directory john doe <johndoe65534@mail.com> - 2019-07-05 19:40 +0200
      Re: not wanting to delete somebody's home directory Greg Wooledge <wooledg@eeg.ccf.org> - 2019-07-05 20:00 +0200
    Re: not wanting to delete somebody's home directory mick crane <mick.crane@gmail.com> - 2019-07-05 19:50 +0200
      Re: not wanting to delete somebody's home directory David Wright <deblis@lionunicorn.co.uk> - 2019-07-05 21:10 +0200
        Re: not wanting to delete somebody's home directory mick crane <mick.crane@gmail.com> - 2019-07-05 23:00 +0200
      Re: not wanting to delete somebody's home directory Curt <curty@free.fr> - 2019-07-06 10:40 +0200
        Re: not wanting to delete somebody's home directory songbird <songbird@anthive.com> - 2019-07-06 13:10 +0200
          Re: not wanting to delete somebody's home directory Curt <curty@free.fr> - 2019-07-06 14:40 +0200
            Re: not wanting to delete somebody's home directory Wolf Halton <wolf.halton@gmail.com> - 2019-07-06 15:10 +0200
            Re: not wanting to delete somebody's home directory rhkramer@gmail.com - 2019-07-06 16:30 +0200
              Re: not wanting to delete somebody's home directory "Thomas Schmitt" <scdbackup@gmx.net> - 2019-07-06 17:40 +0200
                Re: not wanting to delete somebody's home directory "Jeremy Nicoll" <jn.ml.dbn.25@letterboxes.org> - 2019-07-06 18:10 +0200
            Re: not wanting to delete somebody's home directory David Wright <deblis@lionunicorn.co.uk> - 2019-07-06 17:20 +0200
    Re: not wanting to delete somebody's home directory mick crane <mick.crane@gmail.com> - 2019-07-05 20:00 +0200
      Re: not wanting to delete somebody's home directory mick crane <mick.crane@gmail.com> - 2019-07-05 21:10 +0200
  Re: not wanting to delete somebody's home directory David Christensen <dpchrist@holgerdanske.com> - 2019-07-05 22:30 +0200

csiph-web