1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <time.h>
22 #include "test.h"
23
24 extern int __utimes_time64(const char *, const struct timeval [2]);
25
26 /**
27 * @tc.name : utimes_0100
28 * @tc.desc : Change file last access and modification times
29 * @tc.level : Level 0
30 */
utimes_0100(void)31 void utimes_0100(void)
32 {
33 const char *path = "/data/utimes.txt";
34 int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664);
35 if (fd == -1) {
36 t_error("%s write create file error", __func__);
37 return;
38 }
39 close(fd);
40 struct stat buf1;
41 struct stat buf2;
42 time_t t_mold, t_aold, t_now, t_new;
43 stat(path, &buf1);
44
45 t_mold = buf1.st_mtime;
46 t_aold = buf1.st_atime;
47 t_now = time(NULL);
48 if (utimes(path, NULL) != 0) {
49 t_error("%s utimes failed", __func__);
50 } else {
51 stat(path, &buf2);
52 t_new = buf2.st_mtime;
53 if (t_new != t_now) {
54 t_error("%s utimes failed", __func__);
55 }
56 }
57 remove(path);
58 }
59
60 /**
61 * @tc.name : utimes_0200
62 * @tc.desc : Specify time to change file last access and modification time
63 * @tc.level : Level 1
64 */
utimes_0200(void)65 void utimes_0200(void)
66 {
67 const char *path = "/data/utimes.txt";
68 int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664);
69 if (fd == -1) {
70 t_error("%s write create file error", __func__);
71 return;
72 }
73 close(fd);
74 struct stat st;
75 struct timeval tv[2] = {{1, 0}, {1, 0}};
76
77 int result = utimes(path, tv);
78 if (result != 0) {
79 t_error("%s utimes failed", __func__);
80 } else {
81 stat(path, &st);
82 if (st.st_atime != tv[0].tv_sec && st.st_mtime != tv[1].tv_sec) {
83 t_error("%s utimes failed", __func__);
84 }
85 }
86 remove(path);
87 }
88
89 /**
90 * @tc.name : utimes_time64_0100
91 * @tc.desc : Change file last access and modification times
92 * @tc.level : Level 0
93 */
utimes_time64_0100(void)94 void utimes_time64_0100(void)
95 {
96 const char *path = "/data/utimes_time64.txt";
97 int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664);
98 if (fd == -1) {
99 t_error("%s write create file error", __func__);
100 return;
101 }
102 close(fd);
103 struct stat buf1;
104 struct stat buf2;
105 time_t t_mold, t_aold, t_now, t_new;
106 stat(path, &buf1);
107
108 t_mold = buf1.st_mtime;
109 t_aold = buf1.st_atime;
110 t_now = time(NULL);
111 if (__utimes_time64(path, NULL) != 0) {
112 t_error("%s __utimes_time64 failed", __func__);
113 } else {
114 stat(path, &buf2);
115 t_new = buf2.st_mtime;
116 if (t_new != t_now) {
117 t_error("%s __utimes_time64 failed", __func__);
118 }
119 }
120 remove(path);
121 }
122
main(int argc,char * argv[])123 int main(int argc, char *argv[])
124 {
125 utimes_0100();
126 utimes_0200();
127 utimes_time64_0100();
128 return t_status;
129 }