Gossamer Forum
Home : General : Perl Programming :

Directory Permissions

Quote Reply
Directory Permissions
Why would mkdir with a directory permission of 0777 not work on some systems?Crazy


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Directory Permissions In reply to
cause mkdir is an UNIX command that only works in UNIX or Linux operating systems. You can not use commands like this in operating systems like Windows.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] Directory Permissions In reply to
ahhh.... never thought of that. Thanks StealthWink

Edit: I guess I have to figure out some code to create a directory depending on which operating system is in use.


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 4, 2002, 8:19 PM
Quote Reply
Re: [Ian] Directory Permissions In reply to
It's already done for you. I can't stress how much CPAN really every programmer's friend! =)

Try the module File::Path, here:

http://search.cpan.org/...query=File%3A%3APath

- wil
Quote Reply
Re: [Ian] Directory Permissions In reply to
How do you mean?

Do you mean that it won't create the directory if the directory the script is being executed from is not 777, or that the directory created is not chomdded?
Quote Reply
Re: [Paul] Directory Permissions In reply to
Thanks Wil and Paul.

As I scatch my head, I am now wondering if the problem is more a path issue in Links. (incorrect path settings).

But for the sake of the discussion, what is wrong with this code that it may not work on someone system (other than incorrect path settings):

Code:
if (-d "$CFG->{build_root_path}/editors/profiles") {
print "<br>Directory found<br>";
}
else {
print "<br>Path Editors/Profiles not found. Creating Directory.<br>";
mkdir("$CFG->{build_root_path}/editors/profiles", 0777) || die "Cannot mkdir newdir: $!";
}
I hope this clears up my questionAngelic.


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 5, 2002, 9:33 AM
Quote Reply
Re: [Ian] Directory Permissions In reply to
Portability. That's one area where your code is lacking.

Look into the module File::Path which addresses these issues.

- wil
Quote Reply
Re: [Wil] Directory Permissions In reply to
I am absolutely sure you are right Wil. I have breifly looked at the CPAN reference you gave.. thanks for that. I think you are Goliath and I am David when it comes to Perl. Baby steps for me... so let me stu on this.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Wil] Directory Permissions In reply to
For making directories mkdir works perfectly well (surprisingly), you would only need a module for more complexed tasks otherwise the overhead of loading the module far outweights the advantages.

Last edited by:

Paul: Jun 5, 2002, 10:35 AM
Quote Reply
Re: [Paul] Directory Permissions In reply to
Ivan pointed me to the nph-build.cgi where alex does his directory making.... I will study this also.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Directory Permissions In reply to
Hi,

The perl function mkdir works on both windows and unix. The system command mkdir works on both systems, but does not take a second argument. =)

The second argument to the perl function mkdir is a umask modifier, and not the permissions of the directory necessarily. You should do:

mkdir ($dir, 0777) or die "mkdir: $dir ($!)";
chmod(0777, $dir) or die "chmod: $dir ($!)";

to properly create a directory and chmod it 777.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Ian] Directory Permissions In reply to
Yeah you can do recursive directory making without a third party module if you are up to the task.

Code:
sub recursive_mkdir {
#-----------------------------------------------------
# =)

my $path = shift;
my @dirs = split "/" => $path;

foreach my $dir (@dirs) {
$tmp .= "$dir/";
unless ( -e $tmp and -d _ ) {
mkdir($tmp) || die "Cannot make $tmp: $!";
}
next;
}
}

$path would be something like /foo/bar/doo/dar

Thats off the top of my head.
Quote Reply
Re: [Paul] Directory Permissions In reply to
Thanks Alex and Paul.

Paul: I guess, going off of what Alex just mention that I should add the chmod part to the sub as well, for all to work properly? (as well as the permissions 777)


Edit:

Paul or Alex, would you mind looking at the part of my plug-in which is supposed to do this, as it is causing errors on someone else's system and not mineUnsure. I have change the code to do this 4 times now, and right now I have part of Alex's sub _build_dir , which did not seem to work either.... I just want to isolate the problem, so I know if it is my code, or something else....


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 5, 2002, 11:00 AM
Quote Reply
Re: [Ian] Directory Permissions In reply to
Code:
sub recursive_mkdir {
#-----------------------------------------------------
# =)

my $path = shift;
my @dirs = split "/" => $path;

foreach my $dir (@dirs) {
$tmp .= "$dir/";
unless ( -e $tmp and -d _ ) {
mkdir($tmp) or die "Cannot make $tmp: $!";
chmod(0777, $tmp) or die "Can't chmod $tmp: $!";
}
next;
}
}

That may not be perfect - I wrote it whilst listening to a cool song so wasn't really paying full attention :)

Last edited by:

Paul: Jun 5, 2002, 11:02 AM
Quote Reply
Re: [Paul] Directory Permissions In reply to
Paul this looks good. I will put it in... but I cannot test on my system for some reason, everything works on this end (which is a real change for meWink). Just when someone else goes to use it, errors creep in.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Directory Permissions In reply to
Unless you need recursive mkdir, just use mkdir(). Otherwise, you may end up creating a big string of directories you didn't want to create.

Also, Paul, I think in your example you need to start $tmp with a leading slash, otherwise you'll end up creating the entire dir from cwd.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Directory Permissions In reply to
It seemed to work ok when I tested.


Code:
#!/perl/bin/perl

use CGI::Carp qw/fatalsToBrowser/;

print "Content-type: text/html\n\n";

recursive_mkdir('/apache/htdocs/a/b/c');

sub recursive_mkdir {
#-----------------------------------------------------
#
my $path = shift;
my @dirs = split "/" => $path;

foreach my $dir (@dirs) {
$tmp .= "$dir/";
unless ( -e $tmp and -d _ ) {
mkdir($tmp) or die "Cannot make $tmp: $!";
chmod(0777, $tmp) or die "Can't chmod $tmp: $!";
}
next;
}
}

The script is in the cgi-bin and the directories a/b/c were created properly in /apache/htdocs
Quote Reply
Re: [Paul] Directory Permissions In reply to
Oops, your right. Missed the fact that you'll have an empty $dir to start with from split. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Directory Permissions In reply to
Alex, how does this look?

I want to make sure that /editors/profiles exists before I continue with a part of my code:

The user may not have /editors or /editors/profiles.....



#Generate the /editors/profiles directory if it does not already exists
#-------
my $path = "/editors/profiles";
&build_directories ($path);


then further down:

sub build_directories {
#make sure directory exists, including parent directories, if not then create them

my $path = shift;
my $dir = "$CFG->{build_root_path}/$path";

unless (-e "$dir") {
mkdir ($dir, 0777) or die "mkdir: $dir ($!)";
chmod(0777, $dir) or die "chmod: $dir ($!)";
}
}



This is getting complex, to just make sure some directories exist before writing files to themUnsure


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Directory Permissions In reply to
Hi,

Keep it simple, add a function:

sub my_mkdir {
my ($dir, $perms) = @_;
return if -d $dir;
mkdir($dir, $perms) or die "mkdir: $dir ($!)";
chmod($perms, $dir) or die "chmod: $dir ($!)";
}

Then call:

my_mkdir("$CFG->{build_root_path}/editors", 0777);
my_mkdir("$CFG->{build_root_path}/editors/profiles", 0777);

to properly create and chmod the dirs. (Style tip: avoid &function, use function() instead, I know I did this in DBMan and Links, but I've since learned better). ;)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Directory Permissions In reply to
Thank you very much for the help Alex! Style tip taken on board tooSmile. I will try this now.


http://www.iuni.com/...tware/web/index.html
Links Plugins