vfs1.c

00001 /*
00002  * Copyright (c) 2008 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #include <errno.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <str.h>
00033 #include <vfs/vfs.h>
00034 #include <unistd.h>
00035 #include <fcntl.h>
00036 #include <dirent.h>
00037 #include <devmap.h>
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040 #include "../tester.h"
00041 
00042 #define TEST_DIRECTORY  "/tmp/testdir"
00043 #define TEST_FILE       TEST_DIRECTORY "/testfile"
00044 #define TEST_FILE2      TEST_DIRECTORY "/nextfile"
00045 
00046 #define MAX_DEVICE_NAME  32
00047 #define BUF_SIZE         16
00048 
00049 static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
00050 
00051 static const char *read_root(void)
00052 {
00053         TPRINTF("Opening the root directory...");
00054         
00055         DIR *dirp = opendir("/");
00056         if (!dirp) {
00057                 TPRINTF("\n");
00058                 return "opendir() failed";
00059         } else
00060                 TPRINTF("OK\n");
00061         
00062         struct dirent *dp;
00063         while ((dp = readdir(dirp)))
00064                 TPRINTF(" node \"%s\"\n", dp->d_name);
00065         closedir(dirp);
00066         
00067         return NULL;
00068 }
00069 
00070 const char *test_vfs1(void)
00071 {
00072         int rc;
00073         if ((rc = mkdir(TEST_DIRECTORY, 0)) != 0) {
00074                 TPRINTF("rc=%d\n", rc);
00075                 return "mkdir() failed";
00076         }
00077         TPRINTF("Created directory %s\n", TEST_DIRECTORY);
00078         
00079         int fd0 = open(TEST_FILE, O_CREAT);
00080         if (fd0 < 0)
00081                 return "open() failed";
00082         TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
00083         
00084         size_t size = sizeof(text);
00085         ssize_t cnt = write(fd0, text, size);
00086         if (cnt < 0)
00087                 return "write() failed";
00088         TPRINTF("Written %zd bytes\n", cnt);
00089         
00090         if (lseek(fd0, 0, SEEK_SET) != 0)
00091                 return "lseek() failed";
00092         TPRINTF("Sought to position 0\n");
00093         
00094         char buf[BUF_SIZE];
00095         while ((cnt = read(fd0, buf, BUF_SIZE))) {
00096                 if (cnt < 0)
00097                         return "read() failed";
00098                 
00099                 int _cnt = (int) cnt;
00100                 if (_cnt != cnt) {
00101                         /* Count overflow, just to be sure. */
00102                         TPRINTF("Read %zd bytes\n", cnt);
00103                 } else {
00104                         TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, _cnt, buf);
00105                 }
00106         }
00107         
00108         close(fd0);
00109         
00110         const char *rv = read_root();
00111         if (rv != NULL)
00112                 return rv;
00113         
00114         if (rename(TEST_FILE, TEST_FILE2))
00115                 return "rename() failed";
00116         TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
00117         
00118         if (unlink(TEST_FILE2))
00119                 return "unlink() failed";
00120         TPRINTF("Unlinked %s\n", TEST_FILE2);
00121         
00122         if (rmdir(TEST_DIRECTORY))
00123                 return "rmdir() failed";
00124         TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
00125         
00126         rv = read_root();
00127         if (rv != NULL)
00128                 return rv;
00129         
00130         return NULL;
00131 }

Generated on Thu Jun 2 07:45:43 2011 for HelenOS/USB by  doxygen 1.4.7