source: mainline/uspace/app/tester/vfs/vfs1.c@ 8e3498b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8e3498b was 8e3498b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

vfs_read/write() should return error code separately from number of bytes transferred.

  • Property mode set to 100644
File size: 3.8 KB
Line 
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.h>
34#include <vfs/vfs.h>
35#include <dirent.h>
36#include <loc.h>
37#include "../tester.h"
38
39#define TEST_DIRECTORY "/tmp/testdir"
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
46static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
47
48static const char *read_root(void)
49{
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}
66
67const char *test_vfs1(void)
68{
69 aoff64_t pos = 0;
70 int rc;
71
72 rc = vfs_link_path(TEST_DIRECTORY, KIND_DIRECTORY, NULL);
73 if (rc != EOK) {
74 TPRINTF("rc=%d\n", rc);
75 return "vfs_link_path() failed";
76 }
77 TPRINTF("Created directory %s\n", TEST_DIRECTORY);
78
79 int fd0 = vfs_lookup_open(TEST_FILE, WALK_REGULAR | WALK_MAY_CREATE,
80 MODE_READ | MODE_WRITE);
81 if (fd0 < 0)
82 return "vfs_lookup_open() failed";
83 TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
84
85 size_t size = sizeof(text);
86 size_t cnt;
87 rc = vfs_write(fd0, &pos, text, size, &cnt);
88 if (rc != EOK)
89 return "write() failed";
90 TPRINTF("Written %zd bytes\n", cnt);
91
92 pos = 0;
93
94 char buf[BUF_SIZE];
95 TPRINTF("read..\n");
96 while ((rc = vfs_read(fd0, &pos, buf, BUF_SIZE, &cnt))) {
97 TPRINTF("read returns rc = %d, cnt = %zu\n", rc, cnt);
98 if (rc != EOK)
99 return "read() failed";
100
101 int icnt = (int) cnt;
102 if ((size_t) icnt != cnt) {
103 /* Count overflow, just to be sure. */
104 TPRINTF("Read %zd bytes\n", cnt);
105 } else {
106 TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, icnt, buf);
107 }
108 }
109
110 vfs_put(fd0);
111
112 const char *rv = read_root();
113 if (rv != NULL)
114 return rv;
115
116 if (vfs_rename_path(TEST_FILE, TEST_FILE2) != EOK)
117 return "vfs_rename_path() failed";
118 TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
119
120 if (vfs_unlink_path(TEST_FILE2) != EOK)
121 return "vfs_unlink_path() failed";
122 TPRINTF("Unlinked %s\n", TEST_FILE2);
123
124 if (vfs_unlink_path(TEST_DIRECTORY) != EOK)
125 return "vfs_unlink_path() failed";
126 TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
127
128 rv = read_root();
129 if (rv != NULL)
130 return rv;
131
132 return NULL;
133}
Note: See TracBrowser for help on using the repository browser.