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

foxr lon-capa-cvs@mail.lon-capa.org
Mon, 15 Sep 2003 10:03:52 -0000


foxr		Mon Sep 15 06:03:52 2003 EDT

  Modified files:              
    /loncom	lond 
  Log:
  Completed and tested code for pushfile.
  
  
Index: loncom/lond
diff -u loncom/lond:1.142 loncom/lond:1.143
--- loncom/lond:1.142	Tue Sep  9 16:47:46 2003
+++ loncom/lond	Mon Sep 15 06:03:52 2003
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.142 2003/09/09 20:47:46 www Exp $
+# $Id: lond,v 1.143 2003/09/15 10:03:52 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -60,6 +60,9 @@
 # 09/08/2003 Ron Fox:  Told lond to take care of change logging so we
 #      don't have to remember it:
 # $Log: lond,v $
+# Revision 1.143  2003/09/15 10:03:52  foxr
+# Completed and tested code for pushfile.
+#
 # Revision 1.142  2003/09/09 20:47:46  www
 # Permanently store chatroom entries in chatroom.log
 #
@@ -87,13 +90,14 @@
 use Authen::Krb5;
 use lib '/home/httpd/lib/perl/';
 use localauth;
+use File::Copy;
 
 my $DEBUG = 0;		       # Non zero to enable debug log entries.
 
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.142 $'; #' stupid emacs
+my $VERSION='$Revision: 1.143 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid;
 my $currentdomainid;
@@ -186,6 +190,97 @@
     }
 }
 #
+#  CopyFile:  Called as part of the process of installing a 
+#             new configuration file.  This function copies an existing
+#             file to a backup file.
+# Parameters:
+#     oldfile  - Name of the file to backup.
+#     newfile  - Name of the backup file.
+# Return:
+#     0   - Failure (errno has failure reason).
+#     1   - Success.
+#
+sub CopyFile {
+    my $oldfile = shift;
+    my $newfile = shift;
+
+    #  The file must exist:
+
+    if(-e $oldfile) {
+
+	 # Read the old file.
+
+	my $oldfh = IO::File->new("< $oldfile");
+	if(!$oldfh) {
+	    return 0;
+	}
+	my @contents = <$oldfh>;  # Suck in the entire file.
+
+	# write the backup file:
+
+	my $newfh = IO::File->new("> $newfile");
+	if(!(defined $newfh)){
+	    return 0;
+	}
+	my $lines = scalar @contents;
+	for (my $i =0; $i < $lines; $i++) {
+	    print $newfh ($contents[$i]);
+	}
+
+	$oldfh->close;
+	$newfh->close;
+
+	chmod(0660, $newfile);
+
+	return 1;
+	    
+    } else {
+	return 0;
+    }
+}
+
+#
+#   InstallFile: Called to install an administrative file:
+#       - The file is created with <name>.tmp
+#       - The <name>.tmp file is then mv'd to <name>
+#   This lugubrious procedure is done to ensure that we are never without
+#   a valid, even if dated, version of the file regardless of who crashes
+#   and when the crash occurs.
+#
+#  Parameters:
+#       Name of the file
+#       File Contents.
+#  Return:
+#      nonzero - success.
+#      0       - failure and $! has an errno.
+#
+sub InstallFile {
+    my $Filename = shift;
+    my $Contents = shift;
+    my $TempFile = $Filename.".tmp";
+
+    #  Open the file for write:
+
+    my $fh = IO::File->new("> $TempFile"); # Write to temp.
+    if(!(defined $fh)) {
+	&logthis('<font color="red"> Unable to create '.$TempFile."</font>");
+	return 0;
+    }
+    #  write the contents of the file:
+
+    print $fh ($Contents); 
+    $fh->close;			# In case we ever have a filesystem w. locking
+
+    chmod(0660, $TempFile);
+
+    # Now we can move install the file in position.
+    
+    move($TempFile, $Filename);
+
+    return 1;
+}
+
+#
 #   PushFile:  Called to do an administrative push of a file.
 #              - Ensure the file being pushed is one we support.
 #              - Backup the old file to <filename.saved>
@@ -229,13 +324,26 @@
     #
     my $backupfile = $tablefile;
     $backupfile    =~ s/\.tab$/.old/;
-    # CopyFile($tablefile, $backupfile);
+    if(!CopyFile($tablefile, $backupfile)) {
+	&logthis('<font color="green"> CopyFile from '.$tablefile." to ".$backupfile." failed </font>");
+	return "error:$!";
+    }
     &logthis('<font color="green"> Pushfile: backed up '
 	    .$tablefile." to $backupfile</font>");
     
     #  Install the new file:
 
-    # InstallFile($tablefile, $contents);
+    if(!InstallFile($tablefile, $contents)) {
+	&logthis('<font color="red"> Pushfile: unable to install '
+		 .$tablefile." $! </font>");
+	return "error:$!";
+    }
+    else {
+	&logthis('<font color="green"> Installed new '.$tablefile
+		 ."</font>");
+
+    }
+
 
     #  Indicate success: