5 yaffs_mount("/yaffs2/")
6 absolute_path = os.path.abspath(os.path.curdir)
7 print "absolute path:", absolute_path
12 symlinks_in_snapshot=[]
13 unknown_in_snapshot=[]
14 is_mount_in_snapshot=[]
16 # global dir_in_snapshot
17 # global files_in_snapshot
18 # global symlinks_in_snapshot
19 # global unknown_in_snapshot
20 #path=raw_input("path")
21 #print "does this path exist?", os.path.exists(path)
22 #dir_snapshot=os.listdir(path)
24 def is_dir_hidden(dir):
26 for i in range(0, len(dir)):
30 #print "slash_id", slash_id
31 if dir[slash_id[len(slash_id)-1]+1]==".":
37 global files_in_snapshot
38 global symlinks_in_snapshot
39 global search_hidden_directories
41 if os.path.exists(path)==False:
42 print "error#############################"
43 print "path:", path, " doesnot exist"
45 dir_snapshot=os.listdir(path)
46 for i in range(0, len(dir_snapshot)):
47 #print os.path.isdir(dir_snapshot[i])
48 #print os.path.isfile(dir_snapshot[i])
49 #print os.path.islink(dir_snapshot[i])
50 current_snapshot=os.path.join(path, dir_snapshot[i])
51 #print "current snapshot:", current_snapshot
52 isDir=os.path.isdir(current_snapshot)
53 isFile=os.path.isfile(current_snapshot)
54 isLink=os.path.islink(current_snapshot)
55 isMount=os.path.ismount(current_snapshot)
56 #print dir_snapshot[i]
58 if search_hidden_directories==True or is_dir_hidden(current_snapshot) ==False :
59 stat=os.stat(current_snapshot)
60 # st_mode ##mode of the folder read/write ect
61 dir_in_snapshot.append({"path":current_snapshot, "mode":stat.st_mode})
62 dir_in_current_dir.append(current_snapshot)
64 print "file is hidden so it is ingored", current_snapshot
66 stat=os.stat(current_snapshot)
67 # stat.st_ino ##inode number
68 # st_nlink ##number of hard links to this file
69 # st_size ##size of file
70 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})
72 symlinks_in_snapshot.append(current_snapshot)
74 is_mount_in_snapshot.append(current_snapshot)
76 unknown_in_snapshot.append(current_snapshot)
78 for i in range(0, len(dir_in_current_dir)):
79 scan_dir(dir_in_current_dir[i])
81 def print_scanned_dir_list():
82 global files_in_snapshot
83 global symlinks_in_snapshot
87 for i in range(0, len(files_in_snapshot)):
88 if files_in_snapshot[i]["num_of_hardlinks"]>1:
89 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"]
92 print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"]
93 # current_open_file=open(files_in_snapshot[i], "r")
94 # #current_open_file.f.read(3)
95 # lines_in_file=current_open_file.readlines()
96 # #use for loop to write code into yaffs file
97 # print "number of line of code:", len(lines_in_file)
98 # print current_open_file
99 for i in range(0, len(symlinks_in_snapshot)):
100 print "symlinks in snapshot:", symlinks_in_snapshot[i]
101 for i in range(0, len(dir_in_snapshot)):
102 print "directories in snapshot:", dir_in_snapshot[i]
103 for i in range(0, len(unknown_in_snapshot)):
104 print "unknown objects in snapshot:", unknown_in_snapshot[i]
106 def copy_scanned_files_into_yaffs():
107 global files_in_snapshot
108 global symlinks_in_snapshot
110 print"making directories in yaffs"
112 for i in range(0, len(dir_in_snapshot)):
113 path2=dir_in_snapshot[i]["path"][len(path):]
115 dir_path="/yaffs2"+path2
116 output=yaffs_mkdir(dir_path,dir_in_snapshot[i]["mode"] )
117 print"copied", dir_path, " output", output
118 print "mode" ,dir_in_snapshot[i]["mode"]
119 print "copying scanned files into yaffs"
122 for i in range(0, len(files_in_snapshot)):
123 if files_in_snapshot[i]["num_of_hardlinks"]>1:
124 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"]
128 file_path="/yaffs2/"+files_in_snapshot[i]["path"][len(path):]
129 print "creating file:", file_path
130 print "mode", files_in_snapshot[i]["mode"]
131 current_handle=yaffs_open(file_path, yaffs_O_CREAT | yaffs_O_TRUNC| yaffs_O_RDWR, files_in_snapshot[i]["mode"])
132 data_file=open(files_in_snapshot[i]["path"], "r")
133 yaffs_lseek(current_handle, 0, 0)
134 data_to_be_written= data_file.read()
137 #print "data to be saved", data_to_be_written
138 length_of_file=len(data_to_be_written)
139 print "length of data to be written",length_of_file
140 output=yaffs_write(current_handle,data_to_be_written , length_of_file)
141 print "writing file:", output
142 yaffs_ftruncate(current_handle, length_of_file)
144 output=yaffs_close(current_handle)
145 print "created a file", files_in_snapshot[i]["path"][len(path):], " ", output
148 print "ran out of space exiting"
150 #print "inode",files_in_snapshot[i]["inode"],"size",files_in_snapshot[i]["size"],"path:", files_in_snapshot[i]["path"]
152 # current_open_file=open(files_in_snapshot[i], "r")
153 # #current_open_file.f.read(3)
154 # lines_in_file=current_open_file.readlines()
155 # #use for loop to write code into yaffs file
156 # print "number of line of code:", len(lines_in_file)
157 # print current_open_file
158 for i in range(0, len(symlinks_in_snapshot)):
159 print "symlinks in snapshot:", symlinks_in_snapshot[i]
160 for i in range(0, len(dir_in_snapshot)):
161 print "directories in snapshot:", dir_in_snapshot[i]
162 for i in range(0, len(unknown_in_snapshot)):
163 print "unknown objects in snapshot:", unknown_in_snapshot[i]
165 if __name__=="__main__":
167 #path=raw_input("path")
168 path="/home/timothy/work/yaffs/git/yaffs2"
169 path="/home/timothy/my_stuff/old_laptop/timothy/programming_lejos/"
171 #path="/home/timothy"
173 #x=raw_input("search hidden files and directories?[y/n]")
175 if x=="y" or x=="yes":
176 search_hidden_directories=True
178 search_hidden_directories=False
180 copy_scanned_files_into_yaffs()
181 #print_scanned_dir_list()
183 print"unmounting yaffs:", yaffs_unmount("yaffs2/")