1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <time.h> #include <string.h> #include <dirent.h> #include <errno.h>
// 文件时间信息结构体 struct file_time_info { char filename[256]; time_t atime; time_t mtime; off_t size; };
// 获取文件时间信息 int get_file_time_info(const char *filepath, struct file_time_info *info) { struct stat file_stat; if (stat(filepath, &file_stat) == -1) { return -1; } strncpy(info->filename, filepath, sizeof(info->filename) - 1); info->filename[sizeof(info->filename) - 1] = '\0'; info->atime = file_stat.st_atime; info->mtime = file_stat.st_mtime; info->size = file_stat.st_size; return 0; }
// 设置文件时间戳 int set_file_times(const char *filepath, time_t new_atime, time_t new_mtime) { int fd; struct timespec times[2]; fd = open(filepath, O_WRONLY); if (fd == -1) { // 如果写打开失败,尝试只读打开(某些文件可能只需要读权限) fd = open(filepath, O_RDONLY); if (fd == -1) { return -1; } } times[0].tv_sec = new_atime; times[0].tv_nsec = 0; times[1].tv_sec = new_mtime; times[1].tv_nsec = 0; int result = futimens(fd, times); close(fd); return result; }
// 打印文件时间信息 void print_file_info(const struct file_time_info *info) { printf("文件: %s\n", info->filename); printf(" 大小: %ld 字节\n", (long)info->size); printf(" 访问时间: %s", ctime(&info->atime)); printf(" 修改时间: %s", ctime(&info->mtime)); }
// 将目录中所有文件的时间戳设置为相同时间 int sync_directory_times(const char *dirpath, time_t target_time) { DIR *dir; struct dirent *entry; char filepath[512]; int count = 0; int errors = 0; dir = opendir(dirpath); if (dir == NULL) { perror("opendir"); return -1; } printf("正在同步目录 '%s' 中文件的时间戳...\n", dirpath); printf("目标时间: %s\n", ctime(&target_time)); while ((entry = readdir(dir)) != NULL) { // 跳过当前目录和父目录 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } // 构造完整文件路径 snprintf(filepath, sizeof(filepath), "%s/%s", dirpath, entry->d_name); // 设置文件时间戳 if (set_file_times(filepath, target_time, target_time) == 0) { printf("✓ 成功: %s\n", entry->d_name); count++; } else { printf("✗ 失败: %s (%s)\n", entry->d_name, strerror(errno)); errors++; } } closedir(dir); printf("\n处理完成: %d 个文件成功, %d 个文件失败\n", count, errors); return errors == 0 ? 0 : -1; }
int main(int argc, char *argv[]) { if (argc < 2) { printf("用法: %s <文件或目录路径> [目标时间戳]\n", argv[0]); printf("示例: %s /path/to/file\n", argv[0]); printf(" %s /path/to/directory 1672531200\n", argv[0]); return 1; } const char *path = argv[1]; time_t target_time; // 如果提供了目标时间戳,使用它;否则使用当前时间 if (argc > 2) { target_time = (time_t)atol(argv[2]); } else { target_time = time(NULL); } // 检查路径是文件还是目录 struct stat path_stat; if (stat(path, &path_stat) == -1) { perror("stat"); return 1; } if (S_ISDIR(path_stat.st_mode)) { // 处理目录 return sync_directory_times(path, target_time); } else { // 处理单个文件 struct file_time_info before_info, after_info; printf("=== 文件时间戳修改工具 ===\n\n"); // 获取修改前的信息 if (get_file_time_info(path, &before_info) == -1) { perror("获取文件信息失败"); return 1; } printf("修改前:\n"); print_file_info(&before_info); // 设置新的时间戳 if (set_file_times(path, target_time, target_time) == -1) { perror("设置时间戳失败"); return 1; } // 获取修改后的信息 if (get_file_time_info(path, &after_info) == -1) { perror("获取修改后信息失败"); return 1; } printf("\n修改后:\n"); print_file_info(&after_info); printf("\n时间戳修改完成!\n"); } return 0; }
|