| 1 | /*
 | 
|---|
| 2 |  * Copyright (c) 2008 Jakub Jermar
 | 
|---|
| 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>
 | 
|---|
| 31 | #include <stdlib.h>
 | 
|---|
| 32 | #include <stddef.h>
 | 
|---|
| 33 | #include <str_error.h>
 | 
|---|
| 34 | #include <str.h>
 | 
|---|
| 35 | #include <vfs/vfs.h>
 | 
|---|
| 36 | #include <dirent.h>
 | 
|---|
| 37 | #include <loc.h>
 | 
|---|
| 38 | #include "../tester.h"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #define TEST_DIRECTORY  "/tmp/testdir"
 | 
|---|
| 41 | #define TEST_FILE       TEST_DIRECTORY "/testfile"
 | 
|---|
| 42 | #define TEST_FILE2      TEST_DIRECTORY "/nextfile"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #define MAX_DEVICE_NAME  32
 | 
|---|
| 45 | #define BUF_SIZE         16
 | 
|---|
| 46 | 
 | 
|---|
| 47 | static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
 | 
|---|
| 48 | 
 | 
|---|
| 49 | static const char *read_root(void)
 | 
|---|
| 50 | {
 | 
|---|
| 51 |         TPRINTF("Opening the root directory...");
 | 
|---|
| 52 | 
 | 
|---|
| 53 |         DIR *dirp = opendir("/");
 | 
|---|
| 54 |         if (!dirp) {
 | 
|---|
| 55 |                 TPRINTF("\n");
 | 
|---|
| 56 |                 return "opendir() failed";
 | 
|---|
| 57 |         } else
 | 
|---|
| 58 |                 TPRINTF("OK\n");
 | 
|---|
| 59 | 
 | 
|---|
| 60 |         struct dirent *dp;
 | 
|---|
| 61 |         while ((dp = readdir(dirp)))
 | 
|---|
| 62 |                 TPRINTF(" node \"%s\"\n", dp->d_name);
 | 
|---|
| 63 |         closedir(dirp);
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         return NULL;
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | const char *test_vfs1(void)
 | 
|---|
| 69 | {
 | 
|---|
| 70 |         aoff64_t pos = 0;
 | 
|---|
| 71 |         errno_t rc;
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         rc = vfs_link_path(TEST_DIRECTORY, KIND_DIRECTORY, NULL);
 | 
|---|
| 74 |         if (rc != EOK) {
 | 
|---|
| 75 |                 TPRINTF("rc=%s\n", str_error_name(rc));
 | 
|---|
| 76 |                 return "vfs_link_path() failed";
 | 
|---|
| 77 |         }
 | 
|---|
| 78 |         TPRINTF("Created directory %s\n", TEST_DIRECTORY);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         int fd0;
 | 
|---|
| 81 |         rc = vfs_lookup_open(TEST_FILE, WALK_REGULAR | WALK_MAY_CREATE,
 | 
|---|
| 82 |             MODE_READ | MODE_WRITE, &fd0);
 | 
|---|
| 83 |         if (rc != EOK)
 | 
|---|
| 84 |                 return "vfs_lookup_open() failed";
 | 
|---|
| 85 |         TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
 | 
|---|
| 86 | 
 | 
|---|
| 87 |         size_t size = sizeof(text);
 | 
|---|
| 88 |         size_t cnt;
 | 
|---|
| 89 |         rc  = vfs_write(fd0, &pos, text, size, &cnt);
 | 
|---|
| 90 |         if (rc != EOK)
 | 
|---|
| 91 |                 return "write() failed";
 | 
|---|
| 92 |         TPRINTF("Written %zd bytes\n", cnt);
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         pos = 0;
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         char buf[BUF_SIZE];
 | 
|---|
| 97 |         TPRINTF("read..\n");
 | 
|---|
| 98 |         while ((rc = vfs_read(fd0, &pos, buf, BUF_SIZE, &cnt))) {
 | 
|---|
| 99 |                 TPRINTF("read returns rc = %s, cnt = %zu\n", str_error_name(rc), cnt);
 | 
|---|
| 100 |                 if (rc != EOK)
 | 
|---|
| 101 |                         return "read() failed";
 | 
|---|
| 102 | 
 | 
|---|
| 103 |                 int icnt = (int) cnt;
 | 
|---|
| 104 |                 if ((size_t) icnt != cnt) {
 | 
|---|
| 105 |                         /* Count overflow, just to be sure. */
 | 
|---|
| 106 |                         TPRINTF("Read %zd bytes\n", cnt);
 | 
|---|
| 107 |                 } else {
 | 
|---|
| 108 |                         TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, icnt, buf);
 | 
|---|
| 109 |                 }
 | 
|---|
| 110 |         }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         vfs_put(fd0);
 | 
|---|
| 113 | 
 | 
|---|
| 114 |         const char *rv = read_root();
 | 
|---|
| 115 |         if (rv != NULL)
 | 
|---|
| 116 |                 return rv;
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         if (vfs_rename_path(TEST_FILE, TEST_FILE2) != EOK)
 | 
|---|
| 119 |                 return "vfs_rename_path() failed";
 | 
|---|
| 120 |         TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
 | 
|---|
| 121 | 
 | 
|---|
| 122 |         if (vfs_unlink_path(TEST_FILE2) != EOK)
 | 
|---|
| 123 |                 return "vfs_unlink_path() failed";
 | 
|---|
| 124 |         TPRINTF("Unlinked %s\n", TEST_FILE2);
 | 
|---|
| 125 | 
 | 
|---|
| 126 |         if (vfs_unlink_path(TEST_DIRECTORY) != EOK)
 | 
|---|
| 127 |                 return "vfs_unlink_path() failed";
 | 
|---|
| 128 |         TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
 | 
|---|
| 129 | 
 | 
|---|
| 130 |         rv = read_root();
 | 
|---|
| 131 |         if (rv != NULL)
 | 
|---|
| 132 |                 return rv;
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         return NULL;
 | 
|---|
| 135 | }
 | 
|---|