• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fcntl.h>
17 #include <stdio.h>
18 #include <sys/stat.h>
19 #include <time.h>
20 #include <utime.h>
21 #include "test.h"
22 
23 extern int __utime64(const char *, const struct utimbuf *);
24 
25 /**
26  * @tc.name      : utime_0100
27  * @tc.desc      : Specify file modification and access times
28  * @tc.level     : Level 0
29  */
utime_0100(void)30 void utime_0100(void)
31 {
32     int fd;
33     char file[] = "/data/utime1.txt";
34     struct utimbuf ubuf;
35     struct stat info;
36 
37     if ((fd = creat(file, S_IWUSR)) < 0) {
38         t_error("%s creat failed", __func__);
39     } else {
40         close(fd);
41         stat(file, &info);
42         ubuf.modtime = 0;
43         time(&ubuf.actime);
44         if (utime(file, &ubuf) != 0) {
45             t_error("%s utime failed", __func__);
46         } else {
47             stat(file, &info);
48             if (info.st_mtim.tv_sec != 0) {
49                 t_error("%s modify file time failed", __func__);
50             }
51         }
52     }
53     remove(file);
54 }
55 
56 /**
57  * @tc.name      : utime_0200
58  * @tc.desc      : When times is a NULL pointer, the access and modification times are set to the current time
59  * @tc.level     : Level 1
60  */
utime_0200(void)61 void utime_0200(void)
62 {
63     const char *path = "/data/utime.txt";
64     int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664);
65     if (fd == -1) {
66         t_error("%s write create file error", __func__);
67         return;
68     }
69     close(fd);
70     struct stat buf1;
71     struct stat buf2;
72     time_t t_mold, t_aold, t_now, t_mnew, t_anew;
73     stat(path, &buf1);
74 
75     t_mold = buf1.st_mtime;
76     t_aold = buf1.st_atime;
77     t_now = time(NULL);
78     if (utime(path, NULL) != 0) {
79         stat(path, &buf2);
80         t_mnew = buf2.st_mtime;
81         t_anew = buf2.st_atime;
82         if (t_mnew != t_now && t_anew != t_now) {
83             t_error("%s utime failed", __func__);
84         }
85     }
86     remove(path);
87 }
88 
89 /**
90  * @tc.name      : utime64_0100
91  * @tc.desc      : Specify file modification and access times
92  * @tc.level     : Level 0
93  */
utime64_0100(void)94 void utime64_0100(void)
95 {
96     int fd;
97     char file[] = "/data/utime641.txt";
98     struct utimbuf ubuf;
99     struct stat info;
100 
101     if ((fd = creat(file, S_IWUSR)) < 0) {
102         t_error("%s creat failed", __func__);
103     } else {
104         close(fd);
105         stat(file, &info);
106         ubuf.modtime = 0;
107         time(&ubuf.actime);
108         if (__utime64(file, &ubuf) != 0) {
109             t_error("%s __utime64 failed", __func__);
110         } else {
111             stat(file, &info);
112             if (info.st_mtim.tv_sec != 0) {
113                 t_error("%s modify file time failed", __func__);
114             }
115         }
116     }
117     remove(file);
118 }
119 
main(int argc,char * argv[])120 int main(int argc, char *argv[])
121 {
122     utime_0100();
123     utime_0200();
124     utime64_0100();
125     return t_status;
126 }