Preserve date when cp over smbfs



Hi, the list.

I have recently notice a problem when I copy a file to a SMB mount
directory :

My SMB client is a FreeBSD 6.0-STABLE.
My SMB server is a FreeBSD 5.4-STABLE (with port samba-3.0.8,1).

When I do a 'cp -p somefile /path/to/smbmountdir/anotherfolder', the
access time and modification time are not preserved, even if I use the -p
flag.
The user doing the copy with cp is the owner of /path/to/smbmountdir.

Few days later, I have finally found the problem in the src/bin/cp
source code, especially the utils.c file :
I have found out that utimes() does nothing on the newly created file,
if its file descriptor is not closed yet, and this is only the case in a
SMB destination path.

Here is a small patch I have made.
So pleeaaase, committers, I hope(pray) that the correction to the cp
utility can be applied (with or without the patch), for the next time ;)
I will be very grateful for that, as it is very important for me to
preserve the modification time when I copy some photos to my file
server over smbfs :)

--
--------------------------------
(hika) Gilbert Cao
http://www.miaouirc.com
- MiaouIRC Project 2002-2003
http://www.bsdmon.com
- The BSD DMON Power to serve
IRC : #miaule at IRCNET Network
--------------------------------
--- ./src/bin/cp/utils.c.orig	Sat Nov 12 22:21:45 2005+++ ./src/bin/cp/utils.c	Fri Dec 30 19:23:04 2005
@@ -204,8 +204,6 @@
 	 * to remove it if we created it and its length is 0.
 	 */
 
-	if (pflag && setfile(fs, to_fd))
-		rval = 1;
 	if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
 		rval = 1;
 	(void)close(from_fd);
@@ -213,6 +211,14 @@
 		warn("%s", to.p_path);
 		rval = 1;
 	}
+	/*
+	 * To preserve times in SMB to.p_path, 
+	 * setfile() should be call *AFTER* we have closed the file
+	 * descriptors. As we have closed the descriptors, we should
+	 * pass -1 instead of the `to_fd` value
+	 */
+	if (pflag && setfile(fs, -1))
+		rval = 1;
 	return (rval);
 }
 

Attachment: pgpQFggsoKzMN.pgp
Description: PGP signature



Relevant Pages

  • Re: File + Date
    ... modification time, and access time. ... old C books, as they're all stored away, but a check of the header files ... Steve W. Jackson ...
    (comp.lang.java.programmer)
  • Re: Finding the newest directory
    ... You have access time, modification time, and ... File status time (ctime) is as close as ... stat gives you access to the ctime. ...
    (comp.lang.perl.misc)
  • Re: find/ls most recent files
    ... Matt Price writes: ... I'm looking foractive files, and normally would do ... -atime looks at the access time, ... Depending on whether you really mean the modification time or access ...
    (Debian-User)
  • Re: Attributing an old time stamp for a new file.
    ... Updates modification time (mtime) and access time of ... The first creates an empty file called 'timestamp'; ...
    (comp.lang.ruby)
  • Re: stat and touch
    ... On 2005-11-13, sarah wrote: ... then the access time will be newer than the modification time. ... A Problem-Solution Approach | ...
    (comp.unix.questions)