[LON-CAPA-cvs] cvs: loncom / lond

albertel lon-capa-cvs@mail.lon-capa.org
Tue, 26 Oct 2004 14:55:50 -0000


albertel		Tue Oct 26 10:55:50 2004 EDT

  Modified files:              
    /loncom	lond 
  Log:
  - refactoring out the directory creation routines
  
  
Index: loncom/lond
diff -u loncom/lond:1.263 loncom/lond:1.264
--- loncom/lond:1.263	Thu Oct 21 12:05:50 2004
+++ loncom/lond	Tue Oct 26 10:55:49 2004
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.263 2004/10/21 16:05:50 albertel Exp $
+# $Id: lond,v 1.264 2004/10/26 14:55:49 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -57,7 +57,7 @@
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.263 $'; #' stupid emacs
+my $VERSION='$Revision: 1.264 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -1725,18 +1725,10 @@
 	if (-e $passfilename) {
 	    &Failure( $client, "already_exists\n", $userinput);
 	} else {
-	    my @fpparts=split(/\//,$passfilename);
-	    my $fpnow=$fpparts[0].'/'.$fpparts[1].'/'.$fpparts[2];
 	    my $fperror='';
-	    for (my $i=3;$i<= ($#fpparts-1);$i++) {
-		$fpnow.='/'.$fpparts[$i]; 
-		unless (-e $fpnow) {
-		    &logthis("mkdir $fpnow");
-		    unless (mkdir($fpnow,0777)) {
-			$fperror="error: ".($!+0)." mkdir failed while attempting "
-			    ."makeuser";
-		    }
-		}
+	    if (!&mkpath($passfilename)) {
+		$fperror="error: ".($!+0)." mkdir failed while attempting "
+		    ."makeuser";
 	    }
 	    unless ($fperror) {
 		my $result=&make_passwd_file($uname, $umode,$npass, $passfilename);
@@ -1967,21 +1959,10 @@
 	# Note that any regular files in the way of this path are
 	# wiped out to deal with some earlier folly of mine.
 
-	my $path = $udir;
-	if ($ufile =~m|(.+)/([^/]+)$|) {
-	    my @parts=split('/',$1);
-	    foreach my $part (@parts) {
-		$path .= '/'.$part;
-		if( -f $path) {
-		    unlink($path);
-		}
-		if ((-e $path)!=1) {
-		    mkdir($path,0770);
-		}
-	    }
+	if (!&mkpath($udir.'/')) {
+	    &Failure($client, "unable_to_create\n", $userinput);	    
 	}
 
-
 	my $destname=$udir.'/'.$ufile;
 	my $transname=$udir.'/'.$ufile.'.in.transit';
 	my $remoteurl='http://'.$clientip.'/userfiles/'.$fname;
@@ -2090,24 +2071,11 @@
     } else {
 	my $udir = &propath($udom,$uname);
 	if (-e $udir) {
-	    my $newdir=$udir.'/userfiles/'.$ufile;
-	    if (!-e $newdir) {
-		my @parts=split('/',$newdir);
-		my $path;
-		foreach my $part (@parts) {
-		    $path .= '/'.$part;
-		    if (!-e $path) {
-			mkdir($path,0770);
-		    }
-		}
-		if (!-e $newdir) {
-		    &Failure($client, "failed\n", "$cmd:$tail");
-		} else {
-		    &Reply($client, "ok\n", "$cmd:$tail");
-		}
-	    } else {
-		&Failure($client, "not_found\n", "$cmd:$tail");
+	    my $newdir=$udir.'/userfiles/'.$ufile.'/';
+	    if (!&mkpath($newdir)) {
+		&Failure($client, "failed\n", "$cmd:$tail");
 	    }
+	    &Reply($client, "ok\n", "$cmd:$tail");
 	} else {
 	    &Failure($client, "not_home\n", "$cmd:$tail");
 	}
@@ -3828,11 +3796,23 @@
 &register_handler("autoinstcodeformat", \&get_institutional_code_format_handler,
 		  0,1,0);
 
-#
-#
-#
-#
-#
+# mkpath makes all directories for a file, expects an absolute path with a
+# file or a trailing / if just a dir is passed
+# returns 1 on success 0 on failure
+sub mkpath {
+    my ($file)=@_;
+    my @parts=split(/\//,$file,-1);
+    my $now=$parts[0].'/'.$parts[1].'/'.$parts[2];
+    for (my $i=3;$i<= ($#parts-1);$i++) {
+	$now.='/'.$parts[$i];
+	if (!-e $now) {
+	    if  (!mkdir($now,0770)) { return 0; }
+	}
+    }
+    return 1;
+}
+
+
 #---------------------------------------------------------------
 #
 #   Getting, decoding and dispatching requests:
@@ -4887,12 +4867,7 @@
 #
 sub password_path {
     my ($domain, $user) = @_;
-
-
-    my $path   = &propath($domain, $user);
-    $path  .= "/passwd";
-
-    return $path;
+    return &propath($domain, $user).'/passwd';
 }
 
 #   Password Filename