• 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 <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <sys/stat.h>
20 
21 #include "test.h"
22 
23 const char *path = "/data/tests/libc-test/src/file.txt";
24 const long sec = 123840;
25 
26 extern int __utimensat_time64(int, const char *, const struct timespec [2], int);
27 
28 /**
29  * @tc.name      : utimensat_0100
30  * @tc.desc      : change file timestamps with nanosecond precision
31  * @tc.level     : Level 0
32  */
utimensat_0100(void)33 void utimensat_0100(void)
34 {
35     int fd = open(path, O_RDWR | O_CREAT);
36     struct timespec times[] = {{.tv_sec = 0}, {.tv_sec = sec}};
37 
38     int result = utimensat(fd, path, times, 0);
39     if (result != 0) {
40         t_error("%s failed: result = %d\n", __func__, result);
41     }
42 
43     close(fd);
44 
45     struct stat statbuf;
46     result = stat(path, &statbuf);
47     if (result != 0) {
48         t_error("%s failed: result = %d\n", __func__, result);
49     }
50 
51     if (statbuf.st_mtim.tv_sec != sec) {
52         t_error("%s failed: statbuf.st_mtim.tv_sec = %ld\n", __func__, statbuf.st_mtim.tv_sec);
53     }
54 
55     remove(path);
56 }
57 
58 /**
59  * @tc.name      : utimensat_0200
60  * @tc.desc      : change file timestamps with invalid parameters
61  * @tc.level     : Level 2
62  */
utimensat_0200(void)63 void utimensat_0200(void)
64 {
65     errno = 0;
66     int result = utimensat(-1, NULL, NULL, 0);
67     if (result == 0) {
68         t_error("%s failed: result = %d\n", __func__, result);
69     }
70 
71     if (errno != EBADF) {
72         t_error("%s failed: errno = %d\n", __func__, errno);
73     }
74 }
75 
76 /**
77  * @tc.name      : utimensat_time64_0100
78  * @tc.desc      : change file timestamps with nanosecond precision
79  * @tc.level     : Level 0
80  */
utimensat_time64_0100(void)81 void utimensat_time64_0100(void)
82 {
83     int fd = open(path, O_RDWR | O_CREAT);
84     struct timespec times[] = {{.tv_sec = 0}, {.tv_sec = sec}};
85 
86     int result = __utimensat_time64(fd, path, times, 0);
87     if (result != 0) {
88         t_error("%s failed: result = %d\n", __func__, result);
89     }
90 
91     close(fd);
92 
93     struct stat statbuf;
94     result = stat(path, &statbuf);
95     if (result != 0) {
96         t_error("%s failed: result = %d\n", __func__, result);
97     }
98 
99     if (statbuf.st_mtim.tv_sec != sec) {
100         t_error("%s failed: statbuf.st_mtim.tv_sec = %ld\n", __func__, statbuf.st_mtim.tv_sec);
101     }
102 
103     remove(path);
104 }
105 
main(int argc,char * argv[])106 int main(int argc, char *argv[])
107 {
108     utimensat_0100();
109     utimensat_0200();
110     utimensat_time64_0100();
111 
112     return t_status;
113 }
114