more checks for file operations
authorVladimir Nadvornik <nadvornik@suse.cz>
Sat, 26 Jul 2008 09:58:25 +0000 (09:58 +0000)
committerVladimir Nadvornik <nadvornik@suse.cz>
Sat, 26 Jul 2008 09:58:25 +0000 (09:58 +0000)
src/filedata.c
src/typedefs.h

index 5d84883..c14470e 100644 (file)
@@ -1564,34 +1564,90 @@ gboolean file_data_sc_update_ci_unspecified_list(GList *fd_list, const gchar *de
 
 
 /*
- * check dest paths - dest image exists, etc.
+ * verify source and dest paths - dest image exists, etc.
  * it should detect all possible problems with the planned operation
- * FIXME: add more tests
  */
 
 gint file_data_verify_ci(FileData *fd)
 {
        gint ret = CHANGE_OK;
+       gchar *dir;
+       gchar *dest_dir = NULL;
        
        g_assert(fd->change);
-       
-       if (fd->change->dest &&
-           strcmp(fd->change->dest, fd->path) != 0 &&
-           isname(fd->change->dest))
+
+       if (!isname(fd->path))
                {
-               ret |= CHANGE_DEST_EXISTS;
-               DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest);
+               /* this probably should not happen */
+               ret |= CHANGE_NO_SRC;
+               DEBUG_1("Change checked: file does not exist: %s", fd->path);
+               return ret; 
                }
+               
+       dir = remove_level_from_path(fd->path);
        
-       if (!access_file(fd->path, R_OK))
+       if (fd->change->dest) dest_dir = remove_level_from_path(fd->change->dest);
+
+       if (fd->change->type != FILEDATA_CHANGE_DELETE && 
+           !access_file(fd->path, R_OK))
                {
-               ret |= CHANGE_NO_PERM;
+               ret |= CHANGE_NO_READ_PERM;
                DEBUG_1("Change checked: no read permission: %s", fd->path);
                }
+       else if ((fd->change->type == FILEDATA_CHANGE_DELETE || fd->change->type == FILEDATA_CHANGE_MOVE) &&
+           !access_file(dir, W_OK))
+               {
+               ret |= CHANGE_NO_WRITE_PERM_DIR;
+               DEBUG_1("Change checked: source dir is readonly: %s", fd->path);
+               }
+       else if (fd->change->type != FILEDATA_CHANGE_COPY && 
+                fd->change->type != FILEDATA_CHANGE_UNSPECIFIED && 
+                !access_file(fd->path, W_OK)) 
+               {
+               ret |= CHANGE_WARN_NO_WRITE_PERM;
+               DEBUG_1("Change checked: no write permission: %s", fd->path);
+               }
+               
+       if (fd->change->dest && (strcmp(fd->path, fd->change->dest) == 0))
+               {
+               ret |= CHANGE_WARN_SAME;
+               DEBUG_1("Change checked: source and destination is the same: %s", fd->path);
+               }
+       
+       if (fd->change->dest)
+               {
+               if (!isdir(dest_dir))           
+                       {
+                       ret |= CHANGE_NO_DEST_DIR;
+                       DEBUG_1("Change checked: destination dir does not exist: %s -> %s", fd->path, fd->change->dest);
+                       }
+               else if (!access_file(dest_dir, W_OK))
+                       {
+                       ret |= CHANGE_NO_WRITE_PERM_DEST_DIR;
+                       DEBUG_1("Change checked: destination dir is readonly: %s -> %s", fd->path, fd->change->dest);
+                       }
+               else if (isfile(fd->change->dest) && !access_file(fd->change->dest, W_OK) && (strcmp(fd->change->dest, fd->path) != 0))
+                       {
+                       ret |= CHANGE_NO_WRITE_PERM_DEST;
+                       DEBUG_1("Change checked: destination file exists and is readonly: %s -> %s", fd->path, fd->change->dest);
+                       }
+               else if (isfile(fd->change->dest) && (strcmp(fd->change->dest, fd->path) != 0))
+                       {
+                       ret |= CHANGE_WARN_DEST_EXISTS;
+                       DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest);
+                       }
+               else if (isdir(fd->change->dest) && (strcmp(fd->change->dest, fd->path) != 0))
+                       {
+                       ret |= CHANGE_DEST_EXISTS;
+                       DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest);
+                       }
+               }
                
        fd->change->error = ret;
        if (ret == 0) DEBUG_1("Change checked: OK: %s", fd->path);
 
