610a1ea8ba7ff280b06c793d016f581ef0d692ba
[akaros.git] / tests / file_test.c
1 #include <rstdio.h> 
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <arch/arch.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <dirent.h>
9 #include <stdlib.h>
10
11 int main() 
12
13         FILE *file; 
14         file = fopen("/dir1/f1.txt","w+b");
15         if (file == NULL)
16                 printf ("Failed to open file \n");
17         fprintf(file,"%s","hello, world\n"); 
18         fclose(file); 
19
20         int fd = open("../../..//////dir1/test.txt", O_RDWR | O_CREAT );
21         char rbuf[256] = {0}, wbuf[256] = {0};
22         int retval;
23         retval = read(fd, rbuf, 16);
24         printf("Tried to read, got %d bytes of buf: %s\n", retval, rbuf);
25         strcpy(wbuf, "paul <3's the new 61c");
26         retval = write(fd, wbuf, 22);
27         printf("Tried to write, wrote %d bytes\n", retval);
28         printf("Trying to seek to 0\n");
29         lseek(fd, 0, SEEK_SET);
30         retval = read(fd, rbuf, 64);
31         printf("Tried to read again, got %d bytes of buf: %s\n", retval, rbuf);
32
33         retval = access("/bin/laden", X_OK);
34         if (errno != ENOENT)
35                 printf("WARNING! Access error for Osama!\n");
36         retval = access("////../../////dir1/f1.txt", R_OK);
37         if (retval < 0)
38                 printf("WARNING! Access error for f1.txt!\n");
39
40         struct stat st = {0};
41         //retval = stat("/bin/mhello", &st);
42         retval = fstat(fd, &st);
43         printf("Tried to stat, was told %d\n", retval);
44         #if 0
45         printf("STAT RESULTS\n---------------------\n");
46         printf("dev       : %d\n", st.st_dev);
47         printf("ino       : %d\n", st.st_ino);
48         printf("mode      : %d\n", st.st_mode);
49         printf("nlink     : %d\n", st.st_nlink);
50         printf("uid       : %d\n", st.st_uid);
51         printf("gid       : %d\n", st.st_gid);
52         printf("rdev      : %d\n", st.st_rdev);
53         printf("size      : %d\n", st.st_size);
54         printf("blksize   : %d\n", st.st_blksize);
55         printf("blocks    : %d\n", st.st_blocks);
56         printf("atime     : %d\n", st.st_atime);
57         printf("mtime     : %d\n", st.st_mtime);
58         printf("ctime     : %d\n", st.st_ctime);
59         #endif
60
61         retval = symlink("/dir1/random.txt", "/dir2/sym-test");
62         if (retval < 0)
63                 printf("WARNING! Symlink creation failed!\n");
64         retval = readlink("/dir2/sym-test", rbuf, 256);
65         if (retval < 0)
66                 printf("WARNING! Readlink failed!\n");
67         else
68                 printf("Readlink read %d bytes\n", retval);
69         
70         /* Readdir tests: two ways to do it: */
71         DIR *dir = opendir("/dir1/");
72         struct dirent dirent_r, *dirent, *result = 0;
73         #if 0
74         dirent = readdir(dir);
75         printf("Readdir: d_ino %lld, d_off: %lld, d_reclen: %d, d_name: %s\n",
76                dirent->d_ino, dirent->d_off, dirent->d_reclen, dirent->d_name);
77         printf("TAKE TWO:\n-----------\n");
78         dirent = readdir(dir);
79         printf("Readdir: d_ino %lld, d_off: %lld, d_reclen: %d, d_name: %s\n",
80                dirent->d_ino, dirent->d_off, dirent->d_reclen, dirent->d_name);
81         #endif
82
83         retval = readdir_r(dir, &dirent_r, &result);
84         if (retval > 0)
85                 printf("WARNING! Readdir_r failed!, retval %d\n", retval);
86         if (!result)
87                 printf("End of the directory\n");
88         else
89                 printf("Dirent name: %s\n", result->d_name);
90         printf("TAKE TWO:\n-----------\n");
91         memset(&dirent_r, 0, sizeof(struct dirent));
92         retval = readdir_r(dir, &dirent_r, &result);
93         if (retval > 0)
94                 printf("WARNING! Readdir_r failed!, retval %d\n", retval);
95         if (!result)
96                 printf("End of the directory\n");
97         else
98                 printf("Dirent name: %s\n", result->d_name);
99         
100         /* Hardlink tests */
101         printf("Linking to /bin/hello at /dir1/hardhello\n");
102         retval = link("/bin/hello", "/dir1/hardhello");
103         if (retval < 0)
104                 printf("WARNING! Link failed!\n");
105         //breakpoint();
106         printf("Now unlinking /dir1/hardhello\n");
107         retval = unlink("/dir1/hardhello");
108         if (retval < 0)
109                 printf("WARNING! Unlink failed!\n");
110         printf("Linking to /bin/hello at /bin/hardhello2\n");
111         retval = link("/bin/hello", "/bin/hardhello2");
112         if (retval < 0)
113                 printf("WARNING! Link failed!\n");
114         printf("Now unlinking symlink /dir2/sym-test\n");
115         retval = unlink("/dir2/sym-test");
116         if (retval < 0)
117                 printf("WARNING! Unlink failed!\n");
118
119         /* getcwd, on the root dir */
120         char *cwd = getcwd(0, 0);
121         if (!cwd)
122                 printf("WARNING! Couldn't get a CWD!\n");
123         else
124                 printf("Got CWD (/): %s\n", cwd);
125         free(cwd);
126         /* chdir() tests */
127         printf("Testing basic chdir\n");
128         retval = access("dir1/f1.txt", R_OK);
129         if (retval < 0)
130                 printf("WARNING! Access error for dir1/f1.txt!\n");
131         retval = chdir("/dir1");
132         if (retval < 0)
133                 printf("WARNING! Chdir failed for /dir1!\n");
134         retval = access("f1.txt", R_OK);
135         if (retval < 0)
136                 printf("WARNING! Access error for f1.txt!\n");
137         cwd = getcwd(0, 0);
138         if (!cwd)
139                 printf("WARNING! Couldn't get a CWD!\n");
140         else
141                 printf("Got CWD (/dir1/): %s\n", cwd);
142         free(cwd);
143         /* change to a weird directory, see if we can still getcwd() */
144         retval = chdir("../dir2/../dir1/dir1-1");
145         if (retval < 0)
146                 printf("WARNING! Chdir failed for dir1-1!\n");
147         cwd = getcwd(0, 0);
148         if (!cwd)
149                 printf("WARNING! Couldn't get a CWD!\n");
150         else
151                 printf("Got CWD (/dir1/dir1-1/): %s\n", cwd);
152         free(cwd);
153
154         /* Try a chmod() */
155         printf("Trying a chmod\n");
156         retval = chmod("/dir1/dir1-1/f1-1.txt", S_IRWXO);
157         if (retval < 0)
158                 printf("WARNING! chmod failed with %d!\n", retval);
159
160         breakpoint();
161 }