6 yaffs_mount("/yaffs2/")
7 absolute_path = os.path.abspath(os.path.curdir)
8 print "absolute path:", absolute_path
12 symlinks_in_snapshot=[]
13 unknown_in_snapshot=[]
14 is_mount_in_snapshot=[]
16 def join_paths(path1, path2):
18 if path1[len(path1)-1]=="/"and path2[0]=="/":
20 elif path1[len(path1)-1]!="/"and path2[0]!="/":
27 def subtract_paths(path1, path2):
28 if len(path1)>len(path2):
29 ##if the two paths are diretly subtractable
30 if path1[0:len (path2)-1]==path2:
31 new_path=path1[len(path2):]
32 elif path1[1:len (path2)-1]==path2:
33 new_path=path1[len(path2)+1:]
34 elif path1[1:len (path2)]==path2[1:]:
35 new_path=path1[len(path2)-1:]
37 print "paths do not match:", path1, " ", path2
41 print "cannot subtract path2(:", path2, ") from path1(", path1, ")because path 2 is too long"
45 def create_file(file):
47 file_path="/yaffs2/"+file["path"][len(path):]
48 print "creating file:", file_path
49 print "mode", file["mode"]
50 current_handle=yaffs_open(file_path, yaffs_O_CREAT | yaffs_O_TRUNC| yaffs_O_RDWR, file["mode"])
51 data_file=open(file["path"], "r")
52 yaffs_lseek(current_handle, 0, 0)
53 data_to_be_written= data_file.read()
56 #print "data to be saved", data_to_be_written
57 length_of_file=len(data_to_be_written)
58 print "length of data to be written",length_of_file
59 output=yaffs_write(current_handle,data_to_be_written , length_of_file)
60 print "writing file:", output
61 yaffs_ftruncate(current_handle, length_of_file)
63 output=yaffs_close(current_handle)
64 print "created a file", file["path"][len(path):], " ", output
67 print "ran out of space exiting"
71 def remove_file_from_path(path):
73 for i in range(0, len(path)):
76 new_path=path[:slash_id[len(slash_id)-1]]
77 print "removed file from path", new_path
79 def is_dir_hidden(dir):
80 """this code tests if a directory is hidden (has a ./<name> format) and returns true if it is hidden"""
82 for i in range(0, len(dir)):
86 if dir[slash_id[len(slash_id)-1]+1]==".":
92 """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 """
93 global files_in_snapshot
94 global symlinks_in_snapshot
95 global search_hidden_directories
97 if os.path.exists(path)==False:
98 print "error#############################"
99 print "path:", path, " doesnot exist"
101 dir_snapshot=os.listdir(path)
102 for i in range(0, len(dir_snapshot)):
103 #print os.path.isdir(dir_snapshot[i])
104 #print os.path.isfile(dir_snapshot[i])
105 #print os.path.islink(dir_snapshot[i])
106 current_snapshot=os.path.join(path, dir_snapshot[i])
107 #print "current snapshot:", current_snapshot
108 isDir=os.path.isdir(current_snapshot)
109 isFile=os.path.isfile(current_snapshot)
110 isLink=os.path.islink(current_snapshot)
111 isMount=os.path.ismount(current_snapshot)
112 #print dir_snapshot[i]
114 stat=os.lstat(current_snapshot)
116 ##note the order of these if and elif statemens is importaint since a file can be symbloic link and a file
118 if search_hidden_directories==True or is_dir_hidden(current_snapshot) ==False :
119 # st_mode ##mode of the folder read/write ect
120 dir_in_snapshot.append({"path":current_snapshot, "mode":stat.st_mode})
121 dir_in_current_dir.append(current_snapshot)
123 print "file is hidden so it is ingored", current_snapshot
125 # print "symlink:", current_snapshot
126 # print "inode", stat.st_ino
127 # print "symbloic link", stat.s_iflnk
128 # print "mode", stat.st_mode
129 ##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
131 x=remove_file_from_path(x)
132 print "removed file from path"
133 target=join_paths(x,os.readlink(current_snapshot) )
134 symlinks_in_snapshot.append({"path":current_snapshot, "target":target})
137 # stat.st_ino ##inode number
138 # st_nlink ##number of hard links to this file
139 # st_size ##size of file
140 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})
143 is_mount_in_snapshot.append(current_snapshot)
145 unknown_in_snapshot.append(current_snapshot)
147 for i in range(0, len(dir_in_current_dir)):
148 scan_dir(dir_in_current_dir[i])
150 def print_scanned_dir_list():
151 global files_in_snapshot
152 global symlinks_in_snapshot
156 for i in range(0, len(files_in_snapshot)):
157 if files_in_snapshot[i]["num_of_hardlinks"]>1:
158 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"]
161 print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"]
162 # current_open_file=open(files_in_snapshot[i], "r")
163 # #current_open_file.f.read(3)
164 # lines_in_file=current_open_file.readlines()
165 # #use for loop to write code into yaffs file
166 # print "number of line of code:", len(lines_in_file)
167 # print current_open_file
168 for i in range(0, len(symlinks_in_snapshot)):
169 print "symlinks in snapshot:", symlinks_in_snapshot[i]
170 for i in range(0, len(dir_in_snapshot)):
171 print "directories in snapshot:", dir_in_snapshot[i]
172 for i in range(0, len(unknown_in_snapshot)):
173 print "unknown objects in snapshot:", unknown_in_snapshot[i]
175 def copy_scanned_files_into_yaffs():
176 global files_in_snapshot
177 global symlinks_in_snapshot
179 #########################################copy directories into yaffs
180 print"making directories in yaffs"
181 for i in range(0, len(dir_in_snapshot)):
182 path2=dir_in_snapshot[i]["path"][len(path):]
184 dir_path="/yaffs2"+path2
185 output=yaffs_mkdir(dir_path,dir_in_snapshot[i]["mode"] )
186 print"copied", dir_path, " output", output
187 print "mode" ,dir_in_snapshot[i]["mode"]
191 #########################################coyp file into yaffs
192 print "copying scanned files into yaffs"
196 print "file in snapshot##############"
197 for a in range(0, len(files_in_snapshot)):
198 print files_in_snapshot[a]
200 for i in range(0, len(files_in_snapshot)):
202 if files_in_snapshot[i]["num_of_hardlinks"]>1 and files_in_snapshot[i]["inode"] not in inode_blacklist :
203 print "found a hard link"
204 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"]
205 for a in range(0, len(files_in_snapshot) ) :
206 if files_in_snapshot[a]["inode"] ==files_in_snapshot[i]["inode"] :
207 ##and os.path.isfile(files_in_snapshot[i])
208 print "found this file which matches inode",files_in_snapshot[a]
209 list.append(files_in_snapshot[a])
210 print "length of list", len(list)
211 if len(list)==files_in_snapshot[i]["num_of_hardlinks"]:
212 print "trying to break"
215 for a in range(0, len(list)):
217 ##add inode to blacklist. all of the indoes in the list should be the same.
218 inode_blacklist.append(list[0]["inode"])
219 ##create a file from the first hardlink.
221 target_path="/yaffs2/"+list[0]["path"][len(path):]
222 for i in range(1, len(list)):
223 print "target path", target_path
224 hardlink_path="/yaffs2/"+list[i]["path"][len(path):]
225 print "hardlink path", hardlink_path
226 output=yaffs_link(target_path,hardlink_path)
227 print"creating hardlink:", list[i]["path"], "output:", output
228 elif files_in_snapshot[i]["inode"] not in inode_blacklist :
229 create_file(files_in_snapshot[i])
232 ############################copy symlinks into yaffs
234 for i in range(0, len(symlinks_in_snapshot)):
235 print "symlinks in snapshot:", symlinks_in_snapshot[i]
236 target_path=join_paths("/yaffs2/", subtract_paths(symlinks_in_snapshot[i]["target"], path))
237 new_path=join_paths("/yaffs2/", subtract_paths(symlinks_in_snapshot[i]["path"], path))
238 output=yaffs_symlink(target_path, new_path)
239 print "created symlink",new_path , " > ", target_path, " output:", output
240 ##yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath);
243 for i in range(0, len(dir_in_snapshot)):
244 print "directories in snapshot:", dir_in_snapshot[i]
245 for i in range(0, len(unknown_in_snapshot)):
246 print "unknown objects in snapshot:", unknown_in_snapshot[i]
248 if __name__=="__main__":
253 # path="/home/timothy/work/yaffs/git/yaffs2"
254 # path="/home/timothy/my_stuff/old_laptop/timothy/programming_lejos/"
258 if x=="y" or x=="yes":
259 search_hidden_directories=True
261 search_hidden_directories=False
263 copy_scanned_files_into_yaffs()
264 #print_scanned_dir_list()
266 print"unmounting yaffs:", yaffs_unmount("yaffs2/")