yaffs Yaffs importer is now working with command line arguments.
[yaffs2.git] / direct / python / yaffs_importer.py
1 import os
2 from yaffsfs import *
3 import sys
4
5
6 dir_in_snapshot=[]
7 files_in_snapshot=[]
8 symlinks_in_snapshot=[]
9 unknown_in_snapshot=[]
10 is_mount_in_snapshot=[]
11 def debug_message(message, debug_level):
12     """notew that debug level 0 will always be printed"""
13     """level 0 error messages"""
14     """level 1 basic tasks are shown(creating, deleating,ect)"""
15     """level 2 all process are shown"""
16     if current_debug_level>=debug_level:
17         print message
18         
19 def join_paths(path1, path2):
20     new_path=path1
21     if path1[len(path1)-1]=="/"and path2[0]=="/":
22         new_path+=path2[1:]
23     elif path1[len(path1)-1]!="/"and path2[0]!="/":
24         new_path+="/"
25         new_path+=path2
26     else:
27         new_path+=path2
28     return new_path
29     
30 def subtract_paths(path1, path2):
31     if len(path1)>len(path2):
32         ##if the two paths are diretly subtractable
33         if path1[0:len (path2)-1]==path2:
34             new_path=path1[len(path2):]
35         elif path1[1:len (path2)-1]==path2:
36             new_path=path1[len(path2)+1:]
37         elif path1[1:len (path2)]==path2[1:]:
38             new_path=path1[len(path2)-1:]
39         else :
40             debug_message("error:could not subtract paths", 0)
41             debug_message( ("paths do not match:"+ path1+ "  "+path2), 0)
42             return 0
43         return new_path
44     else :
45         debug_message( ("cannot subtract path2(:", path2, ") from path1(", path1, ")because path 2 is too long"), 0)
46         return 0
47  
48     
49 def create_file(file):
50     debug_message( "\n \n \n", 2)
51     file_path="/yaffs2/"+file["path"][len(path):]
52     debug_message( ("creating file:", file_path), 2)
53     debug_message (("mode", file["mode"]), 2)
54     current_handle=yaffs_open(file_path, yaffs_O_CREAT | yaffs_O_TRUNC| yaffs_O_RDWR, file["mode"]) 
55     data_file=open(file["path"], "r")
56     yaffs_lseek(current_handle, 0, 0)
57     data_to_be_written= data_file.read()
58     
59     
60     debug_message( ("data to be saved########################################################################/n"+ data_to_be_written), 2)
61     debug_message("###########################################################################################",2)
62     length_of_file=len(data_to_be_written)
63     debug_message (("length of data to be written",length_of_file), 2) 
64     output=yaffs_write(current_handle,data_to_be_written , length_of_file)
65     debug_message(( "writing file:", output), 2)
66     yaffs_ftruncate(current_handle, length_of_file)
67     
68     output=yaffs_close(current_handle)
69     debug_message( ("created a file", file["path"][len(path):], "  ", output), 1)
70     
71     if output==-1:
72         debug_message ("ran out of space exiting", 0)
73         return 0
74         
75         
76 def remove_file_from_path(path):
77     slash_id=[]
78     for i in range(0, len(path)):
79         if path[i]=="/":
80             slash_id.append(i)
81     new_path=path[:slash_id[len(slash_id)-1]]
82     debug_message( ("removed file from path", new_path), 1)
83     return new_path
84 def is_dir_hidden(dir):
85     """this code tests if a directory is hidden (has a ./<name> format) and returns true if it is hidden"""
86     slash_id=[]
87     for i in range(0, len(dir)):
88         if dir[i]=="/":
89             slash_id.append(i)
90
91     if dir[slash_id[len(slash_id)-1]+1]==".":
92         return True
93     else :
94         return False
95         
96 def scan_dir(path):
97     """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 """
98     global files_in_snapshot
99     global symlinks_in_snapshot
100     global search_hidden_directories
101     dir_in_current_dir=[]
102     if os.path.exists(path)==False:
103         debug_message ("error#############################",0)
104         debug_message (("path:", path, "  doesnot exist"), 0)
105         return 0
106     dir_snapshot=os.listdir(path)
107     for i in range(0, len(dir_snapshot)):
108
109         current_snapshot=os.path.join(path, dir_snapshot[i])
110         debug_message (("current snapshot:", current_snapshot), 2)
111         isDir=os.path.isdir(current_snapshot)
112         isFile=os.path.isfile(current_snapshot)
113         isLink=os.path.islink(current_snapshot)
114         isMount=os.path.ismount(current_snapshot)
115
116         stat=os.lstat(current_snapshot)
117         
118         ##note the order of these if and elif statemens is importaint since a file can be symbloic link and a file
119         if isDir:
120             if search_hidden_directories==True or (is_dir_hidden(current_snapshot) ==False or search_hidden_directories==True ) :
121 #                st_mode ##mode of the folder read/write ect
122                 dir_in_snapshot.append({"path":current_snapshot, "mode":stat.st_mode})
123                 dir_in_current_dir.append(current_snapshot)
124             else :
125                 debug_message( ("file is hidden so it is ingored", current_snapshot,), 1)
126         elif  isLink:
127
128             ##for some reason the os.readlink only gives the target link realative to the directory which the symbloic link is in. change this into a absolute path
129             x=current_snapshot
130             x=remove_file_from_path(x)
131             target=join_paths(x,os.readlink(current_snapshot) )
132             symlinks_in_snapshot.append({"path":current_snapshot, "target":target})
133         elif isFile:
134
135 #            stat.st_ino ##inode number
136 #            st_nlink ##number of hard links to this file
137 #            st_size ##size of file
138             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})
139
140         elif isMount:
141             is_mount_in_snapshot.append(current_snapshot)
142         else:
143             unknown_in_snapshot.append(current_snapshot)
144             
145     for i in range(0, len(dir_in_current_dir)):
146         scan_dir(dir_in_current_dir[i])
147
148 def print_scanned_dir_list():
149     global files_in_snapshot
150     global symlinks_in_snapshot
151     print( "scanning dir", 2)
152
153
154     for i in range(0, len(files_in_snapshot)):
155         if files_in_snapshot[i]["num_of_hardlinks"]>1:
156             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"] 
157
158         else :
159             print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"]
160 #        current_open_file=open(files_in_snapshot[i], "r")
161 #        #current_open_file.f.read(3)
162 #        lines_in_file=current_open_file.readlines()
163 #        #use for loop to write code into yaffs file
164 #        print "number of line of code:", len(lines_in_file)
165 #    print current_open_file
166     for i in range(0, len(symlinks_in_snapshot)):
167         print "symlinks in snapshot:", symlinks_in_snapshot[i]
168     for i in range(0, len(dir_in_snapshot)):
169         print "directories in snapshot:", dir_in_snapshot[i]
170     for i in range(0, len(unknown_in_snapshot)):
171         print "unknown objects in snapshot:", unknown_in_snapshot[i]
172
173 def copy_scanned_files_into_yaffs():
174     global files_in_snapshot
175     global symlinks_in_snapshot
176     global path
177 #########################################copy directories into yaffs so the files can be created in these directories
178     debug_message("making directories in yaffs", 1)
179     for i in range(0, len(dir_in_snapshot)):
180
181         
182         dir_path=join_paths("/yaffs2/",subtract_paths(dir_in_snapshot[i]["path"], path) )
183         output=yaffs_mkdir(dir_path,dir_in_snapshot[i]["mode"] )
184         debug_message(("made directory:", dir_path,   "  output", output), 1)
185         debug_message(("mode" ,dir_in_snapshot[i]["mode"]), 2)
186     
187     
188     
189 #########################################copy file into yaffs
190     debug_message("copying scanned files into yaffs", 1)
191     list=[]
192     inode_blacklist=[]
193
194     debug_message("files to be copyied into yaffs", 2)
195     for a in range(0, len(files_in_snapshot)):
196         debug_message(files_in_snapshot[a], 2)
197     debug_message("\n\n\n", 2)
198     for i in range(0, len(files_in_snapshot)):
199         list=[]
200         if files_in_snapshot[i]["num_of_hardlinks"]>1 and files_in_snapshot[i]["inode"] not in inode_blacklist :
201             debug_message("found a hard link", 2)
202             debug_message(("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"] ), 2)
203             for a in range(0, len(files_in_snapshot) ) :
204                 if files_in_snapshot[a]["inode"] ==files_in_snapshot[i]["inode"]  :
205                     ##and os.path.isfile(files_in_snapshot[i])
206                     debug_message(("found this file which matches inode",files_in_snapshot[a]), 2) 
207                     list.append(files_in_snapshot[a])
208                     debug_message(("length of list", len(list)), 2)
209                 if len(list)==files_in_snapshot[i]["num_of_hardlinks"]:
210                     break
211             for a in range(0, len(list)):
212                 debug_message(list[a], 2)
213             ##add inode to blacklist. all of the indoes in the list should be the same.
214             inode_blacklist.append(list[0]["inode"])
215             ##create a file from the first hardlink.
216             create_file(list[0])
217             target_path="/yaffs2/"+list[0]["path"][len(path):]
218             for i in range(1, len(list)):
219                 debug_message("creating_symlink", 2)
220                 debug_message(("target path", target_path), 2)
221                 hardlink_path="/yaffs2/"+list[i]["path"][len(path):]
222                 debug_message(("hardlink path", hardlink_path), 2)
223                 output=yaffs_link(target_path,hardlink_path)
224                 debug_message(("creating hardlink:", list[i]["path"], "output:", output), 1)
225         elif files_in_snapshot[i]["inode"] not in inode_blacklist :
226             create_file(files_in_snapshot[i])
227
228
229 ############################copy symlinks into yaffs
230
231     for i in range(0, len(symlinks_in_snapshot)):
232         debug_message(("symlinks in snapshot:", symlinks_in_snapshot[i]), 2)
233         target_path=join_paths("/yaffs2/", subtract_paths(symlinks_in_snapshot[i]["target"],  path))
234         new_path=join_paths("/yaffs2/", subtract_paths(symlinks_in_snapshot[i]["path"], path))
235         output=yaffs_symlink(target_path, new_path)
236         debug_message(("created symlink",new_path , " > ", target_path, "  output:", output), 1)
237         ##yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath);
238    
239     
240     for i in range(0, len(unknown_in_snapshot)):
241         debug_message( ("unknown object in snapshot:", unknown_in_snapshot[i]), 0)
242
243 if __name__=="__main__":
244     yaffs_StartUp()
245     yaffs_mount("/yaffs2/")
246     yaffs_set_trace(0)
247     absolute_path = os.path.abspath(os.path.curdir)
248     #print "absolute path:", absolute_path
249     current_debug_level=1
250     search_hidden_directories=True
251
252     #print sys.argv
253     path=sys.argv[1]
254     for i in range(2, len(sys.argv)):
255         if sys.argv[i]=="-d":
256             current_debug_level=int( sys.argv[i+1])
257         if sys.argv[i]=="-ignore_hidden_directories":
258             search_hidden_directories=False
259 #
260 #
261 #    path="/home/timothy/work/yaffs/git/yaffs2"
262 #    path="/home/timothy/my_stuff/old_laptop/timothy/programming_lejos/"
263
264
265
266     scan_dir(path)
267     copy_scanned_files_into_yaffs()
268     #print_scanned_dir_list()
269
270     print"unmounting yaffs:", yaffs_unmount("yaffs2/")