yaffs Made some changes to yaffs_importer.py
[yaffs2.git] / direct / python / yaffs_importer.py
index e3ab5fa7f44978782be2eedf9b1b86381021abea..b0b11955cd2651256222d8b9cd35ef2117fe8a57 100644 (file)
@@ -4,11 +4,11 @@ import sys
 import ctypes
 
 
-dir_in_snapshot=[]
-files_in_snapshot=[]
-symlinks_in_snapshot=[]
-unknown_in_snapshot=[]
-is_mount_in_snapshot=[]
+#dir_in_snapshot=[]
+#files_in_snapshot=[]
+#symlinks_in_snapshot=[]
+#unknown_in_snapshot=[]
+#is_mount_in_snapshot=[]
 def check_for_yaffs_errors(output):
     if output<0:
         ##error has happened
@@ -22,6 +22,8 @@ def debug_message(message, debug_level):
     """level 0 error messages"""
     """level 1 basic tasks are shown(creating, deleating,ect)"""
     """level 2 all process are shown"""
+    """level 3 shows minor tasks such as join_paths, ect"""
+    """level 4 is used for bug hunting and shows each step in detail"""
     if current_debug_level>=debug_level:
         print message
         
@@ -34,37 +36,50 @@ def join_paths(path1, path2):
         new_path+=path2
     else:
         new_path+=path2
+    
+    debug_message(("adding path ", path1, " to ",  path2, " resulting path: ", new_path), 3)
     return new_path
     
 def subtract_paths(path1, path2):
     if len(path1)>len(path2):
+        debug_message("the two path1 is longer than path2 and can therefore be subtracted.", 4)
         ##if the two paths are diretly subtractable
         if path1[0:len (path2)-1]==path2:
+            debug_message("the two paths are direcly subtractable", 4)
             new_path=path1[len(path2):]
         elif path1[1:len (path2)-1]==path2:
+            debug_message("the path1 has one more charecter at the begining. assuming that the first chareter is a slash", 4)##fix this assumption.
             new_path=path1[len(path2)+1:]
         elif path1[1:len (path2)]==path2[1:]:
-            new_path=path1[len(path2)-1:]
+            debug_message("the path2 has one more charecter at the begining. assuming that the first chareter is a slash", 4)##fix this assumption.
+
+            new_path=path1[len(path2)+1:]
         else :
             debug_message("error:could not subtract paths", 0)
             debug_message( ("paths do not match:"+ path1+ "  "+path2), 0)
             return 0
-        return new_path
     else :
         debug_message( ("cannot subtract path2(:", path2, ") from path1(", path1, ")because path 2 is too long"), 0)
+        
         return 0
+    debug_message(("subtracting paths ", path2, " from ",  path1, " resulting path: ", new_path), 3)
+    return new_path
     
 def create_file(file):
     debug_message( "\n \n \n", 2)
-    file_path=yaffs_root_dir_path+file["path"][len(path):]
+    file_path= join_paths(yaffs_root_dir_path, file["path"][len(path):])
     debug_message( ("creating file:", file_path), 2)
     debug_message (("mode", file["mode"]), 2)
     debug_message("opening file",2)
 #    yaffs_ls(file["path"])
-    output=yaffs_unlink(path)
-    print"unlinking", output
-    check_for_yaffs_errors(output)
+
+    ##if there is already a file in yaffs then remove the file . this is to prevent yaffs from opening a nd writing to a read only file
+    if yaffs_access(file_path, 0)==0:##the 0 means does it exist. 
+        debug_message ("file already exists in yaffs", 2)
+        output=yaffs_unlink(file_path)
+        debug_message(("unlinking", file_path, output), 2)
+        check_for_yaffs_errors(output)
+    
     current_handle=yaffs_open(file_path, yaffs_O_CREAT | yaffs_O_TRUNC| yaffs_O_RDWR, yaffs_S_IREAD | yaffs_S_IWRITE)  ##opens a file with mode set to write
     debug_message(("current_handle", current_handle), 2)
     data_file=open(file["path"], "r")
@@ -76,8 +91,6 @@ def create_file(file):
     data_to_be_written= data_file.read()
     
     
-#    debug_message( ("data to be saved########################################################################/n"+ data_to_be_written), 2)
-#    debug_message("###########################################################################################",2)
     length_of_file=len(data_to_be_written)
     debug_message (("length of data to be written",length_of_file), 2) 
     output=yaffs_write(current_handle,data_to_be_written , length_of_file)
