int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { ssize_t size; int status; char* buf; status = -1; uv_fs_req_init(loop, req, UV_FS_READLINK, path, cb); if (cb) { if ((req->eio = eio_readlink(path, EIO_PRI_DEFAULT, uv__fs_after, req))) { uv_ref(loop); return 0; } else { uv_err_new(loop, ENOMEM); return -1; } } else { /* pathconf(_PC_PATH_MAX) may return -1 to signify that path * lengths have no upper limit or aren't suitable for malloc'ing. */ if ((size = pathconf(path, _PC_PATH_MAX)) == -1) { #if defined(PATH_MAX) size = PATH_MAX; #else size = 4096; #endif } if ((buf = malloc(size + 1)) == NULL) { uv_err_new(loop, ENOMEM); return -1; } if ((size = readlink(path, buf, size)) == -1) { req->errorno = errno; req->result = -1; free(buf); } else { /* Cannot conceivably fail since it shrinks the buffer. */ buf = realloc(buf, size + 1); buf[size] = '\0'; req->result = 0; req->ptr = buf; } return 0; } assert(0 && "unreachable"); }
void eio_test() { /* avoid relative paths yourself(!) */ printf("step: %d\n", eio_test_step); int s = eio_test_step++; switch(s) { case 0: eio_mkdir("/tmp/eio-test-dir", 0777, 0, res_cb, "mkdir"); break; case 1: { eio_nop(0, res_cb, "nop"); }; break; case 2: eio_stat("/tmp/eio-test-dir", 0, stat_cb, "stat"); break; case 3: eio_lstat("/tmp/eio-test-dir", 0, stat_cb, "stat"); break; case 4: eio_open("/tmp/eio-test-dir/eio-test-file", O_RDWR | O_CREAT, 0777, 0, open_cb, "open"); break; case 5: eio_symlink("test", "/tmp/eio-test-dir/eio-symlink", 0, res_cb, "symlink"); break; case 6: eio_mknod("/tmp/eio-test-dir/eio-fifo", S_IFIFO, 0, 0, res_cb, "mknod"); break; case 7: eio_utime("/tmp/eio-test-dir", 12345.678, 23456.789, 0, res_cb, "utime"); break; case 8: eio_futime(last_fd, 92345.678, 93456.789, 0, res_cb, "futime"); break; case 9: eio_chown("/tmp/eio-test-dir", getuid(), getgid(), 0, res_cb, "chown"); break; case 10: eio_fchown(last_fd, getuid(), getgid(), 0, res_cb, "fchown"); break; case 11: eio_fchmod(last_fd, 0723, 0, res_cb, "fchmod"); break; case 12: eio_readdir("/tmp/eio-test-dir", 0, 0, readdir_cb, "readdir"); break; case 13: eio_readdir("/nonexistant", 0, 0, readdir_cb, "readdir"); break; case 14: eio_fstat(last_fd, 0, stat_cb, "stat"); break; case 15: { eio_write(last_fd, "test\nfail\n", 10, 4, 0, res_cb, "write"); }; break; case 16: eio_read(last_fd, 0, 8, 0, EIO_PRI_DEFAULT, read_cb, "read"); break; case 17: eio_readlink("/tmp/eio-test-dir/eio-symlink", 0, res_cb, "readlink"); break; case 18: eio_dup2(1, 2, EIO_PRI_DEFAULT, res_cb, "dup"); break; // dup stdout to stderr case 19: eio_chmod("/tmp/eio-test-dir", 0765, 0, res_cb, "chmod"); break; case 20: eio_ftruncate(last_fd, 9, 0, res_cb, "ftruncate"); break; case 21: eio_fdatasync(last_fd, 0, res_cb, "fdatasync"); break; case 22: eio_fsync(last_fd, 0, res_cb, "fsync"); break; case 23: eio_sync(0, res_cb, "sync"); break; case 24: eio_busy(0.5, 0, res_cb, "busy"); break; case 25: eio_sendfile(1, last_fd, 4, 5, 0, res_cb, "sendfile"); break; // write "test\n" to stdout case 26: eio_fstat(last_fd, 0, stat_cb, "stat"); break; case 27: eio_truncate("/tmp/eio-test-dir/eio-test-file", 6, 0, res_cb, "truncate"); break; case 28: eio_readahead(last_fd, 0, 64, 0, res_cb, "readahead"); break; case 29: eio_close(last_fd, 0, res_cb, "close"); break; case 30: eio_link("/tmp/eio-test-dir/eio-test-file", "/tmp/eio-test-dir/eio-test-file-2", 0, res_cb, "link"); break; case 31: eio_rename("/tmp/eio-test-dir/eio-test-file", "/tmp/eio-test-dir/eio-test-file-renamed", 0, res_cb, "rename"); break; case 32: eio_unlink("/tmp/eio-test-dir/eio-fifo", 0, res_cb, "unlink"); break; case 33: eio_unlink("/tmp/eio-test-dir/eio-symlink", 0, res_cb, "unlink"); break; case 34: eio_unlink("/tmp/eio-test-dir/eio-test-file-2", 0, res_cb, "unlink"); break; case 35: eio_unlink("/tmp/eio-test-dir/eio-test-file-renamed", 0, res_cb, "unlink"); break; case 36: eio_rmdir("/tmp/eio-test-dir", 0, res_cb, "rmdir"); break; } }
int main (void) { printf ("pipe ()\n"); if (pipe (respipe)) abort (); printf ("eio_init ()\n"); if (eio_init (want_poll, done_poll)) abort (); do { /* avoid relative paths yourself(!) */ eio_mkdir ("eio-test-dir", 0777, 0, res_cb, "mkdir"); eio_nop (0, res_cb, "nop"); event_loop (); eio_stat ("eio-test-dir", 0, stat_cb, "stat"); eio_lstat ("eio-test-dir", 0, stat_cb, "stat"); eio_open ("eio-test-dir/eio-test-file", O_RDWR | O_CREAT, 0777, 0, open_cb, "open"); eio_symlink ("test", "eio-test-dir/eio-symlink", 0, res_cb, "symlink"); eio_mknod ("eio-test-dir/eio-fifo", S_IFIFO, 0, 0, res_cb, "mknod"); event_loop (); eio_utime ("eio-test-dir", 12345.678, 23456.789, 0, res_cb, "utime"); eio_futime (last_fd, 92345.678, 93456.789, 0, res_cb, "futime"); eio_chown ("eio-test-dir", getuid (), getgid (), 0, res_cb, "chown"); eio_fchown (last_fd, getuid (), getgid (), 0, res_cb, "fchown"); eio_fchmod (last_fd, 0723, 0, res_cb, "fchmod"); eio_readdir ("eio-test-dir", 0, 0, readdir_cb, "readdir"); eio_readdir ("/nonexistant", 0, 0, readdir_cb, "readdir"); eio_fstat (last_fd, 0, stat_cb, "stat"); eio_write (last_fd, "test\nfail\n", 10, 4, 0, res_cb, "write"); event_loop (); eio_read (last_fd, 0, 8, 0, EIO_PRI_DEFAULT, read_cb, "read"); eio_readlink ("eio-test-dir/eio-symlink", 0, res_cb, "readlink"); event_loop (); eio_dup2 (1, 2, EIO_PRI_DEFAULT, res_cb, "dup"); // dup stdout to stderr eio_chmod ("eio-test-dir", 0765, 0, res_cb, "chmod"); eio_ftruncate (last_fd, 9, 0, res_cb, "ftruncate"); eio_fdatasync (last_fd, 0, res_cb, "fdatasync"); eio_fsync (last_fd, 0, res_cb, "fsync"); eio_sync (0, res_cb, "sync"); eio_busy (0.5, 0, res_cb, "busy"); event_loop (); eio_sendfile (1, last_fd, 4, 5, 0, res_cb, "sendfile"); // write "test\n" to stdout eio_fstat (last_fd, 0, stat_cb, "stat"); event_loop (); eio_truncate ("eio-test-dir/eio-test-file", 6, 0, res_cb, "truncate"); eio_readahead (last_fd, 0, 64, 0, res_cb, "readahead"); event_loop (); eio_close (last_fd, 0, res_cb, "close"); eio_link ("eio-test-dir/eio-test-file", "eio-test-dir/eio-test-file-2", 0, res_cb, "link"); event_loop (); eio_rename ("eio-test-dir/eio-test-file", "eio-test-dir/eio-test-file-renamed", 0, res_cb, "rename"); event_loop (); eio_unlink ("eio-test-dir/eio-fifo", 0, res_cb, "unlink"); eio_unlink ("eio-test-dir/eio-symlink", 0, res_cb, "unlink"); eio_unlink ("eio-test-dir/eio-test-file-2", 0, res_cb, "unlink"); eio_unlink ("eio-test-dir/eio-test-file-renamed", 0, res_cb, "unlink"); event_loop (); eio_rmdir ("eio-test-dir", 0, res_cb, "rmdir"); event_loop (); } while (0); return 0; }