Subversion Repositories HelenOS

Rev

Rev 4602 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4602 Rev 4687
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <errno.h>
29
#include <errno.h>
30
#include <stdio.h>
30
#include <stdio.h>
31
#include <stdlib.h>
31
#include <stdlib.h>
32
#include <string.h>
32
#include <string.h>
33
#include <vfs/vfs.h>
33
#include <vfs/vfs.h>
34
#include <unistd.h>
34
#include <unistd.h>
35
#include <fcntl.h>
35
#include <fcntl.h>
36
#include <dirent.h>
36
#include <dirent.h>
37
#include <devmap.h>
37
#include <devmap.h>
38
#include <sys/types.h>
38
#include <sys/types.h>
39
#include <sys/stat.h>
39
#include <sys/stat.h>
40
#include "../tester.h"
40
#include "../tester.h"
41
 
41
 
42
#define FS_TYPE      "tmpfs"
42
#define FS_TYPE      "tmpfs"
43
#define MOUNT_POINT  "/tmp"
43
#define MOUNT_POINT  "/tmp"
44
#define OPTIONS      ""
44
#define OPTIONS      ""
45
#define FLAGS        0
45
#define FLAGS        0
46
 
46
 
47
#define TEST_DIRECTORY  MOUNT_POINT "/testdir"
47
#define TEST_DIRECTORY  MOUNT_POINT "/testdir"
48
#define TEST_FILE       TEST_DIRECTORY "/testfile"
48
#define TEST_FILE       TEST_DIRECTORY "/testfile"
49
#define TEST_FILE2      TEST_DIRECTORY "/nextfile"
49
#define TEST_FILE2      TEST_DIRECTORY "/nextfile"
50
 
50
 
51
#define MAX_DEVICE_NAME  32
51
#define MAX_DEVICE_NAME  32
52
#define BUF_SIZE         16
52
#define BUF_SIZE         16
53
 
53
 
54
static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
54
static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
55
 
55
 
56
static char *read_root(void)
56
static char *read_root(void)
57
{
57
{
58
    TPRINTF("Opening the root directory...");
58
    TPRINTF("Opening the root directory...");
59
   
59
   
60
    DIR *dirp = opendir("/");
60
    DIR *dirp = opendir("/");
61
    if (!dirp) {
61
    if (!dirp) {
62
        TPRINTF("\n");
62
        TPRINTF("\n");
63
        return "opendir() failed";
63
        return "opendir() failed";
64
    } else
64
    } else
65
        TPRINTF("OK\n");
65
        TPRINTF("OK\n");
66
   
66
   
67
    struct dirent *dp;
67
    struct dirent *dp;
68
    while ((dp = readdir(dirp)))
68
    while ((dp = readdir(dirp)))
69
        TPRINTF(" node \"%s\"\n", dp->d_name);
69
        TPRINTF(" node \"%s\"\n", dp->d_name);
70
    closedir(dirp);
70
    closedir(dirp);
71
   
71
   
72
    return NULL;
72
    return NULL;
73
}
73
}
74
 
74
 
