[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>
|
---|
[15f3c3f] | 37 | #include <loc.h>
|
---|
[72bde81] | 38 | #include <sys/types.h>
|
---|
| 39 | #include <sys/stat.h>
|
---|
[6344851] | 40 | #include "../tester.h"
|
---|
| 41 |
|
---|
[6118ccaf] | 42 | #define TEST_DIRECTORY "/tmp/testdir"
|
---|
[2d11a7d8] | 43 | #define TEST_FILE TEST_DIRECTORY "/testfile"
|
---|
| 44 | #define TEST_FILE2 TEST_DIRECTORY "/nextfile"
|
---|
| 45 |
|
---|
| 46 | #define MAX_DEVICE_NAME 32
|
---|
| 47 | #define BUF_SIZE 16
|
---|
| 48 |
|
---|
| 49 | static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
|
---|
| 50 |
|
---|
[a000878c] | 51 | static const char *read_root(void)
|
---|
[6344851] | 52 | {
|
---|
[2d11a7d8] | 53 | TPRINTF("Opening the root directory...");
|
---|
| 54 |
|
---|
| 55 | DIR *dirp = opendir("/");
|
---|
| 56 | if (!dirp) {
|
---|
| 57 | TPRINTF("\n");
|
---|
| 58 | return "opendir() failed";
|
---|
| 59 | } else
|
---|
| 60 | TPRINTF("OK\n");
|
---|
| 61 |
|
---|
| 62 | struct dirent *dp;
|
---|
| 63 | while ((dp = readdir(dirp)))
|
---|
| 64 | TPRINTF(" node \"%s\"\n", dp->d_name);
|
---|
| 65 | closedir(dirp);
|
---|
| 66 |
|
---|
| 67 | return NULL;
|
---|
| 68 | }
|
---|
[2f60a529] | 69 |
|
---|
[a000878c] | 70 | const char *test_vfs1(void)
|
---|
[2d11a7d8] | 71 | {
|
---|
[6118ccaf] | 72 | int rc;
|
---|
| 73 | if ((rc = mkdir(TEST_DIRECTORY, 0)) != 0) {
|
---|
| 74 | TPRINTF("rc=%d\n", rc);
|
---|
[2d11a7d8] | 75 | return "mkdir() failed";
|
---|
[2f60a529] | 76 | }
|
---|
[2d11a7d8] | 77 | TPRINTF("Created directory %s\n", TEST_DIRECTORY);
|
---|
| 78 |
|
---|
| 79 | int fd0 = open(TEST_FILE, O_CREAT);
|
---|
[f7017572] | 80 | if (fd0 < 0)
|
---|
[2d11a7d8] | 81 | return "open() failed";
|
---|
| 82 | TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
|
---|
| 83 |
|
---|
[f7017572] | 84 | size_t size = sizeof(text);
|
---|
[2d11a7d8] | 85 | ssize_t cnt = write(fd0, text, size);
|
---|
[f7017572] | 86 | if (cnt < 0)
|
---|
[2d11a7d8] | 87 | return "write() failed";
|
---|
[0b0f4bb] | 88 | TPRINTF("Written %zd bytes\n", cnt);
|
---|
[2d11a7d8] | 89 |
|
---|
[f7017572] | 90 | if (lseek(fd0, 0, SEEK_SET) != 0)
|
---|
[2d11a7d8] | 91 | return "lseek() failed";
|
---|
| 92 | TPRINTF("Sought to position 0\n");
|
---|
| 93 |
|
---|
| 94 | char buf[BUF_SIZE];
|
---|
| 95 | while ((cnt = read(fd0, buf, BUF_SIZE))) {
|
---|
[1a60feeb] | 96 | if (cnt < 0)
|
---|
[2d11a7d8] | 97 | return "read() failed";
|
---|
| 98 |
|
---|
[0b0f4bb] | 99 | int _cnt = (int) cnt;
|
---|
| 100 | if (_cnt != cnt) {
|
---|
| 101 | /* Count overflow, just to be sure. */
|
---|
| 102 | TPRINTF("Read %zd bytes\n", cnt);
|
---|
| 103 | } else {
|
---|
| 104 | TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, _cnt, buf);
|
---|
| 105 | }
|
---|
[1a60feeb] | 106 | }
|
---|
[2d11a7d8] | 107 |
|
---|
[343dc9e3] | 108 | close(fd0);
|
---|
| 109 |
|
---|
[a000878c] | 110 | const char *rv = read_root();
|
---|
[2d11a7d8] | 111 | if (rv != NULL)
|
---|
| 112 | return rv;
|
---|
| 113 |
|
---|
| 114 | if (rename(TEST_FILE, TEST_FILE2))
|
---|
| 115 | return "rename() failed";
|
---|
| 116 | TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
|
---|
| 117 |
|
---|
| 118 | if (unlink(TEST_FILE2))
|
---|
| 119 | return "unlink() failed";
|
---|
| 120 | TPRINTF("Unlinked %s\n", TEST_FILE2);
|
---|
| 121 |
|
---|
| 122 | if (rmdir(TEST_DIRECTORY))
|
---|
| 123 | return "rmdir() failed";
|
---|
| 124 | TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
|
---|
| 125 |
|
---|
| 126 | rv = read_root();
|
---|
| 127 | if (rv != NULL)
|
---|
| 128 | return rv;
|
---|
[343dc9e3] | 129 |
|
---|
[6344851] | 130 | return NULL;
|
---|
| 131 | }
|
---|