123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- /* Copyright libuv project contributors. All rights reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
- #ifdef _WIN32
- #include "uv.h"
- #include "task.h"
- #if defined(__unix__) || defined(__POSIX__) || \
- defined(__APPLE__) || defined(__sun) || \
- defined(_AIX) || defined(__MVS__) || \
- defined(__HAIKU__)
- # include <unistd.h> /* unlink, rmdir */
- #else
- # include <direct.h>
- # define rmdir _rmdir
- # define unlink _unlink
- #endif
- static int flags;
- static uv_fs_t close_req;
- static uv_fs_t mkdir_req;
- static uv_fs_t open_req;
- static uv_fs_t read_req;
- static uv_fs_t rmdir_req;
- static uv_fs_t unlink_req;
- static uv_fs_t write_req;
- static char buf[32];
- static uv_buf_t iov;
- /* Opening the same file multiple times quickly can cause uv_fs_open to fail
- * with EBUSY, so append an identifier to the file name for each operation */
- static int sid = 0;
- #define FILE_NAME_SIZE 128
- static char absent_file[FILE_NAME_SIZE];
- static char empty_file[FILE_NAME_SIZE];
- static char dummy_file[FILE_NAME_SIZE];
- static char empty_dir[] = "empty_dir";
- static void setup() {
- int r;
- /* empty_dir */
- r = uv_fs_rmdir(NULL, &rmdir_req, empty_dir, NULL);
- ASSERT(r == 0 || r == UV_ENOENT);
- ASSERT(rmdir_req.result == 0 || rmdir_req.result == UV_ENOENT);
- uv_fs_req_cleanup(&rmdir_req);
- r = uv_fs_mkdir(NULL, &mkdir_req, empty_dir, 0755, NULL);
- ASSERT(r == 0);
- ASSERT(mkdir_req.result == 0);
- uv_fs_req_cleanup(&mkdir_req);
- }
- static void refresh() {
- int r;
- /* absent_file */
- sprintf(absent_file, "test_file_%d", sid++);
- r = uv_fs_unlink(NULL, &unlink_req, absent_file, NULL);
- ASSERT(r == 0 || r == UV_ENOENT);
- ASSERT(unlink_req.result == 0 || unlink_req.result == UV_ENOENT);
- uv_fs_req_cleanup(&unlink_req);
- /* empty_file */
- sprintf(empty_file, "test_file_%d", sid++);
- r = uv_fs_open(NULL, &open_req, empty_file,
- UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_WRONLY, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req.result >= 0);
- uv_fs_req_cleanup(&open_req);
- r = uv_fs_close(NULL, &close_req, open_req.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* dummy_file */
- sprintf(dummy_file, "test_file_%d", sid++);
- r = uv_fs_open(NULL, &open_req, dummy_file,
- UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_WRONLY, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req.result >= 0);
- uv_fs_req_cleanup(&open_req);
- iov = uv_buf_init("a", 1);
- r = uv_fs_write(NULL, &write_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == 1);
- ASSERT(write_req.result == 1);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- }
- static void cleanup() {
- unlink(absent_file);
- unlink(empty_file);
- unlink(dummy_file);
- }
- static void openFail(char *file, int error) {
- int r;
- refresh();
- r = uv_fs_open(NULL, &open_req, file, flags, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r == error);
- ASSERT(open_req.result == error);
- uv_fs_req_cleanup(&open_req);
- /* Ensure the first call does not create the file */
- r = uv_fs_open(NULL, &open_req, file, flags, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r == error);
- ASSERT(open_req.result == error);
- uv_fs_req_cleanup(&open_req);
- cleanup();
- }
- static void refreshOpen(char *file) {
- int r;
- refresh();
- r = uv_fs_open(NULL, &open_req, file, flags, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req.result >= 0);
- uv_fs_req_cleanup(&open_req);
- }
- static void writeExpect(char *file, char *expected, int size) {
- int r;
- refreshOpen(file);
- iov = uv_buf_init("b", 1);
- r = uv_fs_write(NULL, &write_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == 1);
- ASSERT(write_req.result == 1);
- uv_fs_req_cleanup(&write_req);
- iov = uv_buf_init("c", 1);
- r = uv_fs_write(NULL, &write_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == 1);
- ASSERT(write_req.result == 1);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Check contents */
- r = uv_fs_open(NULL, &open_req, file, UV_FS_O_RDONLY, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req.result >= 0);
- uv_fs_req_cleanup(&open_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == size);
- ASSERT(read_req.result == size);
- ASSERT(strncmp(buf, expected, size) == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- cleanup();
- }
- static void writeFail(char *file, int error) {
- int r;
- refreshOpen(file);
- iov = uv_buf_init("z", 1);
- r = uv_fs_write(NULL, &write_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == error);
- ASSERT(write_req.result == error);
- uv_fs_req_cleanup(&write_req);
- iov = uv_buf_init("z", 1);
- r = uv_fs_write(NULL, &write_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == error);
- ASSERT(write_req.result == error);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- cleanup();
- }
- static void readExpect(char *file, char *expected, int size) {
- int r;
- refreshOpen(file);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == size);
- ASSERT(read_req.result == size);
- ASSERT(strncmp(buf, expected, size) == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- cleanup();
- }
- static void readFail(char *file, int error) {
- int r;
- refreshOpen(file);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == error);
- ASSERT(read_req.result == error);
- uv_fs_req_cleanup(&read_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req.result, &iov, 1, -1, NULL);
- ASSERT(r == error);
- ASSERT(read_req.result == error);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- cleanup();
- }
- static void fs_open_flags(int add_flags) {
- /* Follow the order from
- * https://github.com/nodejs/node/blob/1a96abe849/lib/internal/fs/utils.js#L329-L354
- */
- /* r */
- flags = add_flags | UV_FS_O_RDONLY;
- openFail(absent_file, UV_ENOENT);
- writeFail(empty_file, UV_EPERM);
- readExpect(empty_file, "", 0);
- writeFail(dummy_file, UV_EPERM);
- readExpect(dummy_file, "a", 1);
- writeFail(empty_dir, UV_EPERM);
- readFail(empty_dir, UV_EISDIR);
- /* rs */
- flags = add_flags | UV_FS_O_RDONLY | UV_FS_O_SYNC;
- openFail(absent_file, UV_ENOENT);
- writeFail(empty_file, UV_EPERM);
- readExpect(empty_file, "", 0);
- writeFail(dummy_file, UV_EPERM);
- readExpect(dummy_file, "a", 1);
- writeFail(empty_dir, UV_EPERM);
- readFail(empty_dir, UV_EISDIR);
- /* r+ */
- flags = add_flags | UV_FS_O_RDWR;
- openFail(absent_file, UV_ENOENT);
- writeExpect(empty_file, "bc", 2);
- readExpect(empty_file, "", 0);
- writeExpect(dummy_file, "bc", 2);
- readExpect(dummy_file, "a", 1);
- writeFail(empty_dir, UV_EISDIR);
- readFail(empty_dir, UV_EISDIR);
- /* rs+ */
- flags = add_flags | UV_FS_O_RDWR | UV_FS_O_SYNC;
- openFail(absent_file, UV_ENOENT);
- writeExpect(empty_file, "bc", 2);
- readExpect(empty_file, "", 0);
- writeExpect(dummy_file, "bc", 2);
- readExpect(dummy_file, "a", 1);
- writeFail(empty_dir, UV_EISDIR);
- readFail(empty_dir, UV_EISDIR);
- /* w */
- flags = add_flags | UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_WRONLY;
- writeExpect(absent_file, "bc", 2);
- readFail(absent_file, UV_EPERM);
- writeExpect(empty_file, "bc", 2);
- readFail(empty_file, UV_EPERM);
- writeExpect(dummy_file, "bc", 2);
- readFail(dummy_file, UV_EPERM);
- openFail(empty_dir, UV_EISDIR);
- /* wx */
- flags = add_flags | UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_WRONLY |
- UV_FS_O_EXCL;
- writeExpect(absent_file, "bc", 2);
- readFail(absent_file, UV_EPERM);
- openFail(empty_file, UV_EEXIST);
- openFail(dummy_file, UV_EEXIST);
- openFail(empty_dir, UV_EEXIST);
- /* w+ */
- flags = add_flags | UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_RDWR;
- writeExpect(absent_file, "bc", 2);
- readExpect(absent_file, "", 0);
- writeExpect(empty_file, "bc", 2);
- readExpect(empty_file, "", 0);
- writeExpect(dummy_file, "bc", 2);
- readExpect(dummy_file, "", 0);
- openFail(empty_dir, UV_EISDIR);
- /* wx+ */
- flags = add_flags | UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_RDWR |
- UV_FS_O_EXCL;
- writeExpect(absent_file, "bc", 2);
- readExpect(absent_file, "", 0);
- openFail(empty_file, UV_EEXIST);
- openFail(dummy_file, UV_EEXIST);
- openFail(empty_dir, UV_EEXIST);
- /* a */
- flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_WRONLY;
- writeExpect(absent_file, "bc", 2);
- readFail(absent_file, UV_EPERM);
- writeExpect(empty_file, "bc", 2);
- readFail(empty_file, UV_EPERM);
- writeExpect(dummy_file, "abc", 3);
- readFail(dummy_file, UV_EPERM);
- writeFail(empty_dir, UV_EISDIR);
- readFail(empty_dir, UV_EPERM);
- /* ax */
- flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_WRONLY |
- UV_FS_O_EXCL;
- writeExpect(absent_file, "bc", 2);
- readFail(absent_file, UV_EPERM);
- openFail(empty_file, UV_EEXIST);
- openFail(dummy_file, UV_EEXIST);
- openFail(empty_dir, UV_EEXIST);
- /* as */
- flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_WRONLY |
- UV_FS_O_SYNC;
- writeExpect(absent_file, "bc", 2);
- readFail(absent_file, UV_EPERM);
- writeExpect(empty_file, "bc", 2);
- readFail(empty_file, UV_EPERM);
- writeExpect(dummy_file, "abc", 3);
- readFail(dummy_file, UV_EPERM);
- writeFail(empty_dir, UV_EISDIR);
- readFail(empty_dir, UV_EPERM);
- /* a+ */
- flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_RDWR;
- writeExpect(absent_file, "bc", 2);
- readExpect(absent_file, "", 0);
- writeExpect(empty_file, "bc", 2);
- readExpect(empty_file, "", 0);
- writeExpect(dummy_file, "abc", 3);
- readExpect(dummy_file, "a", 1);
- writeFail(empty_dir, UV_EISDIR);
- readFail(empty_dir, UV_EISDIR);
- /* ax+ */
- flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_RDWR |
- UV_FS_O_EXCL;
- writeExpect(absent_file, "bc", 2);
- readExpect(absent_file, "", 0);
- openFail(empty_file, UV_EEXIST);
- openFail(dummy_file, UV_EEXIST);
- openFail(empty_dir, UV_EEXIST);
- /* as+ */
- flags = add_flags | UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_RDWR |
- UV_FS_O_SYNC;
- writeExpect(absent_file, "bc", 2);
- readExpect(absent_file, "", 0);
- writeExpect(empty_file, "bc", 2);
- readExpect(empty_file, "", 0);
- writeExpect(dummy_file, "abc", 3);
- readExpect(dummy_file, "a", 1);
- writeFail(empty_dir, UV_EISDIR);
- readFail(empty_dir, UV_EISDIR);
- }
- TEST_IMPL(fs_open_flags) {
- setup();
- fs_open_flags(0);
- fs_open_flags(UV_FS_O_FILEMAP);
- /* Cleanup. */
- rmdir(empty_dir);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #else
- typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
- #endif /* ifndef _WIN32 */
|