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

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

UNIX-like I/O functions should use errno to return error code for many reasons.

  • Property mode set to 100644
File size: 3.6 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 <str.h>
33#include <vfs/vfs.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <dirent.h>
37#include <loc.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include "../tester.h"
41
42#define TEST_DIRECTORY "/tmp/testdir"
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
49static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
50
51static const char *read_root(void)
52{
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}
69
70const char *test_vfs1(void)
71{
72 if (mkdir(TEST_DIRECTORY, 0) != 0) {
73 TPRINTF("rc=%d\n", errno);
74 return "mkdir() failed";
75 }
76 TPRINTF("Created directory %s\n", TEST_DIRECTORY);
77
78 int fd0 = open(TEST_FILE, O_CREAT);
79 if (fd0 < 0)
80 return "open() failed";
81 TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
82
83 size_t size = sizeof(text);
84 ssize_t cnt = write(fd0, text, size);
85 if (cnt < 0)
86 return "write() failed";
87 TPRINTF("Written %zd bytes\n", cnt);
88
89 if (lseek(fd0, 0, SEEK_SET) != 0)
90 return "lseek() failed";
91 TPRINTF("Sought to position 0\n");
92
93 char buf[BUF_SIZE];
94 TPRINTF("read..\n");
95 while ((cnt = read(fd0, buf, BUF_SIZE))) {
96 TPRINTF("read returns %zd\n", cnt);
97 if (cnt < 0)
98 return "read() failed";
99
100 int _cnt = (int) cnt;
101 if (_cnt != cnt) {
102 /* Count overflow, just to be sure. */
103 TPRINTF("Read %zd bytes\n", cnt);
104 } else {
105 TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, _cnt, buf);
106 }
107 }
108
109 close(fd0);
110
111 const char *rv = read_root();
112 if (rv != NULL)
113 return rv;
114
115 if (rename(TEST_FILE, TEST_FILE2) != 0)
116 return "rename() failed";
117 TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
118
119 if (unlink(TEST_FILE2) != 0)
120 return "unlink() failed";
121 TPRINTF("Unlinked %s\n", TEST_FILE2);
122
123 if (rmdir(TEST_DIRECTORY) != 0)
124 return "rmdir() failed";
125 TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
126
127 rv = read_root();
128 if (rv != NULL)
129 return rv;
130
131 return NULL;
132}
Note: See TracBrowser for help on using the repository browser.