/* * fs.readdir */ static int fs_readdir(lua_State* L) { FSR__SETUP const char *path = luaL_checkstring(L, 1); FSR__SET_OPT_CB(2, on_fs_callback) uv_fs_readdir(loop, req, path, 0, cb); FSR__TEARDOWN }
virgo_error_t* virgo__versions_latest_file(virgo_t *v, const char *path, is_file_cmp file_compare, char *buffer, size_t buffer_len) { char *latest = NULL; char *ptr; int rc, i; uv_fs_t readdir_req; virgo_error_t *err; rc = uv_fs_readdir(uv_default_loop(), &readdir_req, path, 0, NULL); if (rc <= 0) { return virgo_error_createf(-1, "readdir returned %u", rc); } ptr = readdir_req.ptr; for (i=0; i < rc; i++) { int comparison; /* Verify this is a bundle filename */ if (!file_compare(ptr)) { goto next; } /* Initial pass */ if (!latest) { latest = ptr; goto next; } /* Perform the comparison */ err = compare_files(ptr, latest, &comparison); if (err) { virgo_error_clear(err); goto next; } /* If comparison returns 1, then the versions are greater */ if (comparison == 1) { latest = ptr; } next: ptr = ptr + strlen(ptr) + 1; } if (!latest) { uv_fs_req_cleanup(&readdir_req); return virgo_error_create(VIRGO_ENOFILE, "zero files"); } /* Save off the path */ snprintf(buffer, buffer_len, "%s%s%s", path, SEP, latest); uv_fs_req_cleanup(&readdir_req); return VIRGO_SUCCESS; }
int main() { loop = uv_default_loop(); int r = uv_fs_readdir(loop, &readdir_req, path, O_RDONLY, readdir_cb); if (r) { fprintf(stderr, "Error at reading directory: %s.\n", uv_strerror(uv_last_error(loop))); return -1; } uv_run(loop, UV_RUN_DEFAULT); return 0; }
DLLEXPORT int jl_readdir(const char* path, uv_fs_t* readdir_req) { // Note that the flags field is mostly ignored by libuv return uv_fs_readdir(uv_default_loop(), readdir_req, path, 0 /*flags*/, NULL); }
extern "C" int rust_uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, uv_fs_cb cb) { return uv_fs_readdir(loop, req, path, flags, cb); }