yaffs just fixed the way in which a symlink is opened in the yaffs browser.
[yaffs2.git] / direct / python / yaffs_browser.py
index e3f58f46cc2539aefbad976a527d2b2fb5aec9ab..626bc78fc6ab9df649b63058032d987bb537506f 100755 (executable)
@@ -5,12 +5,12 @@ from yaffsfs import *
 import ctypes
 
 yaffs_StartUp()
-yaffs_mount("yaffs2/")
+yaffs_mount("/yaffs2/")
 root_window =tk.Tk()
 root_window.title("YAFFS Browser")
 mount_list_text_variable=tk.StringVar()
 
-mount_list_text_variable.set("yaffs2/")
+mount_list_text_variable.set("/yaffs2/")
 current_directory_dict={}
 open_windows_list=[]
 
@@ -60,7 +60,7 @@ class editor():
         self.id=int(x[0])
         self.file_editor_root =tk.Toplevel()
         self.save_button=tk.Button(self.file_editor_root, text="save", command=self.save_file)
-        self.save_button.pack()
+        self.save_button.pack(fill=tk.BOTH)
 
         self.file_path=current_directory_dict[self.id]["path"]
         print "file path", self.file_path
@@ -69,16 +69,19 @@ class editor():
         self.yaffs_handle = yaffs_open(current_directory_dict[self.id]["path"],66,0666)
         length_of_file=yaffs_lseek(self.yaffs_handle, 0, 2) ##seeks to the end of the file
         yaffs_lseek(self.yaffs_handle, 0, 0)## returns the handle to the front of th file
-        if isLink==True:
-            self.file_contents=ctypes.create_string_buffer(100)
-            yaffs_readlink(self.file_path,self.file_contents,100)
+        print "length of file to be opened:", length_of_file
+        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"
+            print "opening symlink"
+            self.file_contents=ctypes.create_string_buffer(1000)
+            yaffs_readlink(self.file_path,self.file_contents,1000)
             self.isLink=True
         else:
+            print"opening file"
             self.file_contents=ctypes.create_string_buffer(length_of_file)
             yaffs_read(self.yaffs_handle,self.file_contents,length_of_file)
-        print "file contents", self.file_contents.raw
+            print "file contents", self.file_contents.raw
         self.file_editor_text.insert(tk.END, self.file_contents.raw)
-        self.file_editor_text.pack()
+        self.file_editor_text.pack(fill=tk.BOTH)
         ##self.file_editor_text.bind("<Control-s>", self.save_file)
         ##doesn't work because it can't pass "self"
 
@@ -90,7 +93,7 @@ def load_dir():
     print "new directory", current_directory_dict
     ##copy directory into file box
     for x in range(0,len(current_directory_dict)):
-        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"]))
+        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"]))
     name_list_box.grid(column=0, row=1)
     return current_directory_dict
 
@@ -167,28 +170,36 @@ def yaffs_ls(dname):
             se = sep.contents
             fullname = dname + se.d_name
             st = yaffs_stat_struct()
-            result = yaffs_stat(fullname,byref(st))
+            result = yaffs_lstat(fullname,byref(st))
             perms = st.st_mode & 0777
-            isFile = True if st.st_mode & 0x8000 else False
-            isDir  = True if st.st_mode & 0x4000 else False
-            isSymlink=False if st.st_mode & 0x120000 else True
+            ftype = st.st_mode & yaffs_S_IFMT
+            isFile = True if ftype == yaffs_S_IFREG else False
+            isDir  = True if ftype == yaffs_S_IFDIR else False
+            isSymlink= True if ftype == yaffs_S_IFLNK else False
 
             if isFile :
-                ls_dict.append ({"type" :"file",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname})
+                ls_dict.append ({"type" :"file",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname,"extra_data":""})
                 print "file st.st_mode:", st.st_mode
-                print "st.st_mode andded with st.st_mode & 0x8000", st.st_mode & 0x8000
+
             elif isDir :
                 print "dir st.st_mode:", st.st_mode
-                print "st.st_mode andded with st.st_mode & 0x4000", st.st_mode & 0x4000
-                ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":str( hex(perms)),"size":"0",   "path": fullname+"/"})
+
+                ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":str( hex(perms)),"size":"0",   "path": fullname+"/", "extra_data":""})
             elif isSymlink:
+                print "symlink st.st_mode:", st.st_mode
+                file_contents=ctypes.create_string_buffer(30)
+                yaffs_readlink(fullname,file_contents,30)
+                string=repr(file_contents.value)
+                print "len of str", len(string)
+#                string.lstrip()
 
