cedric pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=12ee780653f7d4748fa682e3c9bc0401382721b6

commit 12ee780653f7d4748fa682e3c9bc0401382721b6
Author: Ivan Furs <i.f...@samsung.com>
Date:   Thu Jan 12 16:24:31 2017 -0800

    ecore_file: add case to properly rename a file in Windows
    
    Summary:
    If the file with a new path was created and 'rename' wants to replace the 
old path to the new path. 'rename' will return:
       Windows 7: -1 (errno=EEXIST) (EEXIST == 17)
       Ubuntu: 0
    
    **EEXIST**
    **Ubuntu**: The link named by new is a directory that is not an empty 
directory. (https://linux.die.net/man/3/rename)
    **Windows 7**: Files exist. An attempt has been made to create a file that 
already exists. For example, the _O_CREAT and _O_EXCL flags are specified in an 
_open call, but the named file already 
exists.(https://msdn.microsoft.com/en-us/library/5814770t.aspx)
    
    Test Plan:
    **Sample code to rename in Linux and Windows if the file with the new name 
already exists:**
    
    int main()
    {
            const char *_old = "old";
            const char *_new = "new";
    
            int fd1 = open(_old, O_CREAT);
            close(fd1);
            int fd2 = open(_new, O_CREAT);
            close(fd2);
            printf("rename:\t%s -> %s\n", _old, _new);
            int r = rename(_old, _new);
            if (r == 0)
            {
                    printf("GOOD\n");
            }
            else
            {
                    printf("CODE ERROR:\n"                  );
                    printf(" -rename...: %d\n", r    );
                    printf(" -errno....: %d\n", errno);
            }
            return 0;
    }
    
    Reviewers: raster, vtorri, jpeg, NikaWhite, reutskiy.v.v, an.kroitor, cedric
    
    Reviewed By: cedric
    
    Subscribers: artem.popov, cedric, jpeg
    
    Tags: #efl, #windows
    
    Differential Revision: https://phab.enlightenment.org/D4561
    
    Signed-off-by: Cedric BAIL <ced...@osg.samsung.com>
---
 src/lib/ecore_file/ecore_file.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/lib/ecore_file/ecore_file.c b/src/lib/ecore_file/ecore_file.c
index dd53db1..6e341ea 100644
--- a/src/lib/ecore_file/ecore_file.c
+++ b/src/lib/ecore_file/ecore_file.c
@@ -508,6 +508,21 @@ ecore_file_mv(const char *src, const char *dst)
                   goto PASS;
                }
           }
+#ifdef _WIN32
+          if (errno == EEXIST)
+            {
+               struct _stat s;
+               _stat(dst, &s);
+               if (_S_IFREG & s.st_mode)
+               {
+                  ecore_file_unlink(dst);
+                  if (rename(src, dst))
+                    {
+                       return EINA_TRUE;
+                    }
+               }
+            }
+#endif
         goto FAIL;
      }
 

-- 


Reply via email to