site_archiver@lists.apple.com Delivered-To: darwin-kernel@lists.apple.com Thread-index: Accg45Vt1D3EZozWEducvAAWy6NyrA== Thread-topic: utimes() no good for symbolic links? User-agent: Microsoft-Entourage/11.2.5.060620 It looks to me like utimes() does not work for symbolic links. If the symlink is valid, it returns 0, but sets both atime and mtime to the current time instead of the time I pass in as argument. If the symlink is broken, it returns -1 and also sets atime and mtime to the current time. The documentation man utimes (2) does not mention that there is any problem with symbolic links. Does anyone know, is this a bug in utimes(), a documentation bug, or my misunderstanding? Also, does anyone know of a good function that works for setting atime and mtime of all files, including symlinks? I don't think that FSSetCatalogInfo works for symlinks either, but it's too late to try tonight. And even if it does, it ain't documented. Thanks, Jerry Krinock ********* TEST PROGRAM ************ Sorry for the kludgey conversion of seconds to years for display, but the only way I know to do that uses Cocoa (NSDate), and I wanted to keep this easily reproducible for the unix folks. #include <stdio.h> #import <CoreServices/CoreServices.h> #include <sys/stat.h> #define SECS_PER_YEAR (365.25 * 24 * 60 * 60) int main (int argc, const char * argv[]) { __darwin_time_t y2k2_secs = (2002.2 - 1970) * SECS_PER_YEAR ; __darwin_time_t y2k3_secs = (2003.2 - 1970) * SECS_PER_YEAR ; struct timeval tval[2] ; // Set atime to some time in 2002. tval[0].tv_sec = y2k2_secs ; tval[0].tv_usec = 0; // Set mtime to some time in 2003 tval[1].tv_sec = y2k3_secs ; tval[1].tv_usec = 0; // Change file to have this atime and mtime int err ; err = utimes("/f1", tval) ; printf("err utimes = %i\n", err) ; // Now read the times back. Use lstat because gives info about // the symlink, not the symlink target, for symlinks struct stat stats ; err = lstat("/f1", &stats) ; printf("f1 atime: %f\n", stats.st_atime/SECS_PER_YEAR + 1970) ; printf("f1 mtime: %f\n", stats.st_mtime/SECS_PER_YEAR + 1970) ; return 0; } ********* RESULTS ********************** SETUP. Since man utimes (2) says that you must be the super user, compile the program and run under sudo. Create a file at path /f1. Case 1. If /f1 is a regular file, I get the desired result, with the times set to sometime in 2002 and 2003: err utimes = 0 f1 atime: 2002.200000 f1 mtime: 2003.200000 Case 2. If /f1 is a valid symlink, utimes() returns no error but the times are incorrectly set to the current times: err utimes = 0 f1 atime: 2006.956309 f1 mtime: 2006.956309 Case 2. If /f1 is a broken symlink, utimes() returns error and the times are incorrectly set to the current times: err utimes = -1 f1 atime: 2006.956309 f1 mtime: 2006.956309 _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-kernel mailing list (Darwin-kernel@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-kernel/site_archiver%40lists.a... This email sent to site_archiver@lists.apple.com
participants (1)
-
Jerry Krinock