#!/home/rebelsky/perl/bin/perl
#A set of subroutines that will store, read, and delete directories and 
#files in a database.
#(POD documentation at end)

##################
# Package Begins #
##################

#import module
use SimpleRPC;

###########################################################################
# writeFile #
###########################################################################
# Stores a string in a specified file in the database
# ARGUMENTS: The file address in string form and the string to be stored 
# in the file
# RETURNS: Returns 1 to say that it ran correctly.
###########################################################################
sub writeFile
  {
    #read in arguments
    my $file = shift;           #the file address
    my $data = shift;           #the data to be stored in the file
    #other variables
    local(*FILE);               #the file handle for the file
    
    #open the file, store the data, close the file, and change the 
    #permissions
    open(FILE,">" . $file) || die "cannot open file $file for writing: ($!)\n";
    print FILE $data;
    close(FILE) || die "cannot close file $file: ($!)\n";
    chmod(0770,$file) || die "cannot change permissions for file $file: ($!)\n";
    
    #return a true value to say that it ran correctly
    return 1;
  }                             #sub writeFile

###########################################################################
# appendFile #
###########################################################################
# Appends a string to a specified file in the database
# ARGUMENTS: The file's address in string form and the string to be
# appended to the file
# RETURNS: Returns 1 to say that it ran correctly
###########################################################################
sub appendFile
  {
    #read in arguments
    my $file = shift;           #the file address
    my $data = shift;           #the data to be appended in the file
    #other variables
    local(*FILE);               #the file handle for the file
    
    #open the file, append the data, close the file, and change the 
    #permissions
    open(FILE,">>" . $file) || die "cannot open file $file for appending: ($!)\n";
    print FILE $data;
    close(FILE) || die "cannot close file $file: ($!)\n";
    chmod(0770,$file) || die "cannot change permissions for file $file: ($!)\n";
    
    #return a true value to say that it ran correctly
    return 1;
  }                             #sub appendFile

###########################################################################
# readInFile #
###########################################################################
# Reads the data from a file in the database
# ARGUMENTS: The file address of the file to be read in string form.
# RETURNS: The contents of the file in string form.
###########################################################################
sub readInFile
  {
    #read in argument
    my $file = shift;           #the file address
    #other variables
    local(*FILE);               #the file handle for the file
    my @data;                   #the data from the file in array form

    #open the file, read the data, close the file, and change the 
    #permissions
    open(FILE,$file) || die "cannot open file $file for reading: ($!)\n";
    @data = <FILE>;
    close(FILE) || die "cannot close file $file: ($!)\n";
    
    #return the contents of the file
    return "@data";
  }                             #sub readFile

###########################################################################
# fileExists #
###########################################################################
# Detemines whether a file exists in the database
# ARGUMENTS: The file's address in string form.
# RETURNS: Returns 1 if the file exists and 0 if it does not.
###########################################################################
sub fileExists
  {
    #read in argument
    my $file = shift;           #the file address
    
    #return the result of the exist test
    return (-e $file);
  }                             #sub fileExists

###########################################################################
# delFile #
###########################################################################
# Deletes a specified file from the database.
# ARGUMENTS: The address of the file to be deleted.
# RETURNS: Returns 1 to show that it worked.
###########################################################################
sub delFile
  {
    #read in argument
    my $file = shift;           #the file address
    
    #delete the file
    unlink($file) || die "cannot delete file $file: ($!)\n";
    
    #return true to show that it worked
    return 1;
  }                             #sub delFile

###########################################################################
# makeDir #
###########################################################################
# Makes a directory in the database.
# ARGUMENTS: The directory to be made in string form
# RETURNS: Returns 1 to show that it worked correctly
###########################################################################
sub makeDir
  {
    #read in argument
    my $dir = shift;            #the directory that needs to be made
    
    #make the directory and change the permissions
    mkdir($dir,0770) || die "cannot make directory $dir: ($!)\n";
    chmod(0770,$dir) || die "cannot change permissions for directory $dir: ($!)\n";
    
    #return true to say all ran well
    return 1;
  }                             #sub makeDir

###########################################################################
# removeDir #
###########################################################################
# Deletes a directory from the database.
# ARGUMENTS: The directory to be deleted in string form.
# RETURNS: Returns 1 to show that it worked correctly.
###########################################################################
sub removeDir
  {
    #read in argument
    my $dir = shift;            #the directory that needs to be removed
    
    #delete the directory
    rmdir($dir) || die "cannot delete the directory $dir:  ($!)\n";
    
    #return true to say all ran well
    return 1;
  }                             #sub removeDir

###########################################################################
# existsAndStore #
###########################################################################
# Sees if a file exists and if it doesn't it stores specified string in it
# ARGUMENTS: The file address to check in string form and the data to put 
# in that file in string form.
# RETURNS: Returns 1 if the file exists and 0 if it did not and the data 
# was stored.
###########################################################################
sub existsAndStore
  {
    #read in arguments
    my $file = shift;           #the file address 
    my $data = shift;           #the data to store
    #other variables
    local(*FILE);               #file handle for file to be written to
    
    #see if it already exists
    if(-e $file)
      {
        #say that the file exists
        return 1;
      }                         #if(fileExists($file))
    else
      {
        #store the data and say that the file didn't exist and everything 
        #is done now.
        open(FILE,">" . $file) || die "cannot open file $file for writing: ($!)\n";
        print FILE $data;
        close(FILE) || die "cannot close file $file: ($!)\n";
        chmod(0770,$file) || die "cannot change permissions for file $file: ($!)\n";
        return 0;
      }                         #else
  }                             #sub existsAndStore
    
#run the server
RPCServerLoop(7707);

#####################
# Pod Documentation #
#####################
=pod

=head1 ID

=over 4

=item Package

DataBase.pm

=item Author

Rachel Heck

=item Description

A set of subroutines that will store, read, and delete directories and 
files in a database.

=back

=head1 Subroutines

=over 4

=item writeFile

Stores a string in a specified file in the database.

=item appendFile

Appends a string to a specified file in the database.

=item readInFile

Reads the data from a file in the database.

=item fileExists

Detemines whether a file exists in the database.

=item delFile

Deletes a specified file from the database.

=item makeDir

Makes a directory in the database.

=item removeDir

Deletes a directory from the database.

=item existsAndStore

Sees if a file exists and if it doesn't it stores specified string in it

=back

=head1 History

=over 4

=item [9 July 1999]

writeFile, appendFile, readInFile, fileExists, delFile, makeDir, and  
removeDir are working without networking and are fully commented.

=item [13 July 1999]

The database is now an annotation server and readInFile returns a string 
instead of an array.

=item [15 July 1999]

Created existsAndStore.  It works and is completely commented.

=item [14 September 1999]

Files creates through the database no longer have permissions 777.  Now they 
are 770.  This is much more safe.

=back

=cut

