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

Can anyone explain to me why i keep getting this error: Permission denied at temp.pl line 20. On this piece of code:
opendir(LOGS, "/perl/ass/")|| die "dir not found"; #Opens the folder + containing the log files @logfiles = readdir(LOGS);#Puts the name of all files into an ar +ray closedir(LOGS); $currentlog = "/perl/ass/@logfiles[$hour]"; chmod (0777, $currentlog); # << shouldnt this give me permission +?!?! open(CURRENTLOG, $currentlog)|| die "file not found: $!"; # << THI +S IS LINE 20
I'm doing this on a windows platform by the way I have tried so many different things :(

Replies are listed 'Best First'.
Re: Perl Permissions
by Joost (Canon) on Dec 03, 2006 at 13:38 UTC
    If you're not allowed to open that file, it's very likely you're also not allowed to change the permissions. Check the return code of chmod:
    chmod(0777,$currentlog) or die "Can't change permissions: $!"
    As noted above there is almost no reason to do the chmod at all: unless you've got a very strange system administrator, you won't be able to change what you can't read.

      I put that line in mycode but it just goes past it and gives' me the same error:
      Permission denied at temp.pl line 20
      Does this mean i do have permission do change its permissions. I dont see how i have to contact the admin as this is MY computer nobody uses it except me :/ so i'm really confused why i dont just have permission to do everything with everything.
        Permission denied at temp.pl line 20
        You know, it would be very helpful if you provided a complete program (as short as possible!) that does only the chmod & open of one file and nothing else. Also provide the list of owners & permissions for the file in question and the directory it's in.

        i'm really confused why i dont just have permission to do everything with everything.
        Probably because the sysadmin doesn't have the time to fix all those installations that get completely scrambled when users are allowed to do anything and everything to it.

Re: Perl Permissions
by virtualsue (Vicar) on Dec 03, 2006 at 14:41 UTC
    It's a good idea to take a look at what your variables contain when you find that your code is not working as you expect. What's in $currentlog? What is $hour? It's quite likely that the first 2 entries in @logfiles are '.' (the current directory) and '..' (the directory above the current directory). You probably don't want to open either one of those. Have a look at the filenames you are reading from "/perl/ass/" in the order that they appear in the array @logfiles:
    use warnings; use strict; opendir(LOGS, "/perl/ass/") or die "Couldn't open /perl/ass/, $!"; my @logfiles = readdir(LOGS); print "File: $_\n" for @logfiles;
    Enabling warnings is useful, for example it would have pointed out your typo when you set $currentlog (use $logfiles[$hour] instead).

    Update: Oh, Win32. Your initial code threw me off because you used '/' in your file and directory paths. I don't do enough Win32 work to help - sorry.

      Yeah i've been useing the step through function to see whats in them, i start the $hour variable at 2 to avoid the leading ',' & '..' , $currentlog is the first subfolder in /ass/ "1-month-go"
Re: Perl Permissions
by Sidhekin (Priest) on Dec 03, 2006 at 13:35 UTC

    You are not checking whether the chmod succeeds. It could fail, f ex if you have neither ownership of the file nor root access (administrator rights, I presume).

    Which is likely your problem in the first place. In which case: Sorry, the owner or administrator will have to give you access.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: Perl Permissions
by eyepopslikeamosquito (Archbishop) on Dec 03, 2006 at 19:50 UTC
    As a matter of technique, you should always start your scripts with:
    use strict; use warnings;
    Doing that would have picked up that:
    my $currentlog = "/perl/ass/@logfiles[$hour]";
    is better written as:
    my $currentlog = "/perl/ass/$logfiles[$hour]";
    Though, admittedly, that won't solve your current problem.

    To solve your current problem, I suggest you download and install the sysinternals (now Microsoft) Handle utility which will allow you to see all processes on the system which have this file open.

Re: Perl Permissions
by roboticus (Chancellor) on Dec 03, 2006 at 14:19 UTC
    blundell:

    Another way to get this error message that I've occasionally seen is when the file in question is open with a lock by another program.

    --roboticus

