3 ## YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
5 ## Copyright (C) 2002-2010 Aleph One Ltd.
6 ## for Toby Churchill Ltd and Brightstar Engineering
8 ## Created by Timothy Manning <timothy@yaffs.net>
10 ## This program is free software; you can redistribute it and/or modify
11 ## it under the terms of the GNU General Public License version 2 as
12 ## published by the Free Software Foundation.
21 yaffs_mount("/yaffs2/")
23 root_window.title("YAFFS Browser")
24 mount_list_text_variable=tk.StringVar()
26 mount_list_text_variable.set("/yaffs2/")
27 current_directory_dict={}
39 target_path=self.file_editor_text.get("1.0", tk.END) ##"1.0" is the index of the first line of text
40 target_path=target_path[0:len(target_path)-1]
41 new_path=self.file_path
42 print "creating a symlink \n target:##", target_path, "##"
43 target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
44 if target_file_exists>=0:
45 ##file exists,create symlink
46 print "target file exist, creating symlink"
47 yaffs_unlink(new_path)
48 output=yaffs_symlink(target_path, new_path)
49 print "yaffs symlink output=", output
50 self.file_editor_root.destroy()
53 print "target file does not exist, cannot create symlink"
55 #global current_directory_dict
56 print "saving the file"
57 print self.file_editor_text.get("1.0", tk.END) ##"1.0" is the index of the first line of text
58 yaffs_lseek(self.yaffs_handle, 0, 0)
59 data_to_be_written=self.file_editor_text.get("1.0", tk.END)
60 print "data to be saved", data_to_be_written
61 length_of_file=len(data_to_be_written)
62 print "length of data to be written",length_of_file
63 output=yaffs_write(self.yaffs_handle,data_to_be_written , length_of_file)
64 print "output", output
65 yaffs_ftruncate(self.yaffs_handle, length_of_file)
66 yaffs_close(self.yaffs_handle)
67 self.yaffs_handle = yaffs_open(self.file_path,66,0666)
70 def __init__(self, isLink=0):
71 global current_directory_dict
72 x=name_list_box.curselection()
74 self.file_editor_root =tk.Toplevel()
75 self.save_button=tk.Button(self.file_editor_root, text="save", command=self.save_file)
76 self.save_button.pack(fill=tk.BOTH)
78 self.file_path=current_directory_dict[self.id]["path"]
79 print "file path", self.file_path
80 self.file_editor_root.title(current_directory_dict[self.id]["path"])
81 self.file_editor_text=tk.Text(self.file_editor_root)
82 self.yaffs_handle = yaffs_open(current_directory_dict[self.id]["path"],66,0666)
83 length_of_file=yaffs_lseek(self.yaffs_handle, 0, 2) ##seeks to the end of the file
84 yaffs_lseek(self.yaffs_handle, 0, 0)## returns the handle to the front of th file
85 print "length of file to be opened:", length_of_file
86 if isLink==True and False ==True : ##this alows the symlink to be edited and is no longer used. to renable it delete "and False ==True"
87 print "opening symlink"
88 self.file_contents=ctypes.create_string_buffer(1000)
89 yaffs_readlink(self.file_path,self.file_contents,1000)
93 self.file_contents=ctypes.create_string_buffer(length_of_file)
94 yaffs_read(self.yaffs_handle,self.file_contents,length_of_file)
95 print "file contents", self.file_contents.raw
96 self.file_editor_text.insert(tk.END, self.file_contents.raw)
97 self.file_editor_text.pack(fill=tk.BOTH)
98 ##self.file_editor_text.bind("<Control-s>", self.save_file)
99 ##doesn't work because it can't pass "self"
102 global current_directory_dict
103 print "loading a new directory*******************************************************************"
104 ##deleate current items in text box
105 name_list_box.delete(0, tk.END)
106 current_directory_dict=yaffs_ls(mount_list_text_variable.get())
107 print "new directory", current_directory_dict
108 ##copy directory into file box
109 for x in range(0,len(current_directory_dict)):
110 name_list_box.insert(x,(current_directory_dict[x]["inodes"]+" "+ current_directory_dict[x]["type"]+" "+ current_directory_dict[x]["size"]+" "+ current_directory_dict[x]["path"]+" "+current_directory_dict[x]["extra_data"]))
111 name_list_box.grid(column=0, row=1)
112 return current_directory_dict
115 ##this isn't working. somethihg need to be changed in the config of the simulator to release the handle of the emfile
116 print "remounting yaffs"
117 print"unmounting yaffs:", yaffs_unmount("yaffs2/")
118 print "mounting yaffs", yaffs_mount("/yaffs2/")
121 def load_file(link=0):
122 global open_windows_list
123 open_windows_list.append(editor(link))
125 def load_command(self=0):
126 global current_directory_dictls
128 print "you loaded a file/dir/link"
129 x=name_list_box.curselection()
131 print "cursor selection", x
132 print "dict", current_directory_dict
134 print "file inode is:", current_directory_dict[x]["inodes"]
137 print "file path is:", current_directory_dict[x]["path"]
138 if current_directory_dict[x]["type"]=="dir":
140 print "open directory"
141 mount_list_text_variable.set(current_directory_dict[x]["path"])
142 print mount_list_text_variable.get()
143 print "old directory dict", current_directory_dict
144 #current_directory_dict=load_dir()
147 print "new directory dict passed back"
149 elif current_directory_dict[x]["type"]=="file" :
153 elif current_directory_dict[x]["type"]=="link":
154 print "loading a symlink"
156 ##mount_list_text_variable.set(mount_list_text_variable.get()+str(list[0][0]))
158 def back_a_directory(self=0):
159 x=len(mount_list_text_variable.get())
160 string=mount_list_text_variable.get()
162 #print "length of path", x
163 #print "string been sorted:", string
164 for i in range(0, x):
167 #print "slash found at:", i
170 ##slashes_id.sort() not needed because the list is already in acending order
171 ##print "sorted",slashes_id
173 string=string[0: slashes_id[len(slashes_id)-2]+1]
175 mount_list_text_variable.set(string)
186 if dname[-1] != "/": dname = dname + "/"
187 dc = yaffs_opendir(dname)
189 sep = yaffs_readdir(dc)
193 fullname = dname + se.d_name
194 st = yaffs_stat_struct()
195 result = yaffs_lstat(fullname,byref(st))
196 perms = st.st_mode & 0777
197 ftype = st.st_mode & yaffs_S_IFMT
198 isFile = True if ftype == yaffs_S_IFREG else False
199 isDir = True if ftype == yaffs_S_IFDIR else False
200 isSymlink= True if ftype == yaffs_S_IFLNK else False
203 ls_dict.append ({"type" :"file", "inodes" : str(se.d_ino), "permissions" : str(hex(perms)), "size": str(st.st_size), "path": fullname,"extra_data":""})
204 print "file st.st_mode:", st.st_mode
207 print "dir st.st_mode:", st.st_mode
209 ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":str( hex(perms)),"size":"0", "path": fullname+"/", "extra_data":""})
211 print "symlink st.st_mode:", st.st_mode
212 file_contents=ctypes.create_string_buffer(30)
213 yaffs_readlink(fullname,file_contents,30)
214 string=repr(file_contents.value)
215 print "len of str", len(string)
218 print "string", string, "###"
220 ls_dict.append ({"type" :"link", "inodes" : str(se.d_ino), "permissions" : str(hex(perms)), "size": str(st.st_size), "path": fullname, "extra_data":"> "+string})
223 print "unknown st.st_mode:", st.st_mode
224 ls_dict.append({ "type":"Other", "inodes":str(se.d_ino), "permissions":str( hex(perms)), "size":"0", "path": fullname,"extra_data":""})
225 sep = yaffs_readdir(dc)
229 print "Could not open directory"
234 toolbar_frame=tk.Frame(root_window)
235 button_open=tk.Button(toolbar_frame, command=load_command, text="load")
236 button_open.grid(column=0, row=0)
237 button_back=tk.Button(toolbar_frame, command=back_a_directory, text="back")
238 button_back.grid(column=1, row=0)
239 toolbar_frame.grid(row=0, column=0, columnspan=3)
241 def delete_selected(selected_dir=0):
243 print"using current_directory_dict"
244 global current_directory_dict
245 x=name_list_box.curselection()
247 print current_directory_dict[x]["type"]
248 if current_directory_dict[x]["type"]=="file":
249 path=current_directory_dict[x]["path"]
251 output=yaffs_unlink(path)
252 print "unlinking output:", output
253 elif current_directory_dict[x]["type"]=="dir":
254 path=current_directory_dict[x]["path"]
255 inside_dir=yaffs_ls(path)
256 print "files and folder inside dir", inside_dir
257 print "len of dir", len(inside_dir)
258 if inside_dir!=[]: ##if the dir is not empty
259 ## remove stuff in dir
260 for i in range(0,len(inside_dir)):
261 print "calling self*****"
262 delete_selected(inside_dir[i])
264 path =path[0:len(path)-1] ##this is to remove the "/" off the end of the of the file
265 print "removing:", path
266 output=yaffs_rmdir(path)
267 print "rmdir output:", output
269 print "using passed dir"
270 print "dir passed", selected_dir
271 current_directory_dict =selected_dir
273 print "after copying", current_directory_dict
274 print current_directory_dict["type"]
275 if current_directory_dict["type"]=="file":
276 path=current_directory_dict["path"]
278 output=yaffs_unlink(path)
279 print "unlinking output:", output
280 elif current_directory_dict["type"]=="dir":
281 path=current_directory_dict["path"]
282 inside_dir=yaffs_ls(path)
283 print "files and folder inside dir", inside_dir
284 print "len of dir", len(inside_dir)
285 if inside_dir!=[]: ##if the dir is not empty
286 ## remove stuff in dir
287 for i in range(0,len(inside_dir)):
288 print "calling self*****"
289 delete_selected(inside_dir[i])
291 path =path[0:len(path)-1] ##this is to remove the "/" off the end of the of the file
292 print "removing:", path
293 output=yaffs_rmdir(path)
294 print "rmdir output:", output
303 def open_the_file(self):
304 global mount_list_text_variable
305 print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
306 yaffs_handle=yaffs_open(self.path_entry_box.get(),66,0666)
307 yaffs_close(yaffs_handle)
308 self.new_file_window.destroy()
313 self.new_file_window.destroy()
315 global mount_list_text_variable
316 self.new_file_window =tk.Toplevel(takefocus=True)
317 path_frame=tk.Frame(self.new_file_window)
318 path_label=tk.Label(path_frame, text="file path")
319 path_label.pack(side=tk.LEFT)
321 text.set(mount_list_text_variable.get())
322 print "############################",mount_list_text_variable.get()
323 self.path_entry_box= tk.Entry(path_frame, textvariable=text)
324 self.path_entry_box.pack(side=tk.RIGHT)
326 button_frame=tk.Frame(self.new_file_window)
327 create_button=tk.Button(button_frame, text="Create", command=self.open_the_file)
328 create_button.pack(side=tk.LEFT)
329 cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
330 cancel_button.pack(side=tk.RIGHT)
336 def create_the_folder(self):
337 global mount_list_text_variable
338 print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
339 yaffs_mkdir(self.path_entry_box.get(),0666)
340 self.new_folder_window.destroy()
346 self.new_folder_window.destroy()
348 global mount_list_text_variable
349 self.new_folder_window =tk.Toplevel(takefocus=True)
350 path_frame=tk.Frame(self.new_folder_window)
351 path_label=tk.Label(path_frame, text="directory path")
352 path_label.pack(side=tk.LEFT)
354 text.set(mount_list_text_variable.get())
355 print "############################",mount_list_text_variable.get()
356 self.path_entry_box= tk.Entry(path_frame, textvariable=text)
357 self.path_entry_box.pack(side=tk.RIGHT)
359 button_frame=tk.Frame(self.new_folder_window)
360 create_button=tk.Button(button_frame, text="Create", command=self.create_the_folder)
361 create_button.pack(side=tk.LEFT)
362 cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
363 cancel_button.pack(side=tk.RIGHT)
372 def create_the_symlink(self):
373 global mount_list_text_variable
374 ##check the symlink's target is a file.
375 target_path=self.target_text.get()
376 new_path=self.new_text.get()
377 print "creating a symlink \n target:", target_path
378 target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
379 if target_file_exists>=0:
380 ##file exists,create symlink
381 print "target file exist, creating symlink"
382 yaffs_symlink(target_path, new_path)
383 self.new_file_window.destroy()
385 ##file does not exist
386 print "target file does not exist, cannot create symlink"
391 self.new_file_window.destroy()
393 global mount_list_text_variable
394 self.new_file_window =tk.Toplevel(takefocus=True)
395 target_frame=tk.Frame(self.new_file_window)
396 target_label=tk.Label(target_frame, text="target")
397 target_label.pack(side=tk.LEFT)
398 self.target_text=tk.StringVar()
399 self.target_text.set(mount_list_text_variable.get())
400 #print "############################",mount_list_text_variable.get()
401 self.target_entry_box= tk.Entry(target_frame, textvariable=self.target_text)
402 self.target_entry_box.pack(side=tk.RIGHT)
405 new_frame=tk.Frame(self.new_file_window)
406 new_label=tk.Label(new_frame, text="file path")
407 new_label.pack(side=tk.LEFT)
408 self.new_text=tk.StringVar()
409 self.new_text.set(mount_list_text_variable.get())
410 #print "############################",mount_list_text_variable.get()
411 self.new_entry_box= tk.Entry(new_frame, textvariable=self.new_text)
412 self.new_entry_box.pack(side=tk.RIGHT)
415 button_frame=tk.Frame(self.new_file_window)
416 create_button=tk.Button(button_frame, text="Create", command=self.create_the_symlink)
417 create_button.pack(side=tk.LEFT)
418 cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
419 cancel_button.pack(side=tk.RIGHT)
424 class new_hardlink():
429 def create_the_hardlink(self):
430 global mount_list_text_variable
431 ##check the symlink's target is a file.
432 target_path=self.target_text.get()
433 new_path=self.new_text.get()
434 print "creating a hardlink \n target:", target_path
435 target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
436 if target_file_exists>=0:
437 ##file exists,create symlink
438 print "target file exist, creating hardlink"
439 yaffs_link(target_path, new_path)
440 self.new_file_window.destroy()
442 ##file does not exist
443 print "target file does not exist, cannot create hardlink"
448 self.new_file_window.destroy()
450 global mount_list_text_variable
451 self.new_file_window =tk.Toplevel(takefocus=True)
452 target_frame=tk.Frame(self.new_file_window)
453 target_label=tk.Label(target_frame, text="target")
454 target_label.pack(side=tk.LEFT)
455 self.target_text=tk.StringVar()
456 self.target_text.set(mount_list_text_variable.get())
457 #print "############################",mount_list_text_variable.get()
458 self.target_entry_box= tk.Entry(target_frame, textvariable=self.target_text)
459 self.target_entry_box.pack(side=tk.RIGHT)
462 new_frame=tk.Frame(self.new_file_window)
463 new_label=tk.Label(new_frame, text="file path")
464 new_label.pack(side=tk.LEFT)
465 self.new_text=tk.StringVar()
466 self.new_text.set(mount_list_text_variable.get())
467 #print "############################",mount_list_text_variable.get()
468 self.new_entry_box= tk.Entry(new_frame, textvariable=self.new_text)
469 self.new_entry_box.pack(side=tk.RIGHT)
472 button_frame=tk.Frame(self.new_file_window)
473 create_button=tk.Button(button_frame, text="Create", command=self.create_the_hardlink)
474 create_button.pack(side=tk.LEFT)
475 cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
476 cancel_button.pack(side=tk.RIGHT)
480 ##mount list entry box init
481 mount_list_frame=tk.Frame(root_window)
482 mount_list_label=tk.Label(mount_list_frame, text="mount list")
483 mount_list_label.pack(side=tk.RIGHT)
485 mount_list_entry_box= tk.Entry(mount_list_frame,textvariable=mount_list_text_variable)
486 mount_list_entry_box.pack(side=tk.RIGHT)
487 mount_list_frame.grid(row=1, column=0, columnspan=2)
490 list_frame=tk.Frame(root_window)
491 name_list_box=tk.Listbox(list_frame,exportselection=0, height=30, width=80)
493 list_frame.grid(sticky=tk.W+tk.E+tk.N+tk.S)
495 name_list_box.bind("<Double-Button-1>", load_command)
497 browser_menu_bar=tk.Menu(root_window)
498 browser_file_menu=tk.Menu(browser_menu_bar)
500 browser_file_menu.add_command(label="Reload", command=load_dir)
501 browser_file_menu.add_command(label="Remount yaffs", command=remount_yaffs)
503 #browser_file_menu.add_command(label="Open")
504 #browser_file_menu.add_command(label="Save")
505 browser_menu_bar.add_cascade(label="File", menu=browser_file_menu)
506 root_window.config(menu=browser_menu_bar)
509 browser_edit_menu=tk.Menu(browser_menu_bar)
511 browser_edit_menu.add_command(label="New File", command=new_file)
512 browser_edit_menu.add_command(label="New Folder", command=new_folder)
513 browser_edit_menu.add_command(label="New Symlink", command=new_symlink)
514 browser_edit_menu.add_command(label="New Hardlink", command=new_hardlink)
517 browser_edit_menu.add_command(label="delete selected", command=delete_selected)
518 browser_menu_bar.add_cascade(label="Edit", menu=browser_edit_menu)
525 root_window.mainloop()
527 print"unmounting yaffs:", yaffs_unmount("yaffs2/")