This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository efm2.

View the commit online.

commit 594004c1faabe610239ca8aadefa56bc6ba11d45
Author: Carsten Haitzler (Rasterman) <ras...@rasterman.com>
AuthorDate: Wed Jun 12 11:52:30 2024 +0100

    clean up path builds and del empty efm dirs
---
 src/backends/default/mv.c | 131 ++++++++++++++++++++++++++--------------------
 1 file changed, 74 insertions(+), 57 deletions(-)

diff --git a/src/backends/default/mv.c b/src/backends/default/mv.c
index be38ea8..6ca7c4c 100644
--- a/src/backends/default/mv.c
+++ b/src/backends/default/mv.c
@@ -48,11 +48,11 @@ main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
 { // mv [src] [dstdir]
   const char  *fname, *home_dir;
   Eina_Strbuf *buf   = NULL;
-  Eina_Strbuf *buf2   = NULL;
   Eina_List   *files = NULL, *l;
   File_Set    *fs;
   size_t       sz;
-  char        *src = "" *dst = NULL;
+  char        *src = "" *srctmpdir = NULL, *srcdir = NULL;
+  char        *dst = NULL, *dsttmpdir = NULL, *dsttmpfile = NULL;
   char         sbuf[PATH_MAX + 256];
   int          ret;
   struct stat  st;
@@ -89,6 +89,7 @@ main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
     }
   if (src) free(src);
   if (dst) free(dst);
+  src = "" = NULL;
 
   config_dir = getenv("E_HOME_DIR");
   home_dir   = getenv("HOME");
@@ -102,12 +103,12 @@ main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
 
   buf = eina_strbuf_new();
   if (!buf) goto err;
-  buf2 = eina_strbuf_new();
-  if (!buf2) goto err;
 
+  // set up status files for the op
   status_begin();
   status_op("mv");
 
