2 * Simple test program -- demonstrating use of IMFS
14 #include <rtems/libio.h>
15 #include <yaffs/rtems_yaffs.h>
17 void set_uint8_t_buffer(uint8_t *buf, uint32_t n, uint8_t start, uint8_t inc)
27 void make_test_file_name(char *out, int out_size, char *root_path, char *dir, char *file, int index)
30 snprintf(out, out_size, "%s/%s/%s-%d",
31 root_path, dir, file, index);
33 snprintf(out, out_size, "%s/%s/%s",
34 root_path, dir, file);
37 void make_test_dir_name(char *out, int out_size, char *root_path, char *dir)
39 snprintf(out, out_size, "%s/%s", root_path, dir);
42 void dump_directory_tree_worker(const char *dname,int recursive)
52 printf("opendir failed\n");
54 while((de = readdir(d)) != NULL) {
55 sprintf(str,"%s/%s",dname,de->d_name);
59 printf("%s inode %d length %d mode 0%o ",
60 str, (int)s.st_ino, (int)s.st_size, s.st_mode);
61 switch(s.st_mode & S_IFMT) {
62 case S_IFREG: printf("data file"); break;
63 case S_IFDIR: printf("directory"); break;
64 case S_IFLNK: printf("symlink -->");
65 if(readlink(str,str,100) < 0)
70 default: printf("unknown mode"); break;
75 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
76 dump_directory_tree_worker(str,1);
82 static void dump_directory_tree(const char *dname)
84 printf("Directory tree of %s\n", dname);
85 dump_directory_tree_worker(dname,1);
89 void recursively_delete(char *objname)
97 //printf("deleting %s\n", objname);
100 switch(s.st_mode & S_IFMT) {
102 printf("delete data file %s returns %d\n",
103 objname, unlink(objname));
106 printf("delete symlink %s returns %d\n",
107 objname, unlink(objname));
110 d = opendir(objname);
112 printf("opendir failed\n");
114 while((de = readdir(d)) != NULL) {
115 snprintf(str, sizeof(str), "%s/%s",
116 objname, de->d_name);
117 recursively_delete(str);
121 printf("delete directory %s returns %d\n",
122 objname, rmdir(objname));
129 void dumpDir(const char *dname)
131 dump_directory_tree_worker(dname,0);
134 int basic_file_test(char *root_path, char *test_path)
143 make_test_dir_name(dname, sizeof(dname), root_path, test_path);
144 make_test_file_name(fname, sizeof(fname), root_path, test_path, "file", -1);
146 ret = mkdir(dname, 0777);
153 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0777);
154 printf("open %s = %d\n", fname, fd);
156 perror("opening test file");
160 set_uint8_t_buffer(buf, sizeof(buf), 0xAA, 1);
162 ret = write(fd, buf, sizeof(buf));
164 printf("write returned %d\n", ret);
167 perror("writing file");
174 perror("fdatasync problem");
178 ret = lseek(fd, 0, SEEK_END);
180 printf("lseek end ret = %d\n", ret);
182 ret = lseek(fd, 0, SEEK_SET);
183 printf("lseek start ret = %d\n", ret);
185 ret = read(fd, buf2, sizeof(buf2));
187 printf("reading file ret = %d\n", ret);
190 perror("reading file");
194 dump_directory_tree(root_path);
196 if (memcmp(buf, buf2, sizeof(buf)) == 0) {
197 printf("buffers match\n");
200 printf("buffers do not match\n");
208 int create_delete_files_pass(char *root_path, char *test_path, int n_files, int del_when_done)
219 fds = malloc(n_files * sizeof (int));
222 printf("Failed to malloc\n");
227 make_test_dir_name(dname, sizeof(dname), root_path, test_path);
229 recursively_delete(dname);
231 ret = access(dname, F_OK);
232 printf("access of non-existing expects -1 returned %d\n", ret);
235 printf("access should have been -1, was %d\n", ret);
240 ret = mkdir(dname, 0777);
247 ret = access(dname, F_OK);
248 printf("access of existing returned %d\n", ret);
251 perror("access of existing directory");
255 for (i = 0; i < n_files; i++) {
257 make_test_file_name(fname, sizeof(fname), root_path, test_path, "file-", i);
258 make_test_file_name(lname, sizeof(lname), root_path, test_path, "link-", i);
260 ret = symlink(fname, lname);
263 perror("creating symlink");
267 fds[i] = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0777);
268 printf("open %s = %d\n", fname, fds[i]);
271 perror("opening test file");
277 set_uint8_t_buffer(buf, sizeof(buf), 0xAA, 1);
279 for(i = 0; i < n_files; i++) {
280 ret = write(fds[i], buf, sizeof(buf));
281 printf("write returned %d\n", ret);
283 perror("writing file");
288 for(i = 0; i < n_files; i++) {
289 int trunc_size = sizeof(buf2)/2;
291 ret = lseek(fds[i], 0, SEEK_END);
293 printf("lseek end ret = %d\n", ret);
295 ret = lseek(fds[i], 0, SEEK_SET);
296 printf("lseek start ret = %d\n", ret);
298 ret = read(fds[i], buf2, sizeof(buf2));
300 printf("reading file ret = %d\n", ret);
302 perror("reading file");
305 ret = ftruncate(fds[i], trunc_size);
312 ret = lseek(fds[i], 0, SEEK_END);
313 if (ret != trunc_size) {
314 printf("truncated size is %d but lseek returned %d\n",
323 for(i = 0; i < n_files; i++) {
326 perror("closing file");
331 dump_directory_tree(root_path);
333 if (memcmp(buf, buf2, sizeof(buf)) == 0) {
334 printf("buffers match\n");
337 printf("buffers do not match\n");
342 recursively_delete(dname);
349 int create_delete_files(char *root_path, char *test_path, int n_files, int n_passes)
353 for (i = 0; i < n_passes; i++) {
354 printf("\nCreate and Delete Files Pass %d\n", i);
355 ret = create_delete_files_pass(root_path, test_path, n_files, 1);
362 #define YPATH "/yaffs_mount_pt"
363 #define FNAME YPATH"/test"
364 #define DIRNAME YPATH"/dirtest"
366 void check_fail(int ret)
369 printf("Test failed\n");
372 void run_the_test(void)
374 check_fail(basic_file_test(YPATH, "basic-test-dir"));
375 check_fail(create_delete_files(YPATH, "create-del-test-dir", 15, 50));
377 printf("\n\n\nAll Yaffs Tests passed Ok\n\n\n");