-                ls_dict.append ({"type" :"link",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname})
+                print "string", string, "###"
+
+                ls_dict.append ({"type" :"link",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname, "extra_data":"> "+string})
 
             else :
                 print "unknown st.st_mode:", st.st_mode
-                print "st.st_mode andded with st.st_mode & 0x120000", st.st_mode & 0x120000
-               ls_dict.append({ "type":"Other", "inodes":str(se.d_ino),  "permissions":str( hex(perms)), "size":"0",   "path": fullname})
+                ls_dict.append({ "type":"Other", "inodes":str(se.d_ino),  "permissions":str( hex(perms)), "size":"0",   "path": fullname,"extra_data":""})
             sep = yaffs_readdir(dc)
         yaffs_closedir(dc)
         return ls_dict
@@ -353,9 +364,63 @@ class new_symlink():
         else :
             ##file does not exist
             print "target file does not exist, cannot create symlink"
+        load_dir()
+
+    def cancel(self):
+        ##del self
+        self.new_file_window.destroy()
+    def __init__(self):
+        global mount_list_text_variable
+        self.new_file_window =tk.Toplevel(takefocus=True)
+        target_frame=tk.Frame(self.new_file_window)
+        target_label=tk.Label(target_frame, text="target")
+        target_label.pack(side=tk.LEFT)
+        self.target_text=tk.StringVar()
+        self.target_text.set(mount_list_text_variable.get())
+        #print "############################",mount_list_text_variable.get()
+        self.target_entry_box= tk.Entry(target_frame, textvariable=self.target_text)
+        self.target_entry_box.pack(side=tk.RIGHT)
+        target_frame.pack()
         
+        new_frame=tk.Frame(self.new_file_window)
+        new_label=tk.Label(new_frame, text="file path")
+        new_label.pack(side=tk.LEFT)
+        self.new_text=tk.StringVar()
+        self.new_text.set(mount_list_text_variable.get())
+        #print "############################",mount_list_text_variable.get()
+        self.new_entry_box= tk.Entry(new_frame, textvariable=self.new_text)
+        self.new_entry_box.pack(side=tk.RIGHT)
+        new_frame.pack()
         
-        
+        button_frame=tk.Frame(self.new_file_window)
+        create_button=tk.Button(button_frame, text="Create", command=self.create_the_symlink)
+        create_button.pack(side=tk.LEFT)
+        cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
+        cancel_button.pack(side=tk.RIGHT)
+        button_frame.pack()
+
+
+
+class new_hardlink():
+    path_entry_box=0
+    target_text=0
+    new_text=0
+    new_file_window=0
+    def create_the_hardlink(self):
+        global mount_list_text_variable
+        ##check the symlink's target is a file.
+        target_path=self.target_text.get()
+        new_path=self.new_text.get()
+        print "creating a hardlink \n target:", target_path
+        target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
+        if target_file_exists>=0:
+            ##file exists,create symlink
+            print "target file exist, creating hardlink"
+            yaffs_link(target_path, new_path)
+            self.new_file_window.destroy()
+        else :
+            ##file does not exist
+            print "target file does not exist, cannot create hardlink"
         load_dir()
 
     def cancel(self):
@@ -385,7 +450,7 @@ class new_symlink():
         new_frame.pack()
         
         button_frame=tk.Frame(self.new_file_window)
-        create_button=tk.Button(button_frame, text="Create", command=self.create_the_symlink)
+        create_button=tk.Button(button_frame, text="Create", command=self.create_the_hardlink)
         create_button.pack(side=tk.LEFT)
         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
         cancel_button.pack(side=tk.RIGHT)
@@ -403,9 +468,9 @@ mount_list_frame.grid(row=1, column=0, columnspan=2)
 
 
 list_frame=tk.Frame(root_window)
-name_list_box=tk.Listbox(list_frame,exportselection=0, height=30, width=50)
+name_list_box=tk.Listbox(list_frame,exportselection=0, height=30, width=80)
 load_dir()
-list_frame.grid()
+list_frame.grid(sticky=tk.W+tk.E+tk.N+tk.S)
 
 name_list_box.bind("<Double-Button-1>", load_command)
 
@@ -424,6 +489,8 @@ browser_edit_menu=tk.Menu(browser_menu_bar)
 browser_edit_menu.add_command(label="New File", command=new_file)
 browser_edit_menu.add_command(label="New Folder", command=new_folder)
 browser_edit_menu.add_command(label="New Symlink", command=new_symlink)
+browser_edit_menu.add_command(label="New Hardlink", command=new_hardlink)
+
 
 browser_edit_menu.add_command(label="delete selected", command=delete_selected)
 browser_menu_bar.add_cascade(label="Edit", menu=browser_edit_menu)