Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
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
ry committed Sep 2, 2011
1 parent 9f932f9 commit b47fa77
Showing 1 changed file with 43 additions and 247 deletions.
290 changes: 43 additions & 247 deletions src/unix/fs.c
Expand Up @@ -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.

Copy link
@bnoordhuis

bnoordhuis Sep 4, 2011

Contributor

uv_unref(loop) here?

This comment has been minimized.

Copy link
@ry

ry Sep 4, 2011

Author Contributor

yeah, probably - do we have a test that demos an error on a fs event? we must...

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Sep 5, 2011

Contributor

Fixed in d917610. Kind of hard to write a test for, the eio_*() functions only fail on OOM conditions.

} \
} 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,
Expand Down Expand Up @@ -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));
}


Expand Down Expand Up @@ -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))
}


Expand Down Expand Up @@ -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))
}


Expand Down Expand Up @@ -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))
}


Expand Down Expand Up @@ -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))
}


Expand All @@ -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))
}


Expand Down

0 comments on commit b47fa77

Please sign in to comment.