HelenOS sources
This source file includes following definitions.
- read_root
- test_vfs1
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <str_error.h>
#include <str.h>
#include <vfs/vfs.h>
#include <dirent.h>
#include <loc.h>
#include "../tester.h"
#define TEST_DIRECTORY "/tmp/testdir"
#define TEST_FILE TEST_DIRECTORY "/testfile"
#define TEST_FILE2 TEST_DIRECTORY "/nextfile"
#define MAX_DEVICE_NAME 32
#define BUF_SIZE 16
static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
static const char *read_root(void)
{
TPRINTF("Opening the root directory...");
DIR *dirp = opendir("/");
if (!dirp) {
TPRINTF("\n");
return "opendir() failed";
} else
TPRINTF("OK\n");
struct dirent *dp;
while ((dp = readdir(dirp)))
TPRINTF(" node \"%s\"\n", dp->d_name);
closedir(dirp);
return NULL;
}
const char *test_vfs1(void)
{
aoff64_t pos = 0;
errno_t rc;
rc = vfs_link_path(TEST_DIRECTORY, KIND_DIRECTORY, NULL);
if (rc != EOK) {
TPRINTF("rc=%s\n", str_error_name(rc));
return "vfs_link_path() failed";
}
TPRINTF("Created directory %s\n", TEST_DIRECTORY);
int fd0;
rc = vfs_lookup_open(TEST_FILE, WALK_REGULAR | WALK_MAY_CREATE,
MODE_READ | MODE_WRITE, &fd0);
if (rc != EOK)
return "vfs_lookup_open() failed";
TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
size_t size = sizeof(text);
size_t cnt;
rc = vfs_write(fd0, &pos, text, size, &cnt);
if (rc != EOK)
return "write() failed";
TPRINTF("Written %zd bytes\n", cnt);
pos = 0;
char buf[BUF_SIZE];
TPRINTF("read..\n");
while ((rc = vfs_read(fd0, &pos, buf, BUF_SIZE, &cnt))) {
TPRINTF("read returns rc = %s, cnt = %zu\n", str_error_name(rc), cnt);
if (rc != EOK)
return "read() failed";
int icnt = (int) cnt;
if ((size_t) icnt != cnt) {
TPRINTF("Read %zd bytes\n", cnt);
} else {
TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, icnt, buf);
}
}
vfs_put(fd0);
const char *rv = read_root();
if (rv != NULL)
return rv;
if (vfs_rename_path(TEST_FILE, TEST_FILE2) != EOK)
return "vfs_rename_path() failed";
TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
if (vfs_unlink_path(TEST_FILE2) != EOK)
return "vfs_unlink_path() failed";
TPRINTF("Unlinked %s\n", TEST_FILE2);
if (vfs_unlink_path(TEST_DIRECTORY) != EOK)
return "vfs_unlink_path() failed";
TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
rv = read_root();
if (rv != NULL)
return rv;
return NULL;
}
HelenOS homepage, sources at GitHub