0e0dd8b91b48a490d5cbbe72ab0783e3f2f6d4e0
[yaffs2.git] / direct / test-framework / unit_tests / 64_and_32_bit_time / shared / shared.c
1 #include <stdlib.h>
2 #include "yaffsfs.h"
3 #include "shared.h"
4
5 void assert_exit_yaffs(char *fun_name, int fun_return_value){
6         if (fun_return_value < 0) {
7                 printf("yaffs command: %s failed with error code %d \n", fun_name, fun_return_value);
8                 int error_code = yaffsfs_GetLastError();
9                 printf("error code is: %d, which is %s\n", error_code, yaffs_error_to_str(error_code));
10                 printf("exiting program now\n");
11                 exit(-1);
12         }
13 }
14
15 void setup_yaffs() {
16         yaffs_start_up();
17         yaffs_set_trace(0);
18         assert_exit_yaffs("yaffs_mount", yaffs_mount(YAFFS_MOUNT_POINT));
19 }
20
21
22
23 int shared_create(int argc, char *argv[]){
24         
25         if (argc != 3) {
26                 printf("wrong number of arguments\n");
27                 printf("requires $ create file_name time\n");
28                 return TEST_FAIL;
29         }
30         
31         setup_yaffs();  
32         char *file_path = argv[1];
33         int handle = yaffs_open(file_path, O_CREAT | O_RDWR, S_IREAD |S_IWRITE);
34         assert_exit_yaffs ( "yaffs_open", handle);
35
36         assert_exit_yaffs ( "yaffs_close", yaffs_close(handle));
37         
38         
39         //set the time.
40         uint time = atoi(argv[2]);
41
42         struct yaffs_utimbuf new_times;
43         new_times.actime = time;
44         int ret = yaffs_utime(file_path, &new_times);
45         assert_exit_yaffs("yaffs_utime", ret);
46
47         assert_exit_yaffs("yaffs_unmount", yaffs_unmount(YAFFS_MOUNT_POINT));
48         return TEST_FAIL;
49 }
50
51 int shared_validate_file(int argc, char *argv[]){
52         
53         if (argc != 3) {
54                 printf("wrong number of arguments\n");
55                 printf("requires $ create file_name time\n");
56                 return TEST_FAIL;
57         }
58         
59         setup_yaffs();  
60         char *file_path = argv[1];
61         int handle = yaffs_open(file_path, O_RDWR, S_IREAD |S_IWRITE);
62         assert_exit_yaffs ( "yaffs_open", handle);
63
64         //assert the file is the correct size and has the correct time.
65         uint expected_time = atoi(argv[2]);     
66         struct yaffs_stat stat;
67         assert_exit_yaffs ( "yaffs_stat", yaffs_stat(file_path, &stat));
68         assert_exit_yaffs ( "yaffs_close", yaffs_close(handle));
69         if (stat.yst_atime != expected_time) {
70                 printf("stat time is different from expected time\n");
71                 exit(0);
72         }
73         assert_exit_yaffs("yaffs_unmount", yaffs_unmount(YAFFS_MOUNT_POINT));
74         return TEST_FAIL;
75 }