75
char *test_vfs1(void)
75
char *test_vfs1(void)
76
{
76
{
77
    if (mkdir(MOUNT_POINT, 0) != 0)
77
    if (mkdir(MOUNT_POINT, 0) != 0)
78
        return "mkdir() failed";
78
        return "mkdir() failed";
79
    TPRINTF("Created directory %s\n", MOUNT_POINT);
79
    TPRINTF("Created directory %s\n", MOUNT_POINT);
80
   
80
   
81
    char null[MAX_DEVICE_NAME];
81
    char null[MAX_DEVICE_NAME];
82
    int null_id = devmap_null_create();
82
    int null_id = devmap_null_create();
83
   
83
   
84
    if (null_id == -1)
84
    if (null_id == -1)
85
        return "Unable to create null device";
85
        return "Unable to create null device";
86
   
86
   
87
    snprintf(null, MAX_DEVICE_NAME, "null%d", null_id);
87
    snprintf(null, MAX_DEVICE_NAME, "null%d", null_id);
88
    int rc = mount(FS_TYPE, MOUNT_POINT, null, OPTIONS, FLAGS);
88
    int rc = mount(FS_TYPE, MOUNT_POINT, null, OPTIONS, FLAGS);
89
    switch (rc) {
89
    switch (rc) {
90
    case EOK:
90
    case EOK:
91
        TPRINTF("Mounted /dev/%s as %s on %s\n", null, FS_TYPE, MOUNT_POINT);
91
        TPRINTF("Mounted /dev/%s as %s on %s\n", null, FS_TYPE, MOUNT_POINT);
92
        break;
92
        break;
93
    case EBUSY:
93
    case EBUSY:
94
        TPRINTF("(INFO) Filesystem already mounted on %s\n", MOUNT_POINT);
94
        TPRINTF("(INFO) Filesystem already mounted on %s\n", MOUNT_POINT);
95
        break;
95
        break;
96
    default:
96
    default:
97
        TPRINTF("(ERR) IPC returned errno %d (is tmpfs loaded?)\n", rc);
97
        TPRINTF("(ERR) IPC returned errno %d (is tmpfs loaded?)\n", rc);
98
        return "mount() failed";
98
        return "mount() failed";
99
    }
99
    }
100
   
100
   
101
    if (mkdir(TEST_DIRECTORY, 0) != 0)
101
    if (mkdir(TEST_DIRECTORY, 0) != 0)
102
        return "mkdir() failed";
102
        return "mkdir() failed";
103
    TPRINTF("Created directory %s\n", TEST_DIRECTORY);
103
    TPRINTF("Created directory %s\n", TEST_DIRECTORY);
104
   
104
   
105
    int fd0 = open(TEST_FILE, O_CREAT);
105
    int fd0 = open(TEST_FILE, O_CREAT);
106
    if (fd0 < 0)
106
    if (fd0 < 0)
107
        return "open() failed";
107
        return "open() failed";
108
    TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
108
    TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
109
   
109
   
110
    size_t size = sizeof(text);
110
    size_t size = sizeof(text);
111
    ssize_t cnt = write(fd0, text, size);
111
    ssize_t cnt = write(fd0, text, size);
112
    if (cnt < 0)
112
    if (cnt < 0)
113
        return "write() failed";
113
        return "write() failed";
114
    TPRINTF("Written %d bytes\n", cnt);
114
    TPRINTF("Written %d bytes\n", cnt);
115
   
115
   
116
    if (lseek(fd0, 0, SEEK_SET) != 0)
116
    if (lseek(fd0, 0, SEEK_SET) != 0)
117
        return "lseek() failed";
117
        return "lseek() failed";
118
    TPRINTF("Sought to position 0\n");
118
    TPRINTF("Sought to position 0\n");
119
   
119
   
120
    char buf[BUF_SIZE];
120
    char buf[BUF_SIZE];
121
    while ((cnt = read(fd0, buf, BUF_SIZE))) {
121
    while ((cnt = read(fd0, buf, BUF_SIZE))) {
122
        if (cnt < 0)
122
        if (cnt < 0)
123
            return "read() failed";
123
            return "read() failed";
124
       
124
       
125
        TPRINTF("Read %d bytes: \".*s\"\n", cnt, cnt, buf);
125
        TPRINTF("Read %d bytes: \".*s\"\n", cnt, cnt, buf);
126
    }
126
    }
127
   
127
   
128
    close(fd0);
128
    close(fd0);
129
   
129
   
130
    char *rv = read_root();
130
    char *rv = read_root();
131
    if (rv != NULL)
131
    if (rv != NULL)
132
        return rv;
132
        return rv;
133
   
133
   
134
    if (rename(TEST_FILE, TEST_FILE2))
134
    if (rename(TEST_FILE, TEST_FILE2))
135
        return "rename() failed";
135
        return "rename() failed";
136
    TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
136
    TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
137
   
137
   
138
    if (unlink(TEST_FILE2))
138
    if (unlink(TEST_FILE2))
139
        return "unlink() failed";
139
        return "unlink() failed";
140
    TPRINTF("Unlinked %s\n", TEST_FILE2);
140
    TPRINTF("Unlinked %s\n", TEST_FILE2);
141
   
141
   
142
    if (rmdir(TEST_DIRECTORY))
142
    if (rmdir(TEST_DIRECTORY))
143
        return "rmdir() failed";
143
        return "rmdir() failed";
144
    TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
144
    TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
145
   
145
   
146
    rv = read_root();
146
    rv = read_root();
147
    if (rv != NULL)
147
    if (rv != NULL)
148
        return rv;
148
        return rv;
149
   
149
   
150
    return NULL;
150
    return NULL;
151
}
151
}
152
 
152