8 symlinks_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:
19 def join_paths(path1, path2):
21 if path1[len(path1)-1]=="/"and path2[0]=="/":
23 elif path1[len(path1)-1]!="/"and path2[0]!="/":
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:]
40 debug_message("error:could not subtract paths", 0)
41 debug_message( ("paths do not match:"+ path1+ " "+path2), 0)
45 debug_message( ("cannot subtract path2(:", path2, ") from path1(", path1, ")because path 2 is too long"), 0)
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()
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)
68 output=yaffs_close(current_handle)
69 debug_message( ("created a file", file["path"][len(path):], " ", output), 1)
72 debug_message ("ran out of space exiting", 0)
76 def remove_file_from_path(path):
78 for i in range(0, len(path)):
81 new_path=path[:slash_id[len(slash_id)-1]]
82 debug_message( ("removed file from path", new_path), 1)
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"""
87 for i in range(0, len(dir)):
91 if dir[slash_id[len(slash_id)-1]+1]==".":
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)
106 dir_snapshot=os.listdir(path)
107 for i in range(0, len(dir_snapshot)):
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)
116 stat=os.lstat(current_snapshot)
118 ##note the order of these if and elif statemens is importaint since a file can be symbloic link and a file
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)
125 debug_message( ("file is hidden so it is ingored", current_snapshot,), 1)
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
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})
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})
141 is_mount_in_snapshot.append(current_snapshot)
143 unknown_in_snapshot.append(current_snapshot)
145 for i in range(0, len(dir_in_current_dir)):
146 scan_dir(dir_in_current_dir[i])
148 def print_scanned_dir_list():
149 global files_in_snapshot
150 global symlinks_in_snapshot
151 print( "scanning dir", 2)
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"]
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]
173 def copy_scanned_files_into_yaffs():
174 global files_in_snapshot
175 global symlinks_in_snapshot
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)):
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)
189 #########################################copy file into yaffs
190 debug_message("copying scanned files into yaffs", 1)
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)):
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"]:
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.
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])
229 ############################copy symlinks into yaffs
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);
240 for i in range(0, len(unknown_in_snapshot)):
241 debug_message( ("unknown object in snapshot:", unknown_in_snapshot[i]), 0)
243 if __name__=="__main__":
245 yaffs_mount("/yaffs2/")
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
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
261 # path="/home/timothy/work/yaffs/git/yaffs2"
262 # path="/home/timothy/my_stuff/old_laptop/timothy/programming_lejos/"
267 copy_scanned_files_into_yaffs()
268 #print_scanned_dir_list()
270 print"unmounting yaffs:", yaffs_unmount("yaffs2/")