Skip to content

Instantly share code, notes, and snippets.

@heapwolf
Created December 29, 2014 13:36
Show Gist options
  • Save heapwolf/b3099eccf0a775bcd4b1 to your computer and use it in GitHub Desktop.
Save heapwolf/b3099eccf0a775bcd4b1 to your computer and use it in GitHub Desktop.
uv_fs_t stat_req;
uv_fs_t open_req;
uv_fs_t read_req;
uv_fs_t close_req;
int statR = uv_fs_stat(UV_LOOP, &stat_req, path, NULL);
const uv_stat_t* stat = static_cast<const uv_stat_t*>(stat_req.ptr);
int fd = uv_fs_open(UV_LOOP, &open_req, path, opts.flags, opts.mode, NULL);
int const size = (int) stat->st_size;
int offset = 0;
uv_buf_t buf;
buf.base = (char *) malloc(size);
buf.len = size;
int readR = uv_fs_read(UV_LOOP, &read_req, fd, &buf, 1, offset, NULL);
int closeR = uv_fs_close(UV_LOOP, &close_req, fd, NULL);
// this is always true
cout << bool(buf.len == size && buf.len == readR && size == readR) << endl;
cout << bool(strcmp(s.c_str(), buf.base)) << endl;
uv_fs_req_cleanup(&stat_req);
uv_fs_req_cleanup(&read_req);
uv_fs_req_cleanup(&open_req);
uv_fs_req_cleanup(&close_req);
return buf;
@heapwolf
Copy link
Author

The string s and buf.base are not always the same for some reason... but the stat's size, buf.len and number of bytes read by uv_fs_read are always equal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment