Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do I know if I can delete a file/folder?

by soffen (Acolyte)
on Jul 26, 2002 at 04:17 UTC ( [id://185420]=perlquestion: print w/replies, xml ) Need Help??

soffen has asked for the wisdom of the Perl Monks concerning the following question:

Of all the file checks I can run, which one is an indication to me that my script can delete a file or folder? I can tell if I can read a file/folder and I can tell if I can write to a file/folder, but how do I tell if I can delete it, other than actually trying to delete it?

Forgive me my ignorance...
  • Comment on How do I know if I can delete a file/folder?

Replies are listed 'Best First'.
Re: How do I know if I can delete a file/folder?
by gmpassos (Priest) on Jul 26, 2002 at 06:32 UTC
    Small question, hard answer! This depend of the system, the type of file, if is directory, link, is loked or opened, etc...

    * For files:
    your need to know if you have permission (like unix), and if it's loked, and in some systems if it's opened.

    "Don't assume that a single unlink completely gets rid of the file: some filesystems (like VMS) have versioned filesystems, and unlink() removes only the most recent one". (from pod)

    * For links (if the OS accept):
    Are you trying to delete the link or the target of the link!? If is the target all the check for files need to be done.

    * For directorys:
    In almost OS, it need to be clean, without files or sub-directorys inside. In some OS like Windows it can't be opened by another program.

    If you really want to make this easy and safe, use a module! The advantage of the module is that it makes all the checks, for each OS, and the code are already tested!

    * See this modules:

    File::CheckTree (Maybe this is only what you need)
    File::Spec (for path)
    File::Stat (can be usefull for links)
    filestest (Another way to test permissions)

    The good thing is that this modules comes with the standart release! :-P

    Take a look at "perlport - Writing portable Perl", specially in the item "Files and Filesystems", but you should read every thing, lot of things that we have to know are there!

    "The creativity is the expression of the liberty".
Re: How do I know if I can delete a file/folder?
by Chady (Priest) on Jul 26, 2002 at 08:30 UTC
    <joke level="lame">Try deleting the file... if it works, then you have permission:
    $file = '/path/to/file'; eval "unlink $file"; $@ && print "You do not have delete permission on $file\n";
    </joke>
    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: How do I know if I can delete a file/folder?
by BrowserUk (Patriarch) on Jul 26, 2002 at 08:44 UTC

    I'm probably way off here, cos otherwise someone else would have spotted this I'm sure but...at the risk of egg-on-my-face.

    The only reason I can think of for wanting to know if you can delete a file, is because you want to delete that file?

    So, if you want to delete it, but want to take some other action (like changing permissions) if you cannot delete the file, simply try, and take those actions if the delete fails.

    for (@comdemnedFilesOrDirs){ if( -f && !unlink or -d _ && !rmdir ){ # change permission here (uncomment as appropriate) # chmod( nnn, $_ ) # Win32::FileSecurity::Set( $_, Win32::FileSecurity::MakeMask( + qw( CHANGE ))) and redo; } }

    You'll will need to add code to check that the change of permissions worked and last or die "....$!" incase the userid the code is running as doesn't have permission to change the permissions, otherwise this would become an endless loop.

      The only reason I can think of for wanting to know if you can delete a file, is because you want to delete that file?

      It's OK your not alone in that line of thinking :) see replies from dws and merlyn to -e and unlink.

      --
      Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

Re: How do I know if I can delete a file/folder?
by grep (Monsignor) on Jul 26, 2002 at 05:44 UTC

    If you are in a *nix it's easy. To delete a file (in a directory you can get to) you must have write permissions for that directory. So all you have to do is successfully open a file in the same directory.

    For a more OS independant solution copy the file (cp -p) to a backup file then delete the original

    As a note when you ask a question that is heavily dependant on a specific OS please include that information

    UPDATE: Changed modern version of linux to *nix



    grep
    Just me, the boy and these two monks, no questions asked.
      Sorry grep I had to down vote your node because at worse is just wrong and at best its misleading.

      If you are in a modern version of linux it's easy
      What does 'modern linux' mean? The basic operation of *nix file permission and ownership mechanisms have been around since before my beard started turning grey.

      To delete a file (in a directory you can get to) you must have write permissions for that directory.
      Not complete, you must also have write permissions on the file. What if the directory is 777 but he file is 644 and owned by root or file is owned by you and is 444? Try this:

      Prologue drwxrwxrwx 2 mitd mitd 4096 Jul 26 02:36 testdir -r--r--r-- 1 mitd mitd 0 Jul 26 02:36 testdir/myfile perl -e 'open(FILE,">myfile") or die $!'

      For a more OS independant solution copy the file (cp -p) to a backup file then delete the original
      'OS' dependent true but then you quote a *nix dependent command with no regard to what the ownership permissions conditions might be,

      mitd-Made in the Dark
      'Interactive! Paper tape is interactive!
      If you don't believe me I can show you my paper cut scars!'

        Not complete, you must also have write permissions on the file. What if the directory is 777 but he file is 644 and owned by root or file is owned by you and is 444?

        Nope on unix if you have write permission to the directory you can delete the file, even if it is owned by root and not even readable by you, on Solaris it comes up with a "override protection 600" prompt and then it is gone, tested on BSD and Linux as well.

        --
        Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

        Hello,

        Well, you are wrong. When you delete a file, you are actually changing the directory that the file is in, so write access to that directory is sufficient. When you change the contents of the file, eh, you are actually changing the file itself, so you need write access to the file (write access to the directory is not needed). Your demonstration is in no way related to the question asked.

        Since the original question asks about deleting, then write access to the directory is the right answer, therefore grep is not wrong.

        --
        Alper Ersoy

        I put a plus vote for this, not because grep was wrong, but because mitd sent a reply, not just clicked in the radio! I think that any type of post trying to help another user is valid.

        "The creativity is the expression of the liberty".
Re: How do I know if I can delete a file/folder?
by mitd (Curate) on Jul 26, 2002 at 05:36 UTC
    This simple answer is you, that is you the owner of the process which is attempting to delete the file, must have 'write permissions' on/for that file.

    The definition of 'write permissions' is one of the following:

    1. you are the file owner and the file has owner write permission set.
    2. you are a member of the file's group and the file has group write permissions set
    3. The file has other write permissions.

    The same rule apply to directories with addtional condition that the directory must be empty.

    UPDATE

    Holy Dumbus Old Fartus! why an old fart should not be
    allowed to touch the keyboard past midnight.

    After a well deserved 'slap upside the head' from grep and others.

    Please ignore the above the only excuse I can offer is 1969 bad drugs.

    Repeat after me: File deletion operations are Directory operations and therefore depend on directory ownership and mode settings

    mitd-Made in the Dark
    'Interactive! Paper tape is interactive!
    If you don't believe me I can show you my paper cut scars!'

      THIS IS WRONG FOR UNIX

      As I tried to politely explain in a /msg to mitd a file delete is a directory operation. The file permissions are not a factor. You need directory write permissions to delete a file in *nix

      If you are still unsure please check these links

    • Permissions - By Rutgers Univ.
    • An Introduction to Unix Permissions - By ONLAMP.com
    • File and Directory Permissions
    • CHMOD on perlfect.com
    • To quote from the perlfect.com site:

      Directories

      Another interesting thing to note is that lib/ which is a directory has permissions, too. Permissions take a different meaning for directories. Here's what they mean:

      • read determines if a user can view the directory's contents, i.e. do ls in it.
      • write determines if a user can create new files or delete file in the directory. (Note here that this essentially means that a user with write access to a directory can delete files in the directory even if he/she doesn't have write permissions for the file! So be careful with this.)
      • execute determines if the user can cd into the directory.


      grep
      Just me, the boy and these two monks, no questions asked.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://185420]
Approved by mitd
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found