Add shutdown dialog to start menu.
[helenos.git] / uspace / app / tester / vfs / vfs1.c
blob2d0dbc778e30d3745e9c58083a15fd0c67d2f0cf
1 /*
2 * Copyright (c) 2008 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
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"
40 #define TEST_DIRECTORY "/tmp/testdir"
41 #define TEST_FILE TEST_DIRECTORY "/testfile"
42 #define TEST_FILE2 TEST_DIRECTORY "/nextfile"
44 #define MAX_DEVICE_NAME 32
45 #define BUF_SIZE 16
47 static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
49 static const char *read_root(void)
51 TPRINTF("Opening the root directory...");
53 DIR *dirp = opendir("/");
54 if (!dirp) {
55 TPRINTF("\n");
56 return "opendir() failed";
57 } else
58 TPRINTF("OK\n");
60 struct dirent *dp;
61 while ((dp = readdir(dirp)))
62 TPRINTF(" node \"%s\"\n", dp->d_name);
63 closedir(dirp);
65 return NULL;
68 const char *test_vfs1(void)
70 aoff64_t pos = 0;
71 errno_t rc;
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";
78 TPRINTF("Created directory %s\n", TEST_DIRECTORY);
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);
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);
94 pos = 0;
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";
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);
112 vfs_put(fd0);
114 const char *rv = read_root();
115 if (rv != NULL)
116 return rv;
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);
122 if (vfs_unlink_path(TEST_FILE2) != EOK)
123 return "vfs_unlink_path() failed";
124 TPRINTF("Unlinked %s\n", TEST_FILE2);
126 if (vfs_unlink_path(TEST_DIRECTORY) != EOK)
127 return "vfs_unlink_path() failed";
128 TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
130 rv = read_root();
131 if (rv != NULL)
132 return rv;
134 return NULL;