changeset 6:5f53dc5beec9

vfat names incompatibility fix - bug fixed in getFsType(bad fs returned in most cases) - removing gettext (_("")) in a debug fonction with filename, as the filename can be with invalid utf-8, and then cause an exception. A better solution can probably be found later. - No more progress bar when the files have a null sum
author Goffi <goffi@goffi.org>
date Mon, 27 Sep 2010 16:44:25 +0800
parents 7edbb0e0d9dd
children a110d31482f7
files gcp
diffstat 1 files changed, 29 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/gcp	Mon Sep 27 13:09:42 2010 +0800
+++ b/gcp	Mon Sep 27 16:44:25 2010 +0800
@@ -130,15 +130,16 @@
 
         self.copy_list = []
         self.mounts = self.__getMountPoints()
-        self.files_left = 0
         self.bytes_total = 0
         self.bytes_copied = 0
 
     def getFsType(self, path):
-        fs=''
+        fs = ''
+        last_mount_point = ''
         for mount in self.mounts:
-            if path.startswith(mount) and len(self.mounts[mount])>=len(fs):
+            if path.startswith(mount) and len(mount)>=len(last_mount_point):
                 fs = self.mounts[mount]
+                last_mount_point = mount
         return fs
 
     def __getMountPoints(self):
@@ -159,12 +160,11 @@
         """Add a file to the copy list
         @param path: absolute path of file
         @param options: options as return by optparse"""
-        debug (_("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)") % {"path":path,
-                                                                                     "dest_path":dest_path,
-                                                                                     "fs_type":self.getFsType(dest_path)}              )
+        debug ("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)" % {"path":path,
+                                                                                  "dest_path":dest_path,
+                                                                                   "fs_type":self.getFsType(dest_path)}              )
         try:
             self.bytes_total+=os.path.getsize(path)
-            self.files_left+=1
             self.copy_list.insert(0,(path, dest_path, options))
         except OSError,e:
             error(_("Can't copy %(path)s: %(exception)s") % {'path':path, 'exception':e.strerror})
@@ -175,6 +175,7 @@
         @param path: absolute path of dir
         @param options: options as return by optparse"""
         #We first check that the dest path exists, and create it if needed
+        dest_path = self.__filename_fix(dest_path, options)
         if not os.path.exists(dest_path):
             debug (_("Creating directory %s") % dest_path)
             os.makedirs(dest_path) #TODO: check permissions
@@ -221,7 +222,7 @@
             source_fd = open(source_file, 'rb')
             filename = os.path.basename(source_file)
             assert(filename)
-            dest_file = os.path.join(dest_path,filename)
+            dest_file = self.__filename_fix(os.path.join(dest_path,filename),options)
             if os.path.exists(dest_file) and not options.force:
                 warning (_("File [%s] already exists, skipping it !") % dest_file)
                 return True
@@ -254,11 +255,21 @@
             source_fd.close()
             dest_fd.close()
             self.__post_copy(source_fd.name, dest_fd.name, options)
-            self.files_left -= 1
-            assert (self.files_left >= 0)
             return False
         return True
 
+    def __filename_fix(self, filename, options):
+        if self.getFsType(filename) == 'vfat' and options.fs_fix:
+            filename = filename.replace('\\','_')\
+                               .replace(':',';')\
+                               .replace('*','+')\
+                               .replace('?','')\
+                               .replace('"','\'')\
+                               .replace('<','[')\
+                               .replace('>',']')\
+                               .replace('|','!')
+        return filename
+
     def __post_copy(self, source_file, dest_file, options):
         """Do post copy traitement (mainly managing --preserve option)"""
         st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime = os.stat(source_file)
@@ -281,6 +292,9 @@
             if self.pbar.maxval != self.bytes_total:
                 self.pbar.maxval = self.bytes_total
         except AttributeError:
+            if not self.bytes_total:
+                #No progress bar if the files have a null size
+                return
             self.pbar = ProgressBar(self.bytes_total,[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()])
             self.pbar.start()
         self.pbar.update(self.bytes_copied)
@@ -320,9 +334,12 @@
         parser.add_option("--preserve", action="store", default='mode,ownership,timestamps',
                     help=_("preserve  the  specified  attributes"))
         
-        parser.add_option("--no-unicode-fix", action="store_true", default=False,
+        parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True,
                     help=_("don't fixe name encoding errors")) #TODO
-        
+
+        parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True,
+                    help=_("don't fixe filesystem name incompatibily")) #TODO
+
         parser.add_option("--no-progress", action="store_false", dest="progress", default=True,
                     help=_("deactivate progress bar"))