+       g_free(dir);
+       g_free(dest_dir);
        return ret;
 }
 
@@ -1619,16 +1675,64 @@ gchar *file_data_get_error_string(gint error)
 {
        GString *result = g_string_new("");
 
-       if (error & CHANGE_NO_PERM)
+       if (error & CHANGE_NO_SRC)
                {
                if (result->len > 0) g_string_append(result, ", ");
-               g_string_append(result, _("no read permission"));
+               g_string_append(result, _("file or directory does not exist"));
                }
 
        if (error & CHANGE_DEST_EXISTS)
                {
                if (result->len > 0) g_string_append(result, ", ");
-               g_string_append(result, _("destination file already exists and will be overwritten"));
+               g_string_append(result, _("destination already exists"));
+               }
+
+       if (error & CHANGE_NO_WRITE_PERM_DEST)
+               {
+               if (result->len > 0) g_string_append(result, ", ");
+               g_string_append(result, _("destination can't be overwritten"));
+               }
+
+       if (error & CHANGE_NO_WRITE_PERM_DEST_DIR)
+               {
+               if (result->len > 0) g_string_append(result, ", ");
+               g_string_append(result, _("destination directory is not writable"));
+               }
+
+       if (error & CHANGE_NO_DEST_DIR)
+               {
+               if (result->len > 0) g_string_append(result, ", ");
+               g_string_append(result, _("destination directory does not exist"));
+               }
+
+       if (error & CHANGE_NO_WRITE_PERM_DIR)
+               {
+               if (result->len > 0) g_string_append(result, ", ");
+               g_string_append(result, _("source directory is not writable"));
+               }
+
+       if (error & CHANGE_NO_READ_PERM)
+               {
+               if (result->len > 0) g_string_append(result, ", ");
+               g_string_append(result, _("no read permission"));
+               }
+
+       if (error & CHANGE_WARN_NO_WRITE_PERM)
+               {
+               if (result->len > 0) g_string_append(result, ", ");
+               g_string_append(result, _("file is readonly"));
+               }
+
+       if (error & CHANGE_WARN_DEST_EXISTS)
+               {
+               if (result->len > 0) g_string_append(result, ", ");
+               g_string_append(result, _("destination already exists and will be overwritten"));
+               }
+
+       if (error & CHANGE_WARN_SAME)
+               {
+               if (result->len > 0) g_string_append(result, ", ");
+               g_string_append(result, _("source and destination is the same"));
                }
 
        return g_string_free(result, FALSE);
index 65fe08d..49a6598 100644 (file)
@@ -150,13 +150,20 @@ typedef enum {
 } NotifyType;
 
 typedef enum {
-       CHANGE_OK          = 0,
-       CHANGE_DEST_EXISTS = 1 << 0,
-       CHANGE_ERROR_MASK  = (~0) << 1, /* the values below are fatal errors */
-       CHANGE_NO_PERM     = 1 << 1
+       CHANGE_OK                      = 0,
+       CHANGE_WARN_DEST_EXISTS        = 1 << 0,
+       CHANGE_WARN_NO_WRITE_PERM      = 1 << 1,
+       CHANGE_WARN_SAME               = 1 << 2,
+       CHANGE_ERROR_MASK              = (~0) << 3, /* the values below are fatal errors */
+       CHANGE_NO_READ_PERM            = 1 << 3,
+       CHANGE_NO_WRITE_PERM_DIR       = 1 << 4,
+       CHANGE_NO_DEST_DIR             = 1 << 5,
+       CHANGE_NO_WRITE_PERM_DEST_DIR  = 1 << 6,
+       CHANGE_NO_WRITE_PERM_DEST      = 1 << 7,
+       CHANGE_DEST_EXISTS             = 1 << 8,
+       CHANGE_NO_SRC                  = 1 << 9
 } ChangeError;
 
-
 #define MAX_SPLIT_IMAGES 4
 
 typedef struct _ImageLoader ImageLoader;