9 symlinks_in_snapshot=[]
10 unknown_in_snapshot=[]
11 is_mount_in_snapshot=[]
12 def check_for_yaffs_errors(output):
16 error=yaffs_get_error()
17 debug_message("error######################################",0)
18 debug_message(("error code", error), 0)
20 def debug_message(message, debug_level):
21 """notew that debug level 0 will always be printed"""
22 """level 0 error messages"""
23 """level 1 basic tasks are shown(creating, deleating,ect)"""
24 """level 2 all process are shown"""
25 if current_debug_level>=debug_level:
28 def join_paths(path1, path2):
30 if path1[len(path1)-1]=="/"and path2[0]=="/":
32 elif path1[len(path1)-1]!="/"and path2[0]!="/":
39 def subtract_paths(path1, path2):
40 if len(path1)>len(path2):
41 ##if the two paths are diretly subtractable
42 if path1[0:len (path2)-1]==path2:
43 new_path=path1[len(path2):]
44 elif path1[1:len (path2)-1]==path2:
45 new_path=path1[len(path2)+1:]
46 elif path1[1:len (path2)]==path2[1:]:
47 new_path=path1[len(path2)-1:]
49 debug_message("error:could not subtract paths", 0)
50 debug_message( ("paths do not match:"+ path1+ " "+path2), 0)
54 debug_message( ("cannot subtract path2(:", path2, ") from path1(", path1, ")because path 2 is too long"), 0)
58 def create_file(file):
59 debug_message( "\n \n \n", 2)
60 file_path=yaffs_root_dir_path+file["path"][len(path):]
61 debug_message( ("creating file:", file_path), 2)
62 debug_message (("mode", file["mode"]), 2)
63 debug_message("opening file",2)
64 # output=yaffs_unlink(path)
65 # print"unlinking", output
66 # check_for_yaffs_errors(output)
67 current_handle=yaffs_open(file_path, yaffs_O_CREAT | yaffs_O_TRUNC| yaffs_O_RDWR, yaffs_S_IWRITE) ##opens a file with mode set to write
68 debug_message(("current_handle", current_handle), 2)
69 data_file=open(file["path"], "r")
70 output=yaffs_lseek(current_handle, 0, 0)
72 debug_message("error with yaffs lseeking", 2)
74 check_for_yaffs_errors(output)
75 data_to_be_written= data_file.read()
78 # debug_message( ("data to be saved########################################################################/n"+ data_to_be_written), 2)
79 # debug_message("###########################################################################################",2)
80 length_of_file=len(data_to_be_written)
81 debug_message (("length of data to be written",length_of_file), 2)
82 output=yaffs_write(current_handle,data_to_be_written , length_of_file)
84 debug_message(( "writing file:", output), 2)
86 debug_message(( "error writing file:", output), 0)
87 check_for_yaffs_errors(output)
88 output=yaffs_ftruncate(current_handle, length_of_file)
90 debug_message(( "truncating file:", output), 2)
92 debug_message(( "error truncating file:", output), 0)
93 check_for_yaffs_errors(output)
94 output=yaffs_close(current_handle)
96 debug_message(( "closing file:", output), 2)
98 debug_message(( "error closing file:", output), 0)
99 check_for_yaffs_errors(output)
100 ##changes the mode of the yaffs file to be the same as the scanned file
101 # yaffs_chmod(file_path, file["mode"]);
103 # debug_message(( "chmoding file:", output), 2)
105 # debug_message(( "error chmoding file:", output), 0)
106 # check_for_yaffs_errors(output)
108 def remove_file_from_path(path):
110 for i in range(0, len(path)):
113 new_path=path[:slash_id[len(slash_id)-1]]
114 debug_message( ("removed file from path", new_path), 2)
116 def is_dir_hidden(dir):
117 """this code tests if a directory is hidden (has a ./<name> format) and returns true if it is hidden"""
119 for i in range(0, len(dir)):
123 if dir[slash_id[len(slash_id)-1]+1]==".":
129 """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 """
130 global files_in_snapshot
131 global symlinks_in_snapshot
132 global search_hidden_directories
133 dir_in_current_dir=[]
134 if os.path.exists(path)==False:
135 debug_message ("error#############################",0)
136 debug_message (("path:", path, " doesnot exist"), 0)
138 dir_snapshot=os.listdir(path)
139 for i in range(0, len(dir_snapshot)):
141 current_snapshot=os.path.join(path, dir_snapshot[i])
142 debug_message (("current snapshot:", current_snapshot), 2)
143 isDir=os.path.isdir(current_snapshot)
144 isFile=os.path.isfile(current_snapshot)
145 isLink=os.path.islink(current_snapshot)
146 isMount=os.path.ismount(current_snapshot)
148 stat=os.lstat(current_snapshot)
150 ##note the order of these if and elif statemens is importaint since a file can be symbloic link and a file
152 if search_hidden_directories==True or (is_dir_hidden(current_snapshot) ==False or search_hidden_directories==True ) :
153 # st_mode ##mode of the folder read/write ect
154 dir_in_snapshot.append({"path":current_snapshot, "mode":stat.st_mode})
155 dir_in_current_dir.append(current_snapshot)
157 debug_message( ("file is hidden so it is ingored", current_snapshot,), 1)
160 ##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
162 x=remove_file_from_path(x)
163 target=join_paths(x,os.readlink(current_snapshot) )
164 symlinks_in_snapshot.append({"path":current_snapshot, "target":target})
167 # stat.st_ino ##inode number
168 # st_nlink ##number of hard links to this file
169 # st_size ##size of file
170 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})
173 is_mount_in_snapshot.append(current_snapshot)
175 unknown_in_snapshot.append(current_snapshot)
177 for i in range(0, len(dir_in_current_dir)):
178 scan_dir(dir_in_current_dir[i])
180 def print_scanned_dir_list():
181 global files_in_snapshot
182 global symlinks_in_snapshot
183 print( "scanning dir", 2)
186 for i in range(0, len(files_in_snapshot)):
187 if files_in_snapshot[i]["num_of_hardlinks"]>1:
188 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"]
191 print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"]
192 # current_open_file=open(files_in_snapshot[i], "r")
193 # #current_open_file.f.read(3)
194 # lines_in_file=current_open_file.readlines()
195 # #use for loop to write code into yaffs file
196 # print "number of line of code:", len(lines_in_file)
197 # print current_open_file
198 for i in range(0, len(symlinks_in_snapshot)):
199 print "symlinks in snapshot:", symlinks_in_snapshot[i]
200 for i in range(0, len(dir_in_snapshot)):
201 print "directories in snapshot:", dir_in_snapshot[i]
202 for i in range(0, len(unknown_in_snapshot)):
203 print "unknown objects in snapshot:", unknown_in_snapshot[i]
205 def copy_scanned_files_into_yaffs():
206 global files_in_snapshot
207 global symlinks_in_snapshot
209 #########################################copy directories into yaffs so the files can be created in these directories
210 debug_message("making directories in yaffs", 1)
211 for i in range(0, len(dir_in_snapshot)):
214 dir_path=join_paths(yaffs_root_dir_path,subtract_paths(dir_in_snapshot[i]["path"], path) )
215 output=yaffs_mkdir(dir_path,dir_in_snapshot[i]["mode"] )
216 debug_message(("made directory:", dir_path, " output", output), 1)
217 debug_message(("mode" ,dir_in_snapshot[i]["mode"]), 2)
221 #########################################copy file into yaffs
222 debug_message("copying scanned files into yaffs", 1)
226 debug_message("files to be copyied into yaffs", 2)
227 for a in range(0, len(files_in_snapshot)):
228 debug_message(files_in_snapshot[a], 2)
229 debug_message("\n\n\n", 2)
230 for i in range(0, len(files_in_snapshot)):
232 if files_in_snapshot[i]["num_of_hardlinks"]>1 and files_in_snapshot[i]["inode"] not in inode_blacklist :
233 debug_message("found a hard link", 2)
234 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)
235 for a in range(0, len(files_in_snapshot) ) :
236 if files_in_snapshot[a]["inode"] ==files_in_snapshot[i]["inode"] :
237 ##and os.path.isfile(files_in_snapshot[i])
238 debug_message(("found this file which matches inode",files_in_snapshot[a]), 2)
239 list.append(files_in_snapshot[a])
240 debug_message(("length of list", len(list)), 2)
241 if len(list)==files_in_snapshot[i]["num_of_hardlinks"]:
243 for a in range(0, len(list)):
244 debug_message(list[a], 2)
245 ##add inode to blacklist. all of the indoes in the list should be the same.
246 inode_blacklist.append(list[0]["inode"])
247 ##create a file from the first hardlink.
249 target_path=yaffs_root_dir_path+list[0]["path"][len(path):]
250 for i in range(1, len(list)):
251 debug_message("creating_symlink", 2)
252 debug_message(("target path", target_path), 2)
253 hardlink_path=yaffs_root_dir_path+list[i]["path"][len(path):]
254 debug_message(("hardlink path", hardlink_path), 2)
255 output=yaffs_link(target_path,hardlink_path)
256 debug_message(("creating hardlink:", list[i]["path"], "output:", output), 1)
257 elif files_in_snapshot[i]["inode"] not in inode_blacklist :
258 create_file(files_in_snapshot[i])
261 ############################copy symlinks into yaffs
263 for i in range(0, len(symlinks_in_snapshot)):
264 debug_message(("symlinks in snapshot:", symlinks_in_snapshot[i]), 2)
265 target_path=join_paths(yaffs_root_dir_path, subtract_paths(symlinks_in_snapshot[i]["target"], path))
266 new_path=join_paths(yaffs_root_dir_path, subtract_paths(symlinks_in_snapshot[i]["path"], path))
267 output=yaffs_symlink(target_path, new_path)
268 debug_message(("created symlink",new_path , " > ", target_path, " output:", output), 1)
269 ##yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath);
272 for i in range(0, len(unknown_in_snapshot)):
273 debug_message( ("unknown object in snapshot:", unknown_in_snapshot[i]), 0)
275 def import_into_yaffs(file_path, yaffs_path="/yaffs2/", debug_level=1, copy_hidden_dir=False,new_yaffs_trace_val=0 ):
276 global current_debug_level
277 global search_hidden_directories
278 global yaffs_root_dir_path
281 current_debug_level=debug_level
282 search_hidden_directories=copy_hidden_dir
283 yaffs_root_dir_path=yaffs_path
285 old_yaffs_trace_val=yaffs_get_trace()
286 yaffs_set_trace(new_yaffs_trace_val)
288 copy_scanned_files_into_yaffs()
289 yaffs_set_trace(old_yaffs_trace_val)
292 if __name__=="__main__":
294 yaffs_mount("/yaffs2/")
296 absolute_path = os.path.abspath(os.path.curdir)
297 #print "absolute path:", absolute_path
298 current_debug_level=1
299 search_hidden_directories=True
300 yaffs_root_dir_path="/yaffs2/"
303 for i in range(2, len(sys.argv)):
304 if sys.argv[i]=="-d":
305 current_debug_level=int( sys.argv[i+1])
306 if sys.argv[i]=="-ignore_hidden_directories":
307 search_hidden_directories=False
310 # path="/home/timothy/work/yaffs/git/yaffs2"
311 # path="/home/timothy/my_stuff/old_laptop/timothy/programming_lejos/"
316 copy_scanned_files_into_yaffs()
317 #print_scanned_dir_list()
319 print"unmounting yaffs:", yaffs_unmount("/yaffs2/")