Re: Perl Permissions
by ww (Archbishop) on Dec 03, 2006 at 22:48 UTC
    blundell wrote:
    chmod (0777, $currentlog); # << shouldnt this give me permission?!?!
    and
    I'm doing this on a windows platform by the way

    To the best of my knowledge (awaits chorus of "what ARE you talking about, if I'm wrong), Windows doesn't know diddly from chmod.

    (Nor did my cursory search of the perl docs reveal a chmod facility (UPDATE, clarity:) which applies to Windows.)
      FYI: http://perldoc.perl.org/functions/chmod.html,
      for Perl, although you may well be right about MS, certainly if filesys is VFAT/FAT32 ?
      NTFS may support something similar?
      Chris
Re: Perl Permissions
by bingos (Vicar) on Dec 04, 2006 at 09:01 UTC

    Hi. You are getting permission denied, because you are attempting to open a directory as a file

    Your readdir is pulling in all the entries in your /perl/ass folder including subdirectories. You then attempt to open one of those subdirectories as a file.

    You might want to use the -d operator to test if $currentlog is a directory or not before attempting to open it.

      I agree with bingos. Don't get thrown off by the error message by Windows, you'll indeed get a "Permission denied" error message if you try to pull one of the stunts that bingos said: trying to open a file as a directory, or vice versa. But it could have some other, but similarly esotheric cause. Just don't stare yourself blind on the word "permission".
        ah yeah makes sense now thanks :D , my mate said to say your the shiz!! lol
        I looked up your -d and also found the -T which i am useing as i am only dealing with text files,
        so i do this now to avoid directory's:
        if ( -T $currentlog ) #checks wether the file is a text f +ile { open(CURRENTLOG, $currentlog)|| die "file not found: $!"; } else . . .
        However i am now getting the error
        "Permission denied at tempCHRONO.pl line 76, <CURRENTLOG> line 1"
        on the code
        $oldlocation = "/perl/ass/$logfiles[$hour]"; #Cut and Pastes + the log file into corresponding folder $newlocation = "/perl/ass/$folder[$N]/$logfiles[$hour]"; print " old = $oldlocation \n new = $newlocation\n"; move($oldlocation, $newlocation) or die "File cannot be mo +ved: $!";
        I didnt really wanna post all my code as that is not me solving the error myself at all but if it helps :
        #!/usr/bin/perl use Date::EzDate; #Requests to use the +date mod use File::Copy; my $currentdate = Date::EzDate->new(); #Loads the current Da +te an Time # #Manageing Log Files Chronologically # my $hour = 2; until ($hour > 26) #Loo +p to read all log files { my $dateloaded = Date::EzDate->new(); #tak +es todays date opendir(LOGS, "/perl/ass/")|| die "dir not found"; #Ope +ns the folder containing the days log files my @logfiles = readdir(LOGS); #Put +s the name of all files into an array closedir(LOGS); my $currentlog = "/perl/ass/$logfiles[$hour]"; if ( -T $currentlog ) #checks wether the file is a tex +t file { open(CURRENTLOG, $currentlog)|| die "file not found: $!"; } else { $hour++; } $firstline = <CURRENTLOG>; $date = substr($firstline,0,2); #Ta +ke the day of month from the log $date2 = substr($firstline,3 ,2); #Ta +kes the month from the log $today = $currentdate->{'{day of month}'}; #To +days Date $month = $currentdate->{'month number'} +1; #To +days Month $dateloaded -= 7; #mi +nus a week $history = substr($dateloaded,8 ,2); #1- +week-ago $dateloaded -= 7; #fo +r the whole week $historyformer = substr($dateloaded,8 ,2); #En +d 1-week-ago $N = 0; until ( $N == 3) #Un +til all 3 weeks have been checked { @folder = qw( 1-week-ago 2-weeks-ago 3-week-ago ); if ( $date >= $historyformer && $date <= $history ) #if + the log equals greater than a week ago but less than 2 weeks ago { $month--; if ( $date2 == $month || $date2 == $month ) { $oldlocation = "/perl/ass/$logfiles[$hour]"; #Cu +t and Pastes the log file into corresponding folder $newlocation = "/perl/ass/$folder[$N]/$logfiles[$hour]"; print " old = $oldlocation \n new = $newlocation\n"; move($oldlocation, $newlocation) or die "File cannot be mo +ved: $!"; #DOESNT WORK $N = 3; $date = 999; #Ma +kes the date not equal to any other date $month++; } } $N++; $history = substr($dateloaded,8 ,2); $dateloaded -= 7; $historyformer = substr($dateloaded,8 ,2); #En +d 1-week-ago } $date = substr($firstline,3 ,2); #Co +llect the month from the log $N = 0; until ( $N == 6) #Un +til all 6 months have been checked { @folder = qw( 1-month-ago 2-months-ago 3-months-ago 4-months +-ago 5-months-ago 6-months-ago); $month--; #C +ounts down 6 months if ( $date == $month ) #i +f the first line of the log equals the date { $oldlocation = "/perl/ass/$logfiles[$hour]"; #C +ut and Pastes the log file into corresponding folder $newlocation = "/perl/ass/$folder[$N]/$logfiles[$hour]"; print " OLD - $oldlocation \n NEW - $newlocation"; move($oldlocation, $newlocation) or die "File cannot be mo +ved: $!"; #DOESNT WORK $N = 3; $date = 999; #M +akes the date not equal to any other date } $N++; } #if ($date == $month -1) # { # DELETE; # } $hour++; } exit;
        And no laughing because it isnt exactly finished :P
Re: Perl Permissions
by ff (Hermit) on Apr 05, 2009 at 19:39 UTC
    Is it possible that earlier in the code you did an 'open' on the file and now you are getting 'Permission denied' because you did not 'close' the file? I was getting that message for a 'rename' I was doing for lack of closing the file properly....