dav/system.c

changeset 411
a182e503617b
parent 410
0b9bea2d7283
child 412
dc74f736aea1
--- a/dav/system.c	Fri Jun 08 19:58:17 2018 +0200
+++ b/dav/system.c	Sun Jun 10 12:35:00 2018 +0200
@@ -42,21 +42,39 @@
 /* ---------- POSIX implementation ---------- */
 
 SYS_DIR sys_opendir(const char *path) {
-    return opendir(path);
+    DIR *dir = opendir(path);
+    if(!dir) {
+        return NULL;
+    }
+    SysDir *d = malloc(sizeof(SysDir));
+    d->dir = dir;
+    d->ent = NULL;
+    return d;
 }
 
 SysDirEnt* sys_readdir(SYS_DIR dir) {
-    struct dirent *ent = readdir(dir);
+    if(dir->ent) {
+        free(dir->ent->name);
+        free(dir->ent);
+        dir->ent = NULL;
+    }
+    struct dirent *ent = readdir(dir->dir);
     if(ent) {
         SysDirEnt *e = malloc(sizeof(SysDirEnt));
         e->name = strdup(ent->d_name);
+        dir->ent = e;
         return e;
     }
     return NULL;
 }
 
 void sys_closedir(SYS_DIR dir) {
-    closedir(dir);
+    closedir(dir->dir);
+    if(dir->ent) {
+        free(dir->ent->name);
+        free(dir->ent);
+    }
+    free(dir);
 }
 
 FILE* sys_fopen(const char *path, const char *mode) {
@@ -67,6 +85,10 @@
     return stat(path, s);
 }
 
+int sys_rename(const char *oldpath, const char *newpath) {
+    return rename(oldpath, newpath);
+}
+
 
 #else
 /* ---------- Windows implementation ---------- */
@@ -120,10 +142,16 @@
         free(dir);
         return NULL;
     }
+    dir->ent = NULL;
     return dir;
 }
 
 SysDirEnt* sys_readdir(SYS_DIR dir) {
+    if(dir->ent) {
+        free(dir->ent->name);
+        free(dir->ent);
+        dir->ent = NULL;
+    }
     if(dir->first) {
         dir->first = 0;
     } else {
@@ -148,6 +176,7 @@
         name[nlen] = 0;
         SysDirEnt *ent = malloc(sizeof(SysDirEnt));
         ent->name = name;
+        dir->ent = ent;
         return ent;
     } else {
         return NULL;
@@ -155,6 +184,10 @@
 }
 
 void sys_closedir(SYS_DIR dir) {
+    if(dir->ent) {
+        free(dir->ent->name);
+        free(dir->ent);
+    }
     FindClose(dir->handle);
     free(dir);
 }
@@ -180,4 +213,24 @@
     return ret;
 }
 
+int sys_rename(const char *oldpath, const char *newpath) {
+    wchar_t *o = path2winpath(oldpath, FALSE, NULL);
+    wchara_t *n = path2winpath(newpath, FALSE, NULL);
+    if(!o || !n) {
+        return -1;
+    }
+    
+    struct __stat64 s;
+    if(!_wstat64(n, &s)) {
+        if(_wunlink(n)) {
+            fprintf(stderr, "sys_rename: cannot delete existing file: %ls\n", n);
+        }
+    }
+    
+    int ret = _wrename(o, n);
+    free(o);
+    free(n);
+    return ret;
+}
+
 #endif
\ No newline at end of file

mercurial