@@ -106,6 +119,32 @@ def create_file(file):
         debug_message(( "error chmoding file:", output), 0)
         check_for_yaffs_errors(output)
 
+
+
+def create_dir(dir, scanned_path, yaffs_path):
+    debug_message( "\n \n \n", 2)
+    absolute_dir_path=join_paths(yaffs_path, subtract_paths(dir["path"],scanned_path)) 
+    debug_message( ("creating dir:", absolute_dir_path), 2)
+    debug_message (("mode", dir["mode"]), 2)
+
+
+    ##if there is already a dir in yaffs then remove the dir . this is to clean the yaffs folder if it already exists.
+    if yaffs_access(absolute_dir_path, 0)==0:##the 0 means does it exist. 
+        debug_message ("folder already exists in yaffs", 2)
+        output=yaffs_unlink(absolute_dir_path)
+        debug_message(("unlinking", absolute_dir_path, output), 2)
+        check_for_yaffs_errors(output)
+    
+    output=yaffs_mkdir(absolute_dir_path, dir["mode"] )
+    if output>=0:
+        debug_message(( "creating dir:", output), 2)
+    else :
+        debug_message(( "error creating dir:", output), 0)
+        check_for_yaffs_errors(output)
+    
+    
+
+
 def remove_file_from_path(path):
     slash_id=[]
     for i in range(0, len(path)):
@@ -125,13 +164,14 @@ def is_dir_hidden(dir):
         return True
     else :
         return False
-        
-def scan_dir(path):
+    
+def scan_dir(path, search_hidden_directories=True, ):
     """this function scans all of the files and directories in a directory. The function then calls its self on any of the directories that it found. this causes it to build up a tree of all the files and directories """
-    global files_in_snapshot
-    global symlinks_in_snapshot
-    global search_hidden_directories
+    files_in_snapshot=[]
+    symlinks_in_snapshot=[]
+    dir_in_snapshot=[]
     dir_in_current_dir=[]
+    unknown_in_snapshot=[]
     if os.path.exists(path)==False:
         debug_message ("error#############################",0)
         debug_message (("path:", path, "  doesnot exist"), 0)
@@ -170,52 +210,54 @@ def scan_dir(path):
 #            st_size ##size of file
             files_in_snapshot.append({"path":current_snapshot, "inode": stat.st_ino, "size":stat.st_size, "num_of_hardlinks":stat.st_nlink, "mode":stat.st_mode})
 
-        elif isMount:
-            is_mount_in_snapshot.append(current_snapshot)
+#        elif isMount:
+#            is_mount_in_snapshot.append(current_snapshot)
         else:
             unknown_in_snapshot.append(current_snapshot)
             
     for i in range(0, len(dir_in_current_dir)):
         scan_dir(dir_in_current_dir[i])
+    return (files_in_snapshot, dir_in_snapshot, symlinks_in_snapshot, unknown_in_snapshot)
+##
+##def print_scanned_dir_list():
+##    global files_in_snapshot
+##    global symlinks_in_snapshot
+##    print( "scanning dir", 2)
+##
+##
+##    for i in range(0, len(files_in_snapshot)):
+##        if files_in_snapshot[i]["num_of_hardlinks"]>1:
+##            print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"], "    num of hard links",  files_in_snapshot[i]["num_of_hardlinks"] 
+##
+##        else :
+##            print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"]
+###        current_open_file=open(files_in_snapshot[i], "r")
+###        #current_open_file.f.read(3)
+###        lines_in_file=current_open_file.readlines()
+###        #use for loop to write code into yaffs file
+###        print "number of line of code:", len(lines_in_file)
+###    print current_open_file
+##    for i in range(0, len(symlinks_in_snapshot)):
+##        print "symlinks in snapshot:", symlinks_in_snapshot[i]
+##    for i in range(0, len(dir_in_snapshot)):
+##        print "directories in snapshot:", dir_in_snapshot[i]
+##    for i in range(0, len(unknown_in_snapshot)):
+##        print "unknown objects in snapshot:", unknown_in_snapshot[i]
+##
 
-def print_scanned_dir_list():
-    global files_in_snapshot
-    global symlinks_in_snapshot
-    print( "scanning dir", 2)
 
