Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change permissions of a directory

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I'm trying to create directories and files and set permissions for those. This seems to work fine.

My question is if it is possible to return what permissions are set for the current directory and if it is possible to change those permissions if they are not set correctly?

Regards,

Ron
 
The perl "stat" function can give you what you want - I know it works for files, and I think it will work for directories as well. Do "perldoc -f stat" to read the documentation on the "stat" function.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thanks for the tip hmerrill. I will try that!

Another question. Is it normal that files that has been created dynamically in a folder that was created dynamically can't be deleted manually using a ftp client?

When I try to delete the file I get the message:

550 filename permission denied

the folder was created using:

$chmodfolder = 0777;
mkdir ($folder_name, $chmodfolder);

Is this normal?

Ron
 
It might depend on the userid/group you are running as under the web server and the userid/group you are running as under ftp. Check the owner/group attributes of the directory and file.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I'm not sure what you mean tsdragon, how can you set owner/group attributes in both cases?

About using 'stat' to find out permissions of a file or directory. I tried it and if I'm right the third element of the array 'stat' returns should give information about permissions. I tried it on a file that is chmodded '777'. The return value 'stat' gave on this file was something different. How should this be interpreted.

What I would like to achieve is check a folder for permissions. If perl can't write to it change the permissions. And after creating a file there change the permissions back to the original status.

Is this possible?

Ron
 
when you log on with ftp, you log on as a certain user, and the filesystem gives you access to things based on whether or not they have that user listed as having access to them (the permissions of the file), either because of the user's name, or one of the groups that user is included in. when you access a file through a web server, the webserver is generally running as user 'nobody', in group 'nobody', and nothing else. that means when you try to access a file (html file, cgi file, whatever), it only works if the type of access you're attempting is allowed for 'nobody', or, as is usually the general case, anyone.

for the question of why it's showing up differently with stat, i have two ideas. first, the third element is index [2], you may be accessing it wrong. second, the number is in octal, so the easiest way to view it would be a:
[tt]printf("%o", $info[2]);[/tt]
these aren't actually solutions per se, but double checking of a couple possible oversights. if these don't produce a proper answer, it would be worth it for you to post again.

as to your task, it is possible (of course, assuming perl is running as a user who has the ability to change the permissions of the directory in question). if you find that stat isn't working for you, you can always open up pipes to commandline calls, parse the returned text, and work with that instead. this should only be looked at as a final resort, as it takes more work than using the perl functions, but it will work if nothing else will.

HTH "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Thanks Stillflame,

Your tip for using stat helped I think.

When a folder is chmodded '(0)7777' stat returned a value of: 16895. Now it returns a value of '40777'. Any ideas why the 4 is included?

I'm still not sure what I should do to be able to delete folders or files through ftp when they are created by a script.

The weird thing is that it is possible to delete a folder that was created in an existing folder. But it is not possible to delete files and folders that were created in a folder that was also created by a script!

Ron
 
the higher order bits just describe different aspects of a file. the 4 in that spot means it is a directory. there are other possible values for that digit, i think a 1 means it's a normal file. anyway, if you want to get just the last 3 numbers of it, you can do this:[tt]
$num = $num & 0777;[/tt]

this is a logical AND, and it returns a number whose bits correspond only to those that the two numbers have in common. an example:[tt]
6 is 110 in binary,
3 is 011, so AND them together, you'll
have 010, or 2.[/tt]
this is usually used to separate out just the bits that you want to view/manipulate.

your general problem, however, doesn't seem as straigh-forward. it may be that the system isn't creating new files/folders with the default values you're expecting it to. you may try posting the code you have which creates the files and then the code that tries to delete them. other than that, i don't know. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Just a note, in case mkdir might be causing some confusion.
Be aware that the MODE used by mkdir can be modified if the permissions specified by the mkdir MODE are outside the current umask value. If you are doing a mkdir, you may not be getting the permissions you think you are asking for. The permissions are checked against the umask and tweaked before the mkdir is implemented.

HTH


keep the rudder amid ship and beware the odd typo
 
This is the code I am using to create the folders. I didn't try to delete the folders with code only manually by ftp.

$chmodFolder = 0777;
mkdir $iq->param('userDir'), $chmodFolder;

or

mkdir ($iq->param('userDir'), $chmodFolder);

Actually I used 'stat' to check the permission status of folders that are created using this code and the value returned is '40755' not '40777'!

goBoating, umask value? If this is relevant information that could solve the problem could you please explain what is meant with this?

Thanks Stillflame for the explaining of the returned value by 'stat'.

Ron
 
I can't tell for sure if the umask is tripping you, but, it sounds like a real possibility.

If umask is the problem....
If you are at a command prompt and do a 'mkdir' the umask controls the default permissions that are automatically used for that new dir. When I type umask at my command prompt, I get '02' back. That means that when I do a mkdir, the permission on the new dir will be restricted by that value. Instead of getting drwxrwxrwx, or 777, I get drwxrwxr_x, which is 02 less than 777, or 775. The Perl mkdir function does the same thing.

From THE camel book,

"(mkdir)...creates the dir... with permissions specified by the MODE (as modified by the current [red]umask[/red])."

So, even though you are doing ....
mkdir dir,0777;

You are probably ending up with something like 775 permissions.

Since you are creating the dir as the httpd daemon, only it or users in its group would be able to delete the dir. When you log in for ftp, you probably are NOT in the httpd group, therefore, you would not be able to delete/modify (write) that dir.

After you create the dir, you can immediately do a chmod, or, you can tweak your umask prior to doing the mkdir. I would argue against tweaking your umask. If you forget, and leave it wide open, you will be asking for trouble from the next hacker/cracker that comes surfing along. I lean toward a little paranoia there.

HTH


keep the rudder amid ship and beware the odd typo
 
This morning I tried to delete the folders using "rmdir" and this worked.

Thanks goBoating for the explanation of umask. I found something about it in a Perl guide but, like most of the time, a very short description. Your explanation makes it a bit clearer.

So probably the best way to deal with this is simply using 'rmdir'!

Now I only have to find the right syntax to delete the files that were created in these folders.

Ron
 
Sorry guys I'm still ,

I went through the docs on ww.perl.com but I couldn't find a function similar to 'rmdir' but then for files. I tried 'delete', 'del', 'slice' but obviously this is not working.

What is the correct syntax to delete a file.

Ron
 
with perl functions, it is 'unlink', followed by a filename.
if you just wanna use a system call in *nix, it is 'rm', followed by a filename.

HTH "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Thanks Stillflame. Why didn't I check the 'Unlink' function in the docs, I know I saw it?

Anyway, I tried it and everything seems to work now. Thanks for the help on this to all of you!

Ron
 
Ron,

Could I suggest that you use the rmdir rather than unlink to remove directories? Unlinking dirs is considered to be risky, so say the perl dox anyway. I've never had the nerve to test it out. If you decide to continue with unlink for dirs I think there's a command line option you have to set before it will work. Just use rmdir....
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Yes, thanks Mike. I use 'rmdir' to remove directories. But I don't think this works for individual files within a directory.

I read something similar about, like you wrote, using 'unlink' for directories. It doesn't say that it is risky for deleting files. Is it?

Ron
 
no, but make sure you close any open filehandles to the file before you unlink it. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top