1570af302Sopenharmony_ci/* 2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4570af302Sopenharmony_ci * you may not use this file except in compliance with the License. 5570af302Sopenharmony_ci * You may obtain a copy of the License at 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12570af302Sopenharmony_ci * See the License for the specific language governing permissions and 13570af302Sopenharmony_ci * limitations under the License. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci#include <stdio.h> 17570af302Sopenharmony_ci#include <fcntl.h> 18570af302Sopenharmony_ci#include <stdio.h> 19570af302Sopenharmony_ci#include <sys/stat.h> 20570af302Sopenharmony_ci#include <sys/time.h> 21570af302Sopenharmony_ci#include <time.h> 22570af302Sopenharmony_ci#include "test.h" 23570af302Sopenharmony_ci 24570af302Sopenharmony_ciextern int __utimes_time64(const char *, const struct timeval [2]); 25570af302Sopenharmony_ci 26570af302Sopenharmony_ci/** 27570af302Sopenharmony_ci * @tc.name : utimes_0100 28570af302Sopenharmony_ci * @tc.desc : Change file last access and modification times 29570af302Sopenharmony_ci * @tc.level : Level 0 30570af302Sopenharmony_ci */ 31570af302Sopenharmony_civoid utimes_0100(void) 32570af302Sopenharmony_ci{ 33570af302Sopenharmony_ci const char *path = "/data/utimes.txt"; 34570af302Sopenharmony_ci int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664); 35570af302Sopenharmony_ci if (fd == -1) { 36570af302Sopenharmony_ci t_error("%s write create file error", __func__); 37570af302Sopenharmony_ci return; 38570af302Sopenharmony_ci } 39570af302Sopenharmony_ci close(fd); 40570af302Sopenharmony_ci struct stat buf1; 41570af302Sopenharmony_ci struct stat buf2; 42570af302Sopenharmony_ci time_t t_mold, t_aold, t_now, t_new; 43570af302Sopenharmony_ci stat(path, &buf1); 44570af302Sopenharmony_ci 45570af302Sopenharmony_ci t_mold = buf1.st_mtime; 46570af302Sopenharmony_ci t_aold = buf1.st_atime; 47570af302Sopenharmony_ci t_now = time(NULL); 48570af302Sopenharmony_ci if (utimes(path, NULL) != 0) { 49570af302Sopenharmony_ci t_error("%s utimes failed", __func__); 50570af302Sopenharmony_ci } else { 51570af302Sopenharmony_ci stat(path, &buf2); 52570af302Sopenharmony_ci t_new = buf2.st_mtime; 53570af302Sopenharmony_ci if (t_new != t_now) { 54570af302Sopenharmony_ci t_error("%s utimes failed", __func__); 55570af302Sopenharmony_ci } 56570af302Sopenharmony_ci } 57570af302Sopenharmony_ci remove(path); 58570af302Sopenharmony_ci} 59570af302Sopenharmony_ci 60570af302Sopenharmony_ci/** 61570af302Sopenharmony_ci * @tc.name : utimes_0200 62570af302Sopenharmony_ci * @tc.desc : Specify time to change file last access and modification time 63570af302Sopenharmony_ci * @tc.level : Level 1 64570af302Sopenharmony_ci */ 65570af302Sopenharmony_civoid utimes_0200(void) 66570af302Sopenharmony_ci{ 67570af302Sopenharmony_ci const char *path = "/data/utimes.txt"; 68570af302Sopenharmony_ci int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664); 69570af302Sopenharmony_ci if (fd == -1) { 70570af302Sopenharmony_ci t_error("%s write create file error", __func__); 71570af302Sopenharmony_ci return; 72570af302Sopenharmony_ci } 73570af302Sopenharmony_ci close(fd); 74570af302Sopenharmony_ci struct stat st; 75570af302Sopenharmony_ci struct timeval tv[2] = {{1, 0}, {1, 0}}; 76570af302Sopenharmony_ci 77570af302Sopenharmony_ci int result = utimes(path, tv); 78570af302Sopenharmony_ci if (result != 0) { 79570af302Sopenharmony_ci t_error("%s utimes failed", __func__); 80570af302Sopenharmony_ci } else { 81570af302Sopenharmony_ci stat(path, &st); 82570af302Sopenharmony_ci if (st.st_atime != tv[0].tv_sec && st.st_mtime != tv[1].tv_sec) { 83570af302Sopenharmony_ci t_error("%s utimes failed", __func__); 84570af302Sopenharmony_ci } 85570af302Sopenharmony_ci } 86570af302Sopenharmony_ci remove(path); 87570af302Sopenharmony_ci} 88570af302Sopenharmony_ci 89570af302Sopenharmony_ci/** 90570af302Sopenharmony_ci * @tc.name : utimes_time64_0100 91570af302Sopenharmony_ci * @tc.desc : Change file last access and modification times 92570af302Sopenharmony_ci * @tc.level : Level 0 93570af302Sopenharmony_ci */ 94570af302Sopenharmony_civoid utimes_time64_0100(void) 95570af302Sopenharmony_ci{ 96570af302Sopenharmony_ci const char *path = "/data/utimes_time64.txt"; 97570af302Sopenharmony_ci int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664); 98570af302Sopenharmony_ci if (fd == -1) { 99570af302Sopenharmony_ci t_error("%s write create file error", __func__); 100570af302Sopenharmony_ci return; 101570af302Sopenharmony_ci } 102570af302Sopenharmony_ci close(fd); 103570af302Sopenharmony_ci struct stat buf1; 104570af302Sopenharmony_ci struct stat buf2; 105570af302Sopenharmony_ci time_t t_mold, t_aold, t_now, t_new; 106570af302Sopenharmony_ci stat(path, &buf1); 107570af302Sopenharmony_ci 108570af302Sopenharmony_ci t_mold = buf1.st_mtime; 109570af302Sopenharmony_ci t_aold = buf1.st_atime; 110570af302Sopenharmony_ci t_now = time(NULL); 111570af302Sopenharmony_ci if (__utimes_time64(path, NULL) != 0) { 112570af302Sopenharmony_ci t_error("%s __utimes_time64 failed", __func__); 113570af302Sopenharmony_ci } else { 114570af302Sopenharmony_ci stat(path, &buf2); 115570af302Sopenharmony_ci t_new = buf2.st_mtime; 116570af302Sopenharmony_ci if (t_new != t_now) { 117570af302Sopenharmony_ci t_error("%s __utimes_time64 failed", __func__); 118570af302Sopenharmony_ci } 119570af302Sopenharmony_ci } 120570af302Sopenharmony_ci remove(path); 121570af302Sopenharmony_ci} 122570af302Sopenharmony_ci 123570af302Sopenharmony_ciint main(int argc, char *argv[]) 124570af302Sopenharmony_ci{ 125570af302Sopenharmony_ci utimes_0100(); 126570af302Sopenharmony_ci utimes_0200(); 127570af302Sopenharmony_ci utimes_time64_0100(); 128570af302Sopenharmony_ci return t_status; 129570af302Sopenharmony_ci}