Skip to content

Instantly share code, notes, and snippets.

@command-Q
Created April 17, 2012 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save command-Q/2403585 to your computer and use it in GitHub Desktop.
Save command-Q/2403585 to your computer and use it in GitHub Desktop.
Patch against Darwin file_cmds 202.2 (10.6.8) making cp, mv, and touch handle file birthtime attribute.
diff -rupN file_cmds-202.2/mv/pathnames.h file_cmds-202.2-birthtime/mv/pathnames.h
--- file_cmds-202.2/mv/pathnames.h 2005-01-20 20:11:19.000000000 -0500
+++ file_cmds-202.2-birthtime/mv/pathnames.h 2012-03-07 11:25:18.000000000 -0500
@@ -36,5 +36,5 @@
#define _PATH_RM "/bin/rm"
#ifdef __APPLE__
-#define _PATH_CP "/bin/cp"
+#define _PATH_CP "/usr/local/bin/cp"
#endif /* __APPLE__ */
diff -rupN file_cmds-202.2/cp/utils.c file_cmds-202.2-birthtime/cp/utils.c
--- file_cmds-202.2/cp/utils.c 2008-05-17 18:21:32.000000000 -0400
+++ file_cmds-202.2-birthtime/cp/utils.c 2012-03-06 15:32:45.000000000 -0500
@@ -350,7 +350,11 @@ copy_special(struct stat *from_stat, int
int
setfile(struct stat *fs, int fd)
{
+#if (defined(__APPLE__) && defined(_DARWIN_FEATURE_64_BIT_INODE)) || defined(BSD)
+ static struct timeval tv[4];
+#else
static struct timeval tv[2];
+#endif /* __APPLE__ || BSD */
struct stat ts;
int rval, gotstat, islink, fdval;
@@ -362,11 +366,17 @@ setfile(struct stat *fs, int fd)
TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
+#if (defined(__APPLE__) && defined(_DARWIN_FEATURE_64_BIT_INODE)) || defined(BSD)
+ tv[2] = tv[0];
+ TIMESPEC_TO_TIMEVAL(&tv[3], &fs->st_birthtimespec);
#ifdef __APPLE__
- if (islink ? 0 : utimes(to.p_path, tv)) {
+ if (!islink && (utimes(to.p_path, tv+2) | utimes(to.p_path, tv))) { /* "|" is not a typo, don't want to short circuit */
+#else /* BSD */
+ if (islink ? (lutimes(to.p_path, tv+2) | lutimes(to.p_path, tv)) : (utimes(to.p_path, tv+2) | utimes(to.p_path, tv))) {
+#endif /* __APPLE__ */
#else
if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
-#endif /* __APPLE__ */
+#endif /* __APPLE__ || BSD */
warn("%sutimes: %s", islink ? "l" : "", to.p_path);
rval = 1;
}
diff -rupN file_cmds-202.2/mv/mv.c file_cmds-202.2-birthtime/mv/mv.c
--- file_cmds-202.2/mv/mv.c 2008-03-12 16:31:12.000000000 -0400
+++ file_cmds-202.2-birthtime/mv/mv.c 2012-03-06 15:32:59.000000000 -0500
@@ -417,8 +417,13 @@ err: if (unlink(to))
warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
tval[0].tv_sec = sbp->st_atime;
- tval[1].tv_sec = sbp->st_mtime;
tval[0].tv_usec = tval[1].tv_usec = 0;
+#if (defined(__APPLE__) && defined(_DARWIN_FEATURE_64_BIT_INODE)) || defined(BSD)
+ tval[1].tv_sec = sbp->st_birthtime;
+ if (utimes(to, tval))
+ warn("%s: set birthtime", to);
+#endif /* __APPLE__ || BSD */
+ tval[1].tv_sec = sbp->st_mtime;
if (utimes(to, tval))
warn("%s: set times", to);
diff -rupN file_cmds-202.2/touch/touch.c file_cmds-202.2-birthtime/touch/touch.c
--- file_cmds-202.2/touch/touch.c 2008-11-19 00:09:09.000000000 -0500
+++ file_cmds-202.2-birthtime/touch/touch.c 2012-03-06 15:32:12.000000000 -0500
@@ -68,13 +68,17 @@ int
main(int argc, char *argv[])
{
struct stat sb;
+#if (defined(__APPLE__) && defined(_DARWIN_FEATURE_64_BIT_INODE)) || defined(BSD)
+ struct timeval tv[4];
+#else
struct timeval tv[2];
+#endif /* __APPLE__ || BSD */
int (*stat_f)(const char *, struct stat *);
int (*utimes_f)(const char *, const struct timeval *);
- int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
+ int aflag, cflag, fflag, mflag, rflag, ch, fd, len, rval, timeset;
char *p;
- aflag = cflag = fflag = mflag = timeset = 0;
+ aflag = cflag = fflag = mflag = rflag = timeset = 0;
stat_f = stat;
utimes_f = utimes;
if (gettimeofday(&tv[0], NULL))
@@ -106,7 +110,7 @@ main(int argc, char *argv[])
mflag = 1;
break;
case 'r':
- timeset = 1;
+ rflag = timeset = 1;
stime_file(optarg, tv);
break;
case 't':
@@ -170,6 +174,9 @@ main(int argc, char *argv[])
TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
/* Try utimes(2). */
+#if (defined(__APPLE__) && defined(_DARWIN_FEATURE_64_BIT_INODE)) || defined(BSD)
+ if(rflag) utimes_f(*argv, tv+2);
+#endif /* __APPLE__ || BSD */
if (!utimes_f(*argv, tv))
continue;
@@ -302,6 +309,10 @@ stime_file(char *fname, struct timeval *
err(1, "%s", fname);
TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
+#if (defined(__APPLE__) && defined(_DARWIN_FEATURE_64_BIT_INODE)) || defined(BSD)
+ tvp[2] = *tvp;
+ TIMESPEC_TO_TIMEVAL(tvp + 3, &sb.st_birthtimespec);
+#endif /* __APPLE__ || BSD */
}
int
@command-Q
Copy link
Author

Link to source tree / tarball
Patch changes the behavior of cp -p, mv across volumes, and touch -r. The second optional patch changes the hardcoded path to cp that exists within mv -- edit to desired install destination.

For cp and mv, this attribute would previously be set equal to the file's modification time. For touch, the minimum of file2 birthtime and file1 modification time given touch -r file1 file2.

With patch, birthtime is copied for cp and mv, and set to lowest of the two files in the case of touch (as birthtime cannot be set forward).

For more information about the birthtime extension, see the linked page of The Design and Implementation of the FreeBSD Operating System

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment