[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>
|
---|
[19f857a] | 32 | #include <str.h>
|
---|
[5fec355] | 33 | #include <vfs/vfs.h>
|
---|
[449c246] | 34 | #include <unistd.h>
|
---|
| 35 | #include <fcntl.h>
|
---|
[5973fd0] | 36 | #include <dirent.h>
|
---|
[2d11a7d8] | 37 | #include <devmap.h>
|
---|
[72bde81] | 38 | #include <sys/types.h>
|
---|
| 39 | #include <sys/stat.h>
|
---|
[6344851] | 40 | #include "../tester.h"
|
---|
| 41 |
|
---|
[2d11a7d8] | 42 | #define FS_TYPE "tmpfs"
|
---|
| 43 | #define MOUNT_POINT "/tmp"
|
---|
| 44 | #define OPTIONS ""
|
---|
| 45 | #define FLAGS 0
|
---|
[f7017572] | 46 |
|
---|
[2d11a7d8] | 47 | #define TEST_DIRECTORY MOUNT_POINT "/testdir"
|
---|
| 48 | #define TEST_FILE TEST_DIRECTORY "/testfile"
|
---|
| 49 | #define TEST_FILE2 TEST_DIRECTORY "/nextfile"
|
---|
| 50 |
|
---|
| 51 | #define MAX_DEVICE_NAME 32
|
---|
| 52 | #define BUF_SIZE 16
|
---|
| 53 |
|
---|
| 54 | static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
|
---|
| 55 |
|
---|
[a000878c] | 56 | static const char *read_root(void)
|
---|
[6344851] | 57 | {
|
---|
[2d11a7d8] | 58 | TPRINTF("Opening the root directory...");
|
---|
| 59 |
|
---|
| 60 | DIR *dirp = opendir("/");
|
---|
| 61 | if (!dirp) {
|
---|
| 62 | TPRINTF("\n");
|
---|
| 63 | return "opendir() failed";
|
---|
| 64 | } else
|
---|
| 65 | TPRINTF("OK\n");
|
---|
| 66 |
|
---|
| 67 | struct dirent *dp;
|
---|
| 68 | while ((dp = readdir(dirp)))
|
---|
| 69 | TPRINTF(" node \"%s\"\n", dp->d_name);
|
---|
| 70 | closedir(dirp);
|
---|
| 71 |
|
---|
| 72 | return NULL;
|
---|
| 73 | }
|
---|
[2f60a529] | 74 |
|
---|
[a000878c] | 75 | const char *test_vfs1(void)
|
---|
[2d11a7d8] | 76 | {
|
---|
| 77 | if (mkdir(MOUNT_POINT, 0) != 0)
|
---|
| 78 | return "mkdir() failed";
|
---|
| 79 | TPRINTF("Created directory %s\n", MOUNT_POINT);
|
---|
| 80 |
|
---|
[210e50a] | 81 | int rc = mount(FS_TYPE, MOUNT_POINT, "", OPTIONS, FLAGS);
|
---|
[2f60a529] | 82 | switch (rc) {
|
---|
| 83 | case EOK:
|
---|
[210e50a] | 84 | TPRINTF("Mounted %s on %s\n", FS_TYPE, MOUNT_POINT);
|
---|
[2f60a529] | 85 | break;
|
---|
| 86 | case EBUSY:
|
---|
[2d11a7d8] | 87 | TPRINTF("(INFO) Filesystem already mounted on %s\n", MOUNT_POINT);
|
---|
[2f60a529] | 88 | break;
|
---|
| 89 | default:
|
---|
[2d11a7d8] | 90 | TPRINTF("(ERR) IPC returned errno %d (is tmpfs loaded?)\n", rc);
|
---|
| 91 | return "mount() failed";
|
---|
[2f60a529] | 92 | }
|
---|
[f7017572] | 93 |
|
---|
[2d11a7d8] | 94 | if (mkdir(TEST_DIRECTORY, 0) != 0)
|
---|
| 95 | return "mkdir() failed";
|
---|
| 96 | TPRINTF("Created directory %s\n", TEST_DIRECTORY);
|
---|
| 97 |
|
---|
| 98 | int fd0 = open(TEST_FILE, O_CREAT);
|
---|
[f7017572] | 99 | if (fd0 < 0)
|
---|
[2d11a7d8] | 100 | return "open() failed";
|
---|
| 101 | TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
|
---|
| 102 |
|
---|
[f7017572] | 103 | size_t size = sizeof(text);
|
---|
[2d11a7d8] | 104 | ssize_t cnt = write(fd0, text, size);
|
---|
[f7017572] | 105 | if (cnt < 0)
|
---|
[2d11a7d8] | 106 | return "write() failed";
|
---|
[0b0f4bb] | 107 | TPRINTF("Written %zd bytes\n", cnt);
|
---|
[2d11a7d8] | 108 |
|
---|
[f7017572] | 109 | if (lseek(fd0, 0, SEEK_SET) != 0)
|
---|
[2d11a7d8] | 110 | return "lseek() failed";
|
---|
| 111 | TPRINTF("Sought to position 0\n");
|
---|
| 112 |
|
---|
| 113 | char buf[BUF_SIZE];
|
---|
| 114 | while ((cnt = read(fd0, buf, BUF_SIZE))) {
|
---|
[1a60feeb] | 115 | if (cnt < 0)
|
---|
[2d11a7d8] | 116 | return "read() failed";
|
---|
| 117 |
|
---|
[0b0f4bb] | 118 | int _cnt = (int) cnt;
|
---|
| 119 | if (_cnt != cnt) {
|
---|
| 120 | /* Count overflow, just to be sure. */
|
---|
| 121 | TPRINTF("Read %zd bytes\n", cnt);
|
---|
| 122 | } else {
|
---|
| 123 | TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, _cnt, buf);
|
---|
| 124 | }
|
---|
[1a60feeb] | 125 | }
|
---|
[2d11a7d8] | 126 |
|
---|
[343dc9e3] | 127 | close(fd0);
|
---|
| 128 |
|
---|
[a000878c] | 129 | const char *rv = read_root();
|
---|
[2d11a7d8] | 130 | if (rv != NULL)
|
---|
| 131 | return rv;
|
---|
| 132 |
|
---|
| 133 | if (rename(TEST_FILE, TEST_FILE2))
|
---|
| 134 | return "rename() failed";
|
---|
| 135 | TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
|
---|
| 136 |
|
---|
| 137 | if (unlink(TEST_FILE2))
|
---|
| 138 | return "unlink() failed";
|
---|
| 139 | TPRINTF("Unlinked %s\n", TEST_FILE2);
|
---|
| 140 |
|
---|
| 141 | if (rmdir(TEST_DIRECTORY))
|
---|
| 142 | return "rmdir() failed";
|
---|
| 143 | TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
|
---|
| 144 |
|
---|
| 145 | rv = read_root();
|
---|
| 146 | if (rv != NULL)
|
---|
| 147 | return rv;
|
---|
[343dc9e3] | 148 |
|
---|
[6344851] | 149 | return NULL;
|
---|
| 150 | }
|
---|