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 <fcntl.h> 17570af302Sopenharmony_ci#include <stdio.h> 18570af302Sopenharmony_ci#include <sys/stat.h> 19570af302Sopenharmony_ci#include <time.h> 20570af302Sopenharmony_ci#include <utime.h> 21570af302Sopenharmony_ci#include "test.h" 22570af302Sopenharmony_ci 23570af302Sopenharmony_ciextern int __utime64(const char *, const struct utimbuf *); 24570af302Sopenharmony_ci 25570af302Sopenharmony_ci/** 26570af302Sopenharmony_ci * @tc.name : utime_0100 27570af302Sopenharmony_ci * @tc.desc : Specify file modification and access times 28570af302Sopenharmony_ci * @tc.level : Level 0 29570af302Sopenharmony_ci */ 30570af302Sopenharmony_civoid utime_0100(void) 31570af302Sopenharmony_ci{ 32570af302Sopenharmony_ci int fd; 33570af302Sopenharmony_ci char file[] = "/data/utime1.txt"; 34570af302Sopenharmony_ci struct utimbuf ubuf; 35570af302Sopenharmony_ci struct stat info; 36570af302Sopenharmony_ci 37570af302Sopenharmony_ci if ((fd = creat(file, S_IWUSR)) < 0) { 38570af302Sopenharmony_ci t_error("%s creat failed", __func__); 39570af302Sopenharmony_ci } else { 40570af302Sopenharmony_ci close(fd); 41570af302Sopenharmony_ci stat(file, &info); 42570af302Sopenharmony_ci ubuf.modtime = 0; 43570af302Sopenharmony_ci time(&ubuf.actime); 44570af302Sopenharmony_ci if (utime(file, &ubuf) != 0) { 45570af302Sopenharmony_ci t_error("%s utime failed", __func__); 46570af302Sopenharmony_ci } else { 47570af302Sopenharmony_ci stat(file, &info); 48570af302Sopenharmony_ci if (info.st_mtim.tv_sec != 0) { 49570af302Sopenharmony_ci t_error("%s modify file time failed", __func__); 50570af302Sopenharmony_ci } 51570af302Sopenharmony_ci } 52570af302Sopenharmony_ci } 53570af302Sopenharmony_ci remove(file); 54570af302Sopenharmony_ci} 55570af302Sopenharmony_ci 56570af302Sopenharmony_ci/** 57570af302Sopenharmony_ci * @tc.name : utime_0200 58570af302Sopenharmony_ci * @tc.desc : When times is a NULL pointer, the access and modification times are set to the current time 59570af302Sopenharmony_ci * @tc.level : Level 1 60570af302Sopenharmony_ci */ 61570af302Sopenharmony_civoid utime_0200(void) 62570af302Sopenharmony_ci{ 63570af302Sopenharmony_ci const char *path = "/data/utime.txt"; 64570af302Sopenharmony_ci int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664); 65570af302Sopenharmony_ci if (fd == -1) { 66570af302Sopenharmony_ci t_error("%s write create file error", __func__); 67570af302Sopenharmony_ci return; 68570af302Sopenharmony_ci } 69570af302Sopenharmony_ci close(fd); 70570af302Sopenharmony_ci struct stat buf1; 71570af302Sopenharmony_ci struct stat buf2; 72570af302Sopenharmony_ci time_t t_mold, t_aold, t_now, t_mnew, t_anew; 73570af302Sopenharmony_ci stat(path, &buf1); 74570af302Sopenharmony_ci 75570af302Sopenharmony_ci t_mold = buf1.st_mtime; 76570af302Sopenharmony_ci t_aold = buf1.st_atime; 77570af302Sopenharmony_ci t_now = time(NULL); 78570af302Sopenharmony_ci if (utime(path, NULL) != 0) { 79570af302Sopenharmony_ci stat(path, &buf2); 80570af302Sopenharmony_ci t_mnew = buf2.st_mtime; 81570af302Sopenharmony_ci t_anew = buf2.st_atime; 82570af302Sopenharmony_ci if (t_mnew != t_now && t_anew != t_now) { 83570af302Sopenharmony_ci t_error("%s utime failed", __func__); 84570af302Sopenharmony_ci } 85570af302Sopenharmony_ci } 86570af302Sopenharmony_ci remove(path); 87570af302Sopenharmony_ci} 88570af302Sopenharmony_ci 89570af302Sopenharmony_ci/** 90570af302Sopenharmony_ci * @tc.name : utime64_0100 91570af302Sopenharmony_ci * @tc.desc : Specify file modification and access times 92570af302Sopenharmony_ci * @tc.level : Level 0 93570af302Sopenharmony_ci */ 94570af302Sopenharmony_civoid utime64_0100(void) 95570af302Sopenharmony_ci{ 96570af302Sopenharmony_ci int fd; 97570af302Sopenharmony_ci char file[] = "/data/utime641.txt"; 98570af302Sopenharmony_ci struct utimbuf ubuf; 99570af302Sopenharmony_ci struct stat info; 100570af302Sopenharmony_ci 101570af302Sopenharmony_ci if ((fd = creat(file, S_IWUSR)) < 0) { 102570af302Sopenharmony_ci t_error("%s creat failed", __func__); 103570af302Sopenharmony_ci } else { 104570af302Sopenharmony_ci close(fd); 105570af302Sopenharmony_ci stat(file, &info); 106570af302Sopenharmony_ci ubuf.modtime = 0; 107570af302Sopenharmony_ci time(&ubuf.actime); 108570af302Sopenharmony_ci if (__utime64(file, &ubuf) != 0) { 109570af302Sopenharmony_ci t_error("%s __utime64 failed", __func__); 110570af302Sopenharmony_ci } else { 111570af302Sopenharmony_ci stat(file, &info); 112570af302Sopenharmony_ci if (info.st_mtim.tv_sec != 0) { 113570af302Sopenharmony_ci t_error("%s modify file time failed", __func__); 114570af302Sopenharmony_ci } 115570af302Sopenharmony_ci } 116570af302Sopenharmony_ci } 117570af302Sopenharmony_ci remove(file); 118570af302Sopenharmony_ci} 119570af302Sopenharmony_ci 120570af302Sopenharmony_ciint main(int argc, char *argv[]) 121570af302Sopenharmony_ci{ 122570af302Sopenharmony_ci utime_0100(); 123570af302Sopenharmony_ci utime_0200(); 124570af302Sopenharmony_ci utime64_0100(); 125570af302Sopenharmony_ci return t_status; 126570af302Sopenharmony_ci}