@@ -1,14 +1,18 @@
#!/usr/bin/env python
import os
import io
import pickle
import pickle
# Globals
class glb :
fsname = None
curr_dir = '/' # Tracking variable for current directory
files = {} # --Key : absolute file path, item : pyfile
unwritten = {} # Hash for files not in directory, but opened with 'w'
resume = 1 #variable used to keep track of process in resume/suspend
resume = 1 #variable used to keep track of process in resume/suspend
written_data = []
native_size = 0
@@ -24,11 +28,33 @@ def generate_filepath(filename):
dict_filename = glb .curr_dir + '/' + filename
return dict_filename
def check_status ():
if glb .resume == 0 :
raise ValueError ( "Process suspended cannot execute" )
#method for saving to native file
def dump_data ():
dataFileName = '%s.fssave' % glb .fsname
print 'File that is saving will be named %s' % dataFileName
#create File I/O object
fo = io .FileIO (dataFileName , "wb" )
glb .files ['glb' ]= glb
pickle .dump (glb .files , fo , protocol = pickle .HIGHEST_PROTOCOL )
fo .close ()
def clear_written_data (filename ):
filename = glb .files [filename ]
for i in range (filename .native_index ,filename .native_index + filename .maxsize ):
glb .written_data [i ] = 0
###########################################################################
### fs module functions
@@ -43,11 +69,20 @@ def init(fsname):
# files = pickle.load(handle)
if os .path .isfile (fsname ):
file = open (fsname , 'w' )
file .seek (0 )
file .truncate ()
file .close ()
file1 = io . FileIO (fsname , 'w' )
file1 .seek (0 )
file1 .truncate ()
file1 .close ()
# Initalize a root directory
glb .native_size = os .path .getsize (dataFileName )
print glb .native_size
for i in range (glb .native_size ):
glb .written_data .append (0 )
glb .files ['/' ] = pyfile ('/' , 0 , True )
print "--[INFO] FileSystem with name %s has been created." % fsname
@@ -63,6 +98,8 @@ def create(filename, nbytes):
% (filename , nbytes , glb .curr_dir )
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except IndexError :
print '--[ERROR] Native File is full'
except :
print '[ERROR] File note created, failure to allocate space'
@@ -81,6 +118,8 @@ def mkdir(dirname):
print '[ERROR] Failed to make directory'
def chdir (dirname ):
try :
check_status ()
@@ -100,6 +139,7 @@ def chdir(dirname):
print 'Error: Directory not found'
def open (filename , mode ):
try :
check_status ()
@@ -119,14 +159,18 @@ def open(filename, mode):
glb .unwritten [dict_filepath ].open ('w' )
print '--[INFO] Opening file which is not in directory'
return
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
print '--[ERROR] File not in directory'
def write (fd , writebuf ):
try :
check_status ()
dict_filepath = generate_filepath (fd )
if dict_filepath in glb .files :
@@ -136,6 +180,8 @@ def write(fd, writebuf):
glb .unwritten [dict_filepath ].write (writebuf )
print '--[INFO] Written to file %s (file not in filesystem)' % fd
# dump_data()
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
@@ -163,42 +209,34 @@ def close(fd):
def close (fd ):
try :
dict_filepath = generate_filepath (fd )
if dict_filepath in glb .files :
glb .files [dict_filepath ].close ()
print '--[INFO] Closed file %s located in director %s' \
% (fd , glb .curr_dir )
else : # Unwritten file
glb .files [dict_filepath ] = glb .unwritten [dict_filepath ]
glb .unwritten .pop (dict_filepath )
except :
print '--[ERROR] File not in directory'
def read (fd , nbytes ):
try :
dict_filepath = generate_filepath (fd )
glb .files [dict_filepath ].read (nbytes )
check_status ()
dict_filepath = generate_filepath (fd )
glb .files [dict_filepath ].read (nbytes )
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
print '--[ERROR] Issue reading from file'
def readlines (fd ):
try :
check_status ()
dict_filepath = generate_filepath (fd )
glb .files [dict_filepath ].readlines ()
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
print '--[ERROR] File not in directory'
@@ -209,6 +247,7 @@ def length(fd):
dict_filepath = generate_filepath (fd )
length = glb .files [dict_filepath ].length ()
print 'Length of %s : %d' % (fd , length )
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
@@ -221,6 +260,7 @@ def pos(fd):
check_status ()
dict_filepath = generate_filepath (fd )
pos = glb .files [dict_filepath ].pos ()
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
@@ -233,32 +273,51 @@ def seek(fd, pos):
check_status ()
dict_filepath = generate_filepath (fd )
glb .files [dict_filepath ].seek (pos )
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
print '--[ERROR] File not in directory'
def delfile (filename ):
try :
check_status ()
dict_filepath = generate_filepath (filename )
#print dict_filepath
clear_written_data (dict_filepath )
print glb .written_data
for key , value in glb .files .iteritems ():
print key , value
print "Printed Dictionary"
glb .files [glb .curr_dir ].del_indir (filename )
glb .files .pop (dict_filepath )
glb .files .pop (str (dict_filepath ))
print '--[INFO] Deleting file %s' % filename
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
print '--[ERROR] File not in directory'
def deldir (dirname ):
try :
check_status ()
path = generate_filepath (dirname )
if path in glb .files :
# Couldn't figure out how to manually remove keys from pyfile for
# directories without breaking everything
# directories without breaking everyfuckingthing
if glb .files [path ].is_empty ():
glb .files .pop (path )
return
@@ -271,16 +330,20 @@ def deldir(dirname):
deldir (f ) # Recursion into directory
else :
# if regular file
clear_written_data (path )
glb .files .pop (path ) # Pop regular file in directory
chdir ('..' )
path = generate_filepath (dirname )
glb .files .pop (path )
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
print '--[ERROR] File not in directory'
def isdir (filename ):
try :
check_status ()
@@ -294,12 +357,12 @@ def isdir(filename):
print '%s is a directory' % filename
else :
print '%s is not a directory' % filename
except ValueError :
print '--[ERROR] Process suspended cannot execute'
except :
print '--[ERROR] File not in directory'
def listdir (filename ):
try :
check_status ()
@@ -312,6 +375,7 @@ def listdir(filename):
glb .files [glb .curr_dir ].contents .remove (f )
print glb .files [glb .curr_dir ].contents
return
path = generate_filepath (filename )
chdir (filename )
for f in glb .files [path ].contents :
@@ -324,31 +388,57 @@ def listdir(filename):
print '--[ERROR] File not in directory'
def suspend ():
# check to see if any file is open for writing---can't suspend
for fd in glb .files :
# print fd
if glb .files [fd ].isdir == False :
if glb .files [fd ].isopen == True and glb .files [fd ].mode == 'w' :
print '--[ERROR] cannot suspend there is a file open for writing'
return
return 0
print '--[INFO] Suspending filesystem'
glb .resume = 0 # suspending all process
dataFileName = '%s.fssave' % glb .fsname
print 'File that is saving will be named %s' % dataFileName
#create File I/O object
fo = io .FileIO (dataFileName , "wb" )
pickle .dump (glb .files , fo , protocol = pickle .HIGHEST_PROTOCOL )
fo .close ()
dump_data ()
def resume (filename ):
filename = '%s.fssave' % filename
print '--[INFO] Resuming filesystem ' + filename
glb .resume = 1
#dataFileName = '%s.fssave' % glb.fsname
fo = io .FileIO (filename , "rb" )
#load saved file into files
glb .files = pickle .load (fo )
temp = {}
temp = pickle .load (fo )
glb_temp = temp ['glb' ]
print glb_temp .fsname
print glb_temp .curr_dir
print glb_temp .native_size
glb .fsname = glb_temp .fsname
glb .curr_dir = glb_temp .curr_dir # Tracking variable for current directory
glb .unwritten = glb_temp .unwritten # Hash for files not in directory, but opened with 'w'
glb .written_data = glb_temp .written_data
glb .native_size = glb_temp .native_size
glb .files = dict (temp )
glb .files .pop ('glb' )
return
############################################
@@ -364,18 +454,47 @@ def print_keys():
### PYFILE class
###########################################################################
class pyfile :
"""Base class for all files stored in the file system"""
'Base class for all files stored in the file system'
def __init__ (self , path , maxsize , isdir ):
self .path = path
self .isdir = isdir
self .contents = []
self .size = 0
# If not isdir, than is file, so initialze file variables.
if not isdir :
self .maxsize = maxsize
self .position = 0
self .isopen = False
self .mode = 'closed'
self .native_index = self .calc_native_index ()
if self .native_index == - 1 :
raise IndexError ()
# parse out name based on end of path #
###########################################################################
### Native File
###########################################################################
def calc_native_index (self ):
freedata = 0
for i in range (glb .native_size ):
if glb .written_data [i ] == 0 :
freedata = freedata + 1
else :
freedata = 0
if freedata == self .maxsize :
self .native_index = (i - self .maxsize )+ 1
for j in range (self .native_index ,i + 1 ):
glb .written_data [j ] = 1
print glb .written_data
return self .native_index
return - 1
###########################################################################
@@ -502,7 +621,6 @@ def read(self, nbytes):
else :
print '--[ERROR] File is not opened to read'
def readlines (self ):
if self .isopen and self .mode == 'r' :
str_list = []
@@ -531,13 +649,6 @@ def add_file(self, filename):
print "Error Making Directory"
def pos (self ):
if self .isdir :
print '--[ERROR] Attempting to find position of director'
else :
return self .position
def in_dir (self , filename ):
if self .isdir and filename in self .contents :
return True