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