This repository has been archived by the owner on May 4, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
unix/fs.c: Apply macro magic, implement symlink, link, chown, fchown
- Loading branch information
Showing
1 changed file
with
43 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,33 @@ | |
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <utime.h> | ||
|
||
|
||
#define ARGS1(a) (a) | ||
#define ARGS2(a,b) (a), (b) | ||
#define ARGS3(a,b,c) (a), (b), (c) | ||
#define ARGS4(a,b,c,d) (a), (b), (c), (d) | ||
|
||
#define WRAP_EIO(type, eiofunc, func, args) \ | ||
uv_fs_req_init(loop, req, type, cb); \ | ||
if (cb) { \ | ||
/* async */ \ | ||
uv_ref(loop); \ | ||
req->eio = eiofunc(args, EIO_PRI_DEFAULT, uv__fs_after, req); \ | ||
if (!req->eio) { \ | ||
uv_err_new(loop, ENOMEM); \ | ||
return -1; \ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ry
Author
Contributor
|
||
} \ | ||
} else { \ | ||
/* sync */ \ | ||
req->result = func(args); \ | ||
if (req->result) { \ | ||
uv_err_new(loop, errno); \ | ||
return -1; \ | ||
} \ | ||
} \ | ||
return 0; | ||
|
||
|
||
static void uv_fs_req_init(uv_loop_t* loop, uv_fs_t* req, uv_fs_type fs_type, | ||
|
@@ -122,24 +149,7 @@ static int uv__fs_after(eio_req* eio) { | |
|
||
|
||
int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_CLOSE, cb); | ||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_close(file, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
} else { | ||
/* sync */ | ||
if ((req->result = uv__close(file))) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_CLOSE, eio_close, close, ARGS1(file)); | ||
} | ||
|
||
|
||
|
@@ -203,28 +213,7 @@ int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf, | |
|
||
|
||
int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_UNLINK, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_unlink(path, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = unlink(path); | ||
|
||
if (req->result) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_UNLINK, eio_unlink, unlink, ARGS1(path)) | ||
} | ||
|
||
|
||
|
@@ -260,54 +249,12 @@ int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf, | |
|
||
int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode, | ||
uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_MKDIR, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_mkdir(path, mode, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = mkdir(path, mode); | ||
|
||
if (req->result < 0) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_MKDIR, eio_mkdir, mkdir, ARGS2(path, mode)) | ||
} | ||
|
||
|
||
int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_RMDIR, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_rmdir(path, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = rmdir(path); | ||
|
||
if (req->result < 0) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_RMDIR, eio_rmdir, rmdir, ARGS1(path)) | ||
} | ||
|
||
|
||
|
@@ -436,162 +383,36 @@ int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { | |
|
||
int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path, | ||
uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_RENAME, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_rename(path, new_path, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = rename(path, new_path); | ||
|
||
if (req->result) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_RENAME, eio_rename, rename, ARGS2(path, new_path)) | ||
} | ||
|
||
|
||
int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_FSYNC, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_fsync(file, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = fsync(file); | ||
|
||
if (req->result) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_FSYNC, eio_fsync, fsync, ARGS1(file)) | ||
} | ||
|
||
|
||
int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_FDATASYNC, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_fdatasync(file, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = fdatasync(file); | ||
|
||
if (req->result) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_FDATASYNC, eio_fdatasync, fdatasync, ARGS1(file)) | ||
} | ||
|
||
|
||
int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file, off_t offset, | ||
uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_FTRUNCATE, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_ftruncate(file, offset, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = ftruncate(file, offset); | ||
|
||
if (req->result) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_FTRUNCATE, eio_ftruncate, ftruncate, ARGS2(file, offset)) | ||
} | ||
|
||
|
||
int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd, uv_file in_fd, | ||
off_t in_offset, size_t length, uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_SENDFILE, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_sendfile(out_fd, in_fd, in_offset, length, EIO_PRI_DEFAULT, | ||
uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = eio_sendfile_sync(out_fd, in_fd, in_offset, length); | ||
|
||
if (req->result) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_SENDFILE, eio_sendfile, eio_sendfile_sync, | ||
ARGS4(out_fd, in_fd, in_offset, length)) | ||
} | ||
|
||
|
||
int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode, | ||
uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_CHMOD, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_chmod(path, mode, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = chmod(path, mode); | ||
|
||
if (req->result) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_CHMOD, eio_chmod, chmod, ARGS2(path, mode)) | ||
} | ||
|
||
|
||
|
@@ -657,15 +478,13 @@ int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { | |
|
||
int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path, | ||
const char* new_path, uv_fs_cb cb) { | ||
assert(0 && "implement me"); | ||
return -1; | ||
WRAP_EIO(UV_FS_LINK, eio_link, link, ARGS2(path, new_path)) | ||
} | ||
|
||
|
||
int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path, | ||
const char* new_path, uv_fs_cb cb) { | ||
assert(0 && "implement me"); | ||
return -1; | ||
WRAP_EIO(UV_FS_SYMLINK, eio_symlink, symlink, ARGS2(path, new_path)) | ||
} | ||
|
||
|
||
|
@@ -678,42 +497,19 @@ int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path, | |
|
||
int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file, int mode, | ||
uv_fs_cb cb) { | ||
uv_fs_req_init(loop, req, UV_FS_FCHMOD, cb); | ||
|
||
if (cb) { | ||
/* async */ | ||
uv_ref(loop); | ||
req->eio = eio_fchmod(file, mode, EIO_PRI_DEFAULT, uv__fs_after, req); | ||
if (!req->eio) { | ||
uv_err_new(loop, ENOMEM); | ||
return -1; | ||
} | ||
|
||
} else { | ||
/* sync */ | ||
req->result = fchmod(file, mode); | ||
|
||
if (req->result) { | ||
uv_err_new(loop, errno); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
WRAP_EIO(UV_FS_FCHMOD, eio_fchmod, fchmod, ARGS2(file, mode)) | ||
} | ||
|
||
|
||
int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path, int uid, | ||
int gid, uv_fs_cb cb) { | ||
assert(0 && "implement me"); | ||
return -1; | ||
WRAP_EIO(UV_FS_CHOWN, eio_chown, chown, ARGS3(path, uid, gid)) | ||
} | ||
|
||
|
||
int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file, int uid, int gid, | ||
uv_fs_cb cb) { | ||
assert(0 && "implement me"); | ||
return -1; | ||
WRAP_EIO(UV_FS_FCHOWN, eio_fchown, fchown, ARGS3(file, uid, gid)) | ||
} | ||
|
||
|
||
|
uv_unref(loop)
here?