34448510635e823df086e2a2af442990dd08f0c1
[yaffs2.git] / direct / python / yaffs_importer.py
1 import os
2 from yaffsfs import *
3 import sys
4
5 yaffs_StartUp()
6 yaffs_mount("/yaffs2/")
7 absolute_path = os.path.abspath(os.path.curdir)
8 print "absolute path:", absolute_path
9
10 dir_in_snapshot=[]
11 files_in_snapshot=[]
12 symlinks_in_snapshot=[]
13 unknown_in_snapshot=[]
14 is_mount_in_snapshot=[]
15
16 def join_paths(path1, path2):
17     new_path=path1
18     if path1[len(path1)-1]=="/"and path2[0]=="/":
19         new_path+=path2[1:]
20     elif path1[len(path1)-1]!="/"and path2[0]!="/":
21         new_path+="/"
22         new_path+=path2
23     else:
24         new_path+=path2
25     return new_path
26     
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:]
36         else :
37             print "paths do not match:", path1, "  ", path2
38             return 0
39         return new_path
40     else :
41         print "cannot subtract path2(:", path2, ") from path1(", path1, ")because path 2 is too long"
42         return 0
43  
44     
45 def create_file(file):
46     print "\n \n \n"
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()
54     
55     
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)
62     
63     output=yaffs_close(current_handle)
64     print "created a file", file["path"][len(path):], "  ", output
65     
66     if output==-1:
67         print "ran out of space exiting"
68         return 0
69         
70         
71 def remove_file_from_path(path):
72     slash_id=[]
73     for i in range(0, len(path)):
74         if path[i]=="/":
75             slash_id.append(i)
76     new_path=path[:slash_id[len(slash_id)-1]]
77     print "removed file from path", new_path
78     return 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"""
81     slash_id=[]
82     for i in range(0, len(dir)):
83         if dir[i]=="/":
84             slash_id.append(i)
85
86     if dir[slash_id[len(slash_id)-1]+1]==".":
87         return True
88     else :
89         return False
90         
91 def scan_dir(path):
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
96     dir_in_current_dir=[]
97     if os.path.exists(path)==False:
98         print "error#############################"
99         print "path:", path, "  doesnot exist"
100         return 0
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]
113
114         stat=os.lstat(current_snapshot)
115         
116         ##note the order of these if and elif statemens is importaint since a file can be symbloic link and a file
117         if isDir:
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)
122             else :
123                 print "file is hidden so it is ingored", current_snapshot
124         elif  isLink:
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
130             x=current_snapshot
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})
135         elif isFile:
136
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})
141
142         elif isMount:
143             is_mount_in_snapshot.append(current_snapshot)
144         else:
145             unknown_in_snapshot.append(current_snapshot)
146             
147     for i in range(0, len(dir_in_current_dir)):
148         scan_dir(dir_in_current_dir[i])
149
150 def print_scanned_dir_list():
151     global files_in_snapshot
152     global symlinks_in_snapshot
153     print "scanned dir"
154
155
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"] 
159
160         else :
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]
174
175 def copy_scanned_files_into_yaffs():
176     global files_in_snapshot
177     global symlinks_in_snapshot
178     global path
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):]
183         print"path2", path2
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"]
188     
189     
190     
191 #########################################coyp file into yaffs
192     print "copying scanned files into yaffs"
193     list=[]
194     inode_blacklist=[]
195     break_flag=0
196     print "file in snapshot##############"
197     for a in range(0, len(files_in_snapshot)):
198         print files_in_snapshot[a]
199     print "\n\n\n"
200     for i in range(0, len(files_in_snapshot)):
201         list=[]
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"
213                     break_flag=1
214                     break
215             for a in range(0, len(list)):
216                 print list[a]
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.
220             create_file(list[0])
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])
230
231
232 ############################copy symlinks into yaffs
233
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);
241    
242   
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]
247
248 if __name__=="__main__":
249     print sys.argv
250     path=sys.argv[1]
251 #
252 #
253 #    path="/home/timothy/work/yaffs/git/yaffs2"
254 #    path="/home/timothy/my_stuff/old_laptop/timothy/programming_lejos/"
255
256
257     x="n"
258     if x=="y" or x=="yes":
259         search_hidden_directories=True
260     else :
261         search_hidden_directories=False
262     scan_dir(path)
263     copy_scanned_files_into_yaffs()
264     #print_scanned_dir_list()
265
266     print"unmounting yaffs:", yaffs_unmount("yaffs2/")