+  // build up a list of all files to mv and scan them to find how much
   EINA_LIST_FOREACH(files, l, fs)
   {
     struct stat stsrc, stdst;
@@ -128,23 +129,59 @@ main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
     status_dst(fs->dst);
     ret = stat(fs->dst, &st);
     if (ret != 0)
-      {
+      { // if we can't stat the dstdir then we have issues
         status_error(fs->src, fs->dst, "Cannot stat destination");
         break;
       }
 
-    src_can_write = meta_path_can_write(fs->src);
+    // free up previous loop stuff
+    free(src);
+    free(srctmpdir);
+    free(dst);
+    free(dsttmpdir);
+    free(dsttmpfile);
+    src = "" = dst = dsttmpdir = dsttmpfile = NULL;
+
+    // src and src temp dir
+    src = ""
+    srcdir = ecore_file_dir_get(fs->src);
+    if (!srcdir)
+      {
+        status_error(fs->src, fs->dst, "Cannot get source dir");
+        break;
+      }
+    eina_strbuf_reset(buf);
+    eina_strbuf_append(buf, srcdir);
+    eina_strbuf_append(buf, "/.efm");
+    srctmpdir = strdup(eina_strbuf_string_get(buf));
+    free(srcdir);
+
+    // final dest file path - can we write to the meta there?
     eina_strbuf_reset(buf);
     eina_strbuf_append(buf, fs->dst);
     eina_strbuf_append(buf, "/");
     eina_strbuf_append(buf, fname);
-    dst_can_write = meta_path_can_write(eina_strbuf_string_get(buf));
-
+    dst = strdup(eina_strbuf_string_get(buf));
     eina_strbuf_reset(buf);
     eina_strbuf_append(buf, fs->dst);
     eina_strbuf_append(buf, "/.efm");
+    dsttmpdir = strdup(eina_strbuf_string_get(buf));
+    eina_strbuf_reset(buf);
+    eina_strbuf_append(buf, fs->dst);
+    eina_strbuf_append(buf, "/.efm");
+    eina_strbuf_append(buf, fname);
+    eina_strbuf_append(buf, ".tmp");
+    dsttmpfile = strdup(eina_strbuf_string_get(buf));
+    if ((!src) || (!srctmpdir) || (!dst) || (!dsttmpdir) || (!dsttmpfile)) mem_abort();
+
+    // can we modify the meta files in the src? (like mv/rm them?)
+    src_can_write = meta_path_can_write(src);
+    dst_can_write = meta_path_can_write(dst);
+
+    // mkdir the /dstdir/.efm dir in the dest as we'll use it for tmp files
     prev_umask = umask(0);
-    ret        = mkdir(eina_strbuf_string_get(buf), st.st_mode);
+    // make the dir using the same mode as the parent
+    ret        = mkdir(dsttmpdir, st.st_mode);
     if ((ret != 0) && (errno != EEXIST))
       {
         status_error(fs->src, eina_strbuf_string_get(buf),
@@ -153,30 +190,17 @@ main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
       }
     umask(prev_umask);
 
-    eina_strbuf_reset(buf);
-    eina_strbuf_append(buf, fs->dst);
-    eina_strbuf_append(buf, "/.efm/");
-    eina_strbuf_append(buf, fname);
-    eina_strbuf_append(buf, ".tmp");
-    // mv the file ...
-    if (fs_mv(fs->src, eina_strbuf_string_get(buf), EINA_TRUE))
+    // mv the file to a tmp file: mv /srcdir/filename /dstdir/.efm/filename.tmp
+    if (fs_mv(src, dsttmpfile, EINA_TRUE))
       { // it worked so deal with meta/thumbs
-        const char *dstfile;
-        char       *src_meta, *dst_meta, *dstpath;
-
-        eina_strbuf_reset(buf);
-        eina_strbuf_append(buf, fs->dst);
-        eina_strbuf_append(buf, "/");
-        eina_strbuf_append(buf, fname);
-        dstfile = eina_strbuf_string_get(buf);
-        dstpath = strdup(dstfile);
+        char *src_meta, *dst_meta;
 
         // metadata file for the base target file
-        if (src_can_write) src_meta = meta_path_find(fs->src, "meta.efm");
-        else src_meta = meta_path_user_find(fs->src, "meta.efm");
-        if (dst_can_write) dst_meta = meta_path_find(dstfile, "meta.efm");
-        else dst_meta = meta_path_user_find(dstfile, "meta.efm");
-        if ((src_meta) && (dst_meta) && (meta_path_prepare(dstfile)))
+        if (src_can_write) src_meta = meta_path_find(src, "meta.efm");
+        else src_meta = meta_path_user_find(src, "meta.efm");
+        if (dst_can_write) dst_meta = meta_path_find(dst, "meta.efm");
+        else dst_meta = meta_path_user_find(dst, "meta.efm");
+        if ((src_meta) && (dst_meta) && (meta_path_prepare(dst)))
           {
             status_count(1, src_meta);
             fs_mv(src_meta, dst_meta, EINA_FALSE);
@@ -192,23 +216,18 @@ main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
         free(dst_meta);
 
         // thumbnail file for the base target file
-        if (src_can_write) src_meta = meta_path_find(fs->src, "thumb.efm");
-        else src_meta = meta_path_user_find(fs->src, "thumb.efm");
-        if (dst_can_write) dst_meta = meta_path_find(dstfile, "thumb.efm");
-        else dst_meta = meta_path_user_find(dstfile, "thumb.efm");
+        if (src_can_write) src_meta = meta_path_find(src, "thumb.efm");
+        else src_meta = meta_path_user_find(src, "thumb.efm");
+        if (dst_can_write) dst_meta = meta_path_find(dst, "thumb.efm");
+        else dst_meta = meta_path_user_find(dst, "thumb.efm");
 
-        if ((src_meta) && (dst_meta) && (meta_path_prepare(dstfile)))
+        if ((src_meta) && (dst_meta) && (meta_path_prepare(dst)))
           {
             status_count(1, src_meta);
             fs_mv(src_meta, dst_meta, EINA_FALSE);
             // fix up the oprig stat info to match so thub is valid
-            eina_strbuf_reset(buf);
-            eina_strbuf_append(buf, fs->dst);
-            eina_strbuf_append(buf, "/.efm/");
-            eina_strbuf_append(buf, fname);
-            eina_strbuf_append(buf, ".tmp");
             // get stat info of new mv'd file
-            if (stat(eina_strbuf_string_get(buf), &st) == 0)
+            if (stat(dsttmpfile, &st) == 0)
               {
                 Eet_File *ef;
                 unsigned char statsha1[21];
@@ -219,7 +238,7 @@ main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
                 // open the thumb for read+write
                 ef = eet_open(dst_meta, EET_FILE_MODE_READ_WRITE);
                 // update orig path just to be nice - not used for validity
-                eet_write(ef, "orig/path", dstpath, strlen(dstpath),
+                eet_write(ef, "orig/path", dst, strlen(dst),
                           EET_COMPRESSION_LOW);
                 // write new stat info so it's valid
                 eet_write(ef, "orig/stat/sha1", statsha1, 20,
@@ -229,35 +248,33 @@ main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
           }
         free(src_meta);
         free(dst_meta);
-        free(dstpath);
 
         // rename atomically the tmp mv'd file to its dest now thumb and meta
-        // are ok/valid
-        eina_strbuf_reset(buf);
-        eina_strbuf_append(buf, fs->dst);
-        eina_strbuf_append(buf, "/.efm/");
-        eina_strbuf_append(buf, fname);
-        eina_strbuf_append(buf, ".tmp");
-        eina_strbuf_reset(buf2);
-        eina_strbuf_append(buf2, fs->dst);
-        eina_strbuf_append(buf2, "/");
-        eina_strbuf_append(buf2, fname);
-        ret = rename(eina_strbuf_string_get(buf), eina_strbuf_string_get(buf2));
+        // are ok/valid. i.e.
+        // mv /dstdir/.efm/filename.tmp /distdir/filename
+        ret = rename(dsttmpfile, dst);
         if (ret != 0)
           {
-            status_error(eina_strbuf_string_get(buf),
-                         eina_strbuf_string_get(buf2), "Cannot rename");
+            status_error(dsttmpfile, dst, "Cannot rename");
             break;
           }
       }
     else break;
+    // it all worked - let's clean up possibly empty tmp/efm dirs
+    rmdir(srctmpdir);
+    rmdir(dsttmpdir);
   }
 err2:
   status_end();
+  // free up leftover strings from loop
+  free(src);
+  free(srctmpdir);
+  free(dst);
+  free(dsttmpdir);
+  free(dsttmpfile);
 
 err:
   if (buf) eina_strbuf_free(buf);
-  if (buf2) eina_strbuf_free(buf2);
   EINA_LIST_FREE(files, fs)
   {
     eina_stringshare_del(fs->src);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to