-
-    for i in range(0, len(files_in_snapshot)):
-        if files_in_snapshot[i]["num_of_hardlinks"]>1:
-            print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"], "    num of hard links",  files_in_snapshot[i]["num_of_hardlinks"] 
-
-        else :
-            print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"]
-#        current_open_file=open(files_in_snapshot[i], "r")
-#        #current_open_file.f.read(3)
-#        lines_in_file=current_open_file.readlines()
-#        #use for loop to write code into yaffs file
-#        print "number of line of code:", len(lines_in_file)
-#    print current_open_file
-    for i in range(0, len(symlinks_in_snapshot)):
-        print "symlinks in snapshot:", symlinks_in_snapshot[i]
-    for i in range(0, len(dir_in_snapshot)):
-        print "directories in snapshot:", dir_in_snapshot[i]
-    for i in range(0, len(unknown_in_snapshot)):
-        print "unknown objects in snapshot:", unknown_in_snapshot[i]
-
-def copy_scanned_files_into_yaffs():
-    global files_in_snapshot
-    global symlinks_in_snapshot
-    global path
+def copy_scanned_files_into_yaffs(files_in_snapshot,dir_in_snapshot,  symlinks_in_snapshot, unknown_in_snapshot,   path, yaffs_root_dir_path ):
+#files_in_snapshot, dir_in_snapshot, symlinks_in_snapshot, unknown_in_snapshot
 #########################################copy directories into yaffs so the files can be created in these directories
     debug_message("making directories in yaffs", 1)
     for i in range(0, len(dir_in_snapshot)):
 
         
         dir_path=join_paths(yaffs_root_dir_path,subtract_paths(dir_in_snapshot[i]["path"], path) )
-        output=yaffs_mkdir(dir_path,dir_in_snapshot[i]["mode"] )
-        debug_message(("made directory:", dir_path,   "  output", output), 1)
-        debug_message(("mode" ,dir_in_snapshot[i]["mode"]), 2)
+        create_dir(dir_in_snapshot[i], path, yaffs_root_dir_path)
+#        output=yaffs_mkdir(dir_path,dir_in_snapshot[i]["mode"] )
+#        debug_message(("made directory:", dir_path,   "  output", output), 1)
+#        debug_message(("mode" ,dir_in_snapshot[i]["mode"]), 2)
     
     
     
@@ -272,21 +314,24 @@ def copy_scanned_files_into_yaffs():
     
     for i in range(0, len(unknown_in_snapshot)):
         debug_message( ("unknown object in snapshot:", unknown_in_snapshot[i]), 0)
-        
-def import_into_yaffs(file_path, yaffs_path="/yaffs2/", debug_level=1,  copy_hidden_dir=False,new_yaffs_trace_val=0 ):
-    global current_debug_level
-    global search_hidden_directories
-    global yaffs_root_dir_path
-    global path
     
-    current_debug_level=debug_level
-    search_hidden_directories=copy_hidden_dir
-    yaffs_root_dir_path=yaffs_path
-    path=file_path
+    
+def import_into_yaffs(file_path, yaffs_path="/yaffs2/", debug_level=1,  copy_hidden_dir=True,new_yaffs_trace_val=0 ):
+#    global current_debug_level
+#    global search_hidden_directories
+#    global yaffs_root_dir_path
+#    global path
+    
+#    current_debug_level=debug_level
+#    search_hidden_directories=copy_hidden_dir
+#    yaffs_root_dir_path=yaffs_path
+#    path=file_path
     old_yaffs_trace_val=yaffs_get_trace()
     yaffs_set_trace(new_yaffs_trace_val)
-    scan_dir(path)
-    copy_scanned_files_into_yaffs()
+    
+    data=scan_dir(file_path, copy_hidden_dir)
+    copy_scanned_files_into_yaffs(data[0], data[1], data[2], data[3],file_path,  yaffs_path)
+    
     yaffs_set_trace(old_yaffs_trace_val)
     
     
@@ -294,7 +339,7 @@ if __name__=="__main__":
     yaffs_StartUp()
     yaffs_mount("/yaffs2/")
     yaffs_set_trace(0)
-    absolute_path = os.path.abspath(os.path.curdir)
+#    absolute_path = os.path.abspath(os.path.curdir)
     #print "absolute path:", absolute_path
     current_debug_level=1
     search_hidden_directories=True
@@ -312,9 +357,9 @@ if __name__=="__main__":
 #    path="/home/timothy/my_stuff/old_laptop/timothy/programming_lejos/"
 
 
-
-    scan_dir(path)
-    copy_scanned_files_into_yaffs()
+    import_into_yaffs(path, yaffs_root_dir_path, current_debug_level,  search_hidden_directories, 0 )
+#    scan_dir(path)
+#    copy_scanned_files_into_yaffs()
     #print_scanned_dir_list()
 
     print"unmounting yaffs:", yaffs_unmount("/yaffs2/")