[6344851] | 1 | /*
|
---|
[5973fd0] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[6344851] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <errno.h>
|
---|
| 30 | #include <stdio.h>
|
---|
[986332aa] | 31 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 32 | #include <stddef.h>
|
---|
[19f857a] | 33 | #include <str.h>
|
---|
[5fec355] | 34 | #include <vfs/vfs.h>
|
---|
[5973fd0] | 35 | #include <dirent.h>
|
---|
[15f3c3f] | 36 | #include <loc.h>
|
---|
[6344851] | 37 | #include "../tester.h"
|
---|
| 38 |
|
---|
[6118ccaf] | 39 | #define TEST_DIRECTORY "/tmp/testdir"
|
---|
[2d11a7d8] | 40 | #define TEST_FILE TEST_DIRECTORY "/testfile"
|
---|
| 41 | #define TEST_FILE2 TEST_DIRECTORY "/nextfile"
|
---|
| 42 |
|
---|
| 43 | #define MAX_DEVICE_NAME 32
|
---|
| 44 | #define BUF_SIZE 16
|
---|
| 45 |
|
---|
| 46 | static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
|
---|
| 47 |
|
---|
[a000878c] | 48 | static const char *read_root(void)
|
---|
[6344851] | 49 | {
|
---|
[2d11a7d8] | 50 | TPRINTF("Opening the root directory...");
|
---|
| 51 |
|
---|
| 52 | DIR *dirp = opendir("/");
|
---|
| 53 | if (!dirp) {
|
---|
| 54 | TPRINTF("\n");
|
---|
| 55 | return "opendir() failed";
|
---|
| 56 | } else
|
---|
| 57 | TPRINTF("OK\n");
|
---|
| 58 |
|
---|
| 59 | struct dirent *dp;
|
---|
| 60 | while ((dp = readdir(dirp)))
|
---|
| 61 | TPRINTF(" node \"%s\"\n", dp->d_name);
|
---|
| 62 | closedir(dirp);
|
---|
| 63 |
|
---|
| 64 | return NULL;
|
---|
| 65 | }
|
---|
[2f60a529] | 66 |
|
---|
[a000878c] | 67 | const char *test_vfs1(void)
|
---|
[2d11a7d8] | 68 | {
|
---|
[58898d1d] | 69 | aoff64_t pos = 0;
|
---|
[6e5562a] | 70 | int rc;
|
---|
[58898d1d] | 71 |
|
---|
[a6fc88a] | 72 | rc = vfs_link_path(TEST_DIRECTORY, KIND_DIRECTORY, NULL);
|
---|
[6e5562a] | 73 | if (rc != EOK) {
|
---|
| 74 | TPRINTF("rc=%d\n", rc);
|
---|
| 75 | return "vfs_link_path() failed";
|
---|
[2f60a529] | 76 | }
|
---|
[2d11a7d8] | 77 | TPRINTF("Created directory %s\n", TEST_DIRECTORY);
|
---|
| 78 |
|
---|
[b19e892] | 79 | int fd0 = vfs_lookup_open(TEST_FILE, WALK_REGULAR | WALK_MAY_CREATE,
|
---|
| 80 | MODE_READ | MODE_WRITE);
|
---|
[f7017572] | 81 | if (fd0 < 0)
|
---|
[b19e892] | 82 | return "vfs_lookup_open() failed";
|
---|
[2d11a7d8] | 83 | TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
|
---|
| 84 |
|
---|
[f7017572] | 85 | size_t size = sizeof(text);
|
---|
[ce04ea44] | 86 | ssize_t cnt = vfs_write(fd0, &pos, text, size);
|
---|
[f7017572] | 87 | if (cnt < 0)
|
---|
[2d11a7d8] | 88 | return "write() failed";
|
---|
[0b0f4bb] | 89 | TPRINTF("Written %zd bytes\n", cnt);
|
---|
[58898d1d] | 90 |
|
---|
| 91 | pos = 0;
|
---|
[2d11a7d8] | 92 |
|
---|
| 93 | char buf[BUF_SIZE];
|
---|
[6afc9d7] | 94 | TPRINTF("read..\n");
|
---|
[ce04ea44] | 95 | while ((cnt = vfs_read(fd0, &pos, buf, BUF_SIZE))) {
|
---|
[6afc9d7] | 96 | TPRINTF("read returns %zd\n", cnt);
|
---|
[1a60feeb] | 97 | if (cnt < 0)
|
---|
[2d11a7d8] | 98 | return "read() failed";
|
---|
| 99 |
|
---|
[0b0f4bb] | 100 | int _cnt = (int) cnt;
|
---|
| 101 | if (_cnt != cnt) {
|
---|
| 102 | /* Count overflow, just to be sure. */
|
---|
| 103 | TPRINTF("Read %zd bytes\n", cnt);
|
---|
| 104 | } else {
|
---|
| 105 | TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, _cnt, buf);
|
---|
| 106 | }
|
---|
[1a60feeb] | 107 | }
|
---|
[2d11a7d8] | 108 |
|
---|
[9c4cf0d] | 109 | vfs_put(fd0);
|
---|
[343dc9e3] | 110 |
|
---|
[a000878c] | 111 | const char *rv = read_root();
|
---|
[2d11a7d8] | 112 | if (rv != NULL)
|
---|
| 113 | return rv;
|
---|
| 114 |
|
---|
[163fc09] | 115 | if (vfs_rename_path(TEST_FILE, TEST_FILE2) != EOK)
|
---|
| 116 | return "vfs_rename_path() failed";
|
---|
[2d11a7d8] | 117 | TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
|
---|
| 118 |
|
---|
[79ea5af] | 119 | if (vfs_unlink_path(TEST_FILE2) != EOK)
|
---|
| 120 | return "vfs_unlink_path() failed";
|
---|
[2d11a7d8] | 121 | TPRINTF("Unlinked %s\n", TEST_FILE2);
|
---|
| 122 |
|
---|
[79ea5af] | 123 | if (vfs_unlink_path(TEST_DIRECTORY) != EOK)
|
---|
| 124 | return "vfs_unlink_path() failed";
|
---|
[2d11a7d8] | 125 | TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
|
---|
| 126 |
|
---|
| 127 | rv = read_root();
|
---|
| 128 | if (rv != NULL)
|
---|
| 129 | return rv;
|
---|
[343dc9e3] | 130 |
|
---|
[6344851] | 131 | return NULL;
|
---|
| 132 | }
|
---|