aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Hoffmann <stefan991@gmail.com>2014-04-08 20:46:42 +0200
committerThiago de Arruda <tpadilha84@gmail.com>2014-04-11 13:31:48 -0300
commit445f31f076325a68d91746bb41ac9ba139144c2d (patch)
treedd05a6d09822572ae152be6b3759696d8ebf6901
parent136e5e5b84469559e72e061b2c6e78db18174a12 (diff)
downloadrneovim-445f31f076325a68d91746bb41ac9ba139144c2d.tar.gz
rneovim-445f31f076325a68d91746bb41ac9ba139144c2d.tar.bz2
rneovim-445f31f076325a68d91746bb41ac9ba139144c2d.zip
port vim_mkdir, mch_rmdir and mch_remove to libuv
-rw-r--r--src/if_cscope.c1
-rw-r--r--src/memfile.c1
-rw-r--r--src/os/fs.c25
-rw-r--r--src/os/os.h15
-rw-r--r--src/os_unix_defs.h11
-rw-r--r--test/unit/os/fs.moon50
6 files changed, 92 insertions, 11 deletions
diff --git a/src/if_cscope.c b/src/if_cscope.c
index e257e4e84b..65155c3421 100644
--- a/src/if_cscope.c
+++ b/src/if_cscope.c
@@ -24,6 +24,7 @@
#include "tag.h"
#include "ui.h"
#include "window.h"
+#include "os/os.h"
#include <sys/types.h>
#include <sys/stat.h>
diff --git a/src/memfile.c b/src/memfile.c
index 7a9b062d58..30a5ed389d 100644
--- a/src/memfile.c
+++ b/src/memfile.c
@@ -45,6 +45,7 @@
#include "os_unix.h"
#include "path.h"
#include "ui.h"
+#include "os/os.h"
/*
* Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
diff --git a/src/os/fs.c b/src/os/fs.c
index a68e66ef00..f9b02375e1 100644
--- a/src/os/fs.c
+++ b/src/os/fs.c
@@ -202,3 +202,28 @@ int os_rename(const char_u *path, const char_u *new_path)
return FAIL;
}
+
+int os_mkdir(const char *path, int32_t mode)
+{
+ uv_fs_t request;
+ int result = uv_fs_mkdir(uv_default_loop(), &request, path, mode, NULL);
+ uv_fs_req_cleanup(&request);
+ return result;
+}
+
+int os_rmdir(const char *path)
+{
+ uv_fs_t request;
+ int result = uv_fs_rmdir(uv_default_loop(), &request, path, NULL);
+ uv_fs_req_cleanup(&request);
+ return result;
+}
+
+int os_remove(const char *path)
+{
+ uv_fs_t request;
+ int result = uv_fs_unlink(uv_default_loop(), &request, path, NULL);
+ uv_fs_req_cleanup(&request);
+ return result;
+}
+
diff --git a/src/os/os.h b/src/os/os.h
index ff21a95f3a..45bec739f5 100644
--- a/src/os/os.h
+++ b/src/os/os.h
@@ -63,6 +63,21 @@ int os_file_is_writable(const char *name);
/// @return `OK` for success, `FAIL` for failure.
int os_rename(const char_u *path, const char_u *new_path);
+/// Make a directory.
+///
+/// @return `0` for success, non-zero for failure.
+int os_mkdir(const char *path, int32_t mode);
+
+/// Remove a directory.
+///
+/// @return `0` for success, non-zero for failure.
+int os_rmdir(const char *path);
+
+/// Remove a file.
+///
+/// @return `0` for success, non-zero for failure.
+int os_remove(const char *path);
+
long_u os_total_mem(int special);
const char *os_getenv(const char *name);
int os_setenv(const char *name, const char *value, int overwrite);
diff --git a/src/os_unix_defs.h b/src/os_unix_defs.h
index 54627b5b7d..621e461030 100644
--- a/src/os_unix_defs.h
+++ b/src/os_unix_defs.h
@@ -30,14 +30,9 @@
# include <sys/param.h> /* defines BSD, if it's a BSD system */
#endif
-/*
- * Sun defines FILE on SunOS 4.x.x, Solaris has a typedef for FILE
- */
-
-/* always use unlink() to remove files */
-# define vim_mkdir(x, y) mkdir((char *)(x), y)
-# define mch_rmdir(x) rmdir((char *)(x))
-# define mch_remove(x) unlink((char *)(x))
+#define vim_mkdir(x, y) os_mkdir((char *)(x), (y))
+#define mch_rmdir(x) os_rmdir((char *)(x))
+#define mch_remove(x) os_remove((char *)(x))
/* The number of arguments to a signal handler is configured here. */
/* It used to be a long list of almost all systems. Any system that doesn't
diff --git a/test/unit/os/fs.moon b/test/unit/os/fs.moon
index c0499fe3dc..1fc3022368 100644
--- a/test/unit/os/fs.moon
+++ b/test/unit/os/fs.moon
@@ -19,6 +19,9 @@ bool os_file_exists(const char_u *name);
bool os_file_is_readonly(char *fname);
int os_file_is_writable(const char *name);
int os_rename(const char_u *path, const char_u *new_path);
+int os_mkdir(const char *path, int32_t mode);
+int os_rmdir(const char *path);
+int os_remove(const char *path);
]]
-- import constants parsed by ffi
@@ -94,10 +97,10 @@ describe 'fs function', ->
eq lfs.currentdir! .. '/unit-test-directory', (ffi.string buffer)
eq OK, result
- describe 'os_isdir', ->
- os_isdir = (name) ->
- fs.os_isdir (to_cstr name)
+ os_isdir = (name) ->
+ fs.os_isdir (to_cstr name)
+ describe 'os_isdir', ->
it 'returns false if an empty string is given', ->
eq false, (os_isdir '')
@@ -237,6 +240,9 @@ describe 'fs function', ->
os_rename = (path, new_path) ->
fs.os_rename (to_cstr path), (to_cstr new_path)
+ os_remove = (path) ->
+ fs.os_remove (to_cstr path)
+
describe 'os_file_exists', ->
it 'returns false when given a non-existing file', ->
eq false, (os_file_exists 'non-existing-file')
@@ -270,3 +276,41 @@ describe 'fs function', ->
file = io.open test, 'r'
eq 'other', (file\read '*all')
file\close!
+
+ describe 'os_remove', ->
+ it 'returns non-zero when given a non-existing file', ->
+ neq 0, (os_remove 'non-existing-file')
+
+ it 'removes the given file and returns 0', ->
+ eq true, (os_file_exists 'unit-test-directory/test.file')
+ eq 0, (os_remove 'unit-test-directory/test.file')
+ eq false, (os_file_exists 'unit-test-directory/test.file')
+
+ describe 'folder operations', ->
+ os_mkdir = (path, mode) ->
+ fs.os_mkdir (to_cstr path), mode
+
+ os_rmdir = (path) ->
+ fs.os_rmdir (to_cstr path)
+
+ describe 'os_mkdir', ->
+ it 'returns non-zero when given a already existing directory', ->
+ mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
+ neq 0, (os_mkdir 'unit-test-directory', mode)
+
+ it 'creates a directory and returns 0', ->
+ mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
+ eq false, (os_isdir 'unit-test-directory/new-dir')
+ eq 0, (os_mkdir 'unit-test-directory/new-dir', mode)
+ eq true, (os_isdir 'unit-test-directory/new-dir')
+ lfs.rmdir 'unit-test-directory/new-dir'
+
+ describe 'os_rmdir', ->
+ it 'returns non_zero when given a non-existing directory', ->
+ neq 0, (os_rmdir 'non-existing-directory')
+
+ it 'removes the given directory and returns 0', ->
+ lfs.mkdir 'unit-test-directory/new-dir'
+ eq 0, (os_rmdir 'unit-test-directory/new-dir', mode)
+ eq false, (os_isdir 'unit-test-directory/new-dir')
+