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