if (utimes(test_file, new_times) == -1) { perror("utimes"); unlink(test_file); exit(EXIT_FAILURE); } printf("Set atime to %s", ctime(&fixed_time_sec)); // ctime adds newline printf("Set mtime to %s", ctime(&fixed_time_sec)); printf("Note: Microseconds are set but not displayed by ctime.\n\n");
printf("3. Timestamps after utimes (fixed time):\n"); print_file_times(test_file);
// 4. 使用 utimes(NULL) 将时间设置为当前时间 printf("4. Setting timestamps to CURRENT time using utimes(NULL)...\n"); sleep(3); // 等待几秒,确保当前时间不同 if (utimes(test_file, NULL) == -1) { perror("utimes(NULL)"); unlink(test_file); exit(EXIT_FAILURE); } printf("Timestamps updated to current time.\n\n");
printf("5. Timestamps after utimes(NULL):\n"); print_file_times(test_file);
// 6. 比较 utime (旧版,秒级精度) printf("6. --- Comparing with older utime() ---\n"); structutimbuf old_times; old_times.actime = fixed_time_sec; // 访问时间 old_times.modtime = fixed_time_sec; // 修改时间 printf("Setting timestamps back to fixed time using utime() (second precision)...\n"); if (utime(test_file, &old_times) == -1) { perror("utime"); } else { printf("utime() succeeded.\n"); print_file_times(test_file); printf("Note: atime and mtime are now at second precision.\n\n"); }
// 8. 清理 printf("8. --- Cleaning up ---\n"); if (unlink(test_file) == 0) { printf("Deleted test file '%s'.\n", test_file); } else { perror("unlink"); }
printf("\n--- Summary ---\n"); printf("1. utimes(filename, times[2]): Sets atime and mtime for a file via its path.\n"); printf("2. Precision is up to microseconds (tv_usec).\n"); printf("3. If times is NULL, both atime and mtime are set to the current time.\n"); printf("4. Related functions:\n"); printf(" - utime(): Older, second-precision version.\n"); printf(" - lutimes(): Modifies symlink itself, not target.\n"); printf(" - futimes(): Modifies file via file descriptor.\n"); printf(" - futimens() / utimensat(): Modern, nanosecond-precision, more flexible (recommended).\n");
---Demonstratingutimes--- Created test file:utimes_test_file.txt
1. Initial timestamps: File:utimes_test_file.txt Last Status Change (ctime):FriOct2711:00:002023 Last Modification (mtime):FriOct2711:00:002023 Last Access (atime):FriOct2711:00:002023
3. Timestamps after utimes (fixed time): File:utimes_test_file.txt Last Status Change (ctime):FriOct2711:00:002023 Last Modification (mtime):FriOct2710:00:002023 Last Access (atime):FriOct2710:00:002023
5. Timestamps after utimes(NULL): File:utimes_test_file.txt Last Status Change (ctime):FriOct2711:00:032023 Last Modification (mtime):FriOct2711:00:032023 Last Access (atime):FriOct2711:00:032023
6.---Comparingwitholderutime()--- Settingtimestampsbacktofixedtimeusingutime()(secondprecision)... utime()succeeded. File:utimes_test_file.txt Last Status Change (ctime):FriOct2711:00:032023 Last Modification (mtime):FriOct2710:00:002023 Last Access (atime):FriOct2710:00:002023 Note:atimeandmtimearenowatsecondprecision.
7.---Comparingwithfutimes()--- Settingtimestampsusingfutimes()viafiledescriptor... futimes()succeeded. File:utimes_test_file.txt Last Status Change (ctime):FriOct2711:00:032023 Last Modification (mtime):FriOct2712:00:002023 Last Access (atime):FriOct2711:00:002023