From hika@bsdmon.com  Sat Dec 31 11:42:32 2005
Return-Path: <hika@bsdmon.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 5430316A41F
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 31 Dec 2005 11:42:32 +0000 (GMT)
	(envelope-from hika@bsdmon.com)
Received: from smtp.bsdmon.com (hosting-28.42.rev.fr.colt.net [213.41.42.28])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 4B25043D6E
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 31 Dec 2005 11:42:28 +0000 (GMT)
	(envelope-from hika@bsdmon.com)
Received: from localhost (localhost [127.0.0.1])
	by smtp.bsdmon.com (Postfix) with ESMTP id 6E57E3760B4;
	Sat, 31 Dec 2005 12:42:27 +0100 (CET)
Received: from smtp.bsdmon.com ([127.0.0.1])
 by localhost (freefugu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
 id 45043-01; Sat, 31 Dec 2005 12:41:58 +0100 (CET)
Received: from mail.bsdmon.com (14.128.101-84.rev.gaoland.net [84.101.128.14])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by smtp.bsdmon.com (Postfix) with ESMTP id EBE3737606C;
	Sat, 31 Dec 2005 12:22:59 +0100 (CET)
Received: by sdf1.bsdmon.com (Postfix, from userid 1001)
	id C4B72B853; Sat, 31 Dec 2005 12:22:58 +0100 (CET)
Message-Id: <20051231112258.C4B72B853@sdf1.bsdmon.com>
Date: Sat, 31 Dec 2005 12:22:58 +0100 (CET)
From: Gilbert Cao <hika@bsdmon.com>
Reply-To: Gilbert Cao <hika@bsdmon.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc: Gilbert Cao <hika@bsdmon.com>
Subject: [PATCH] Preserve access and modification time when cp to a smbfs destination path
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         91134
>Category:       kern
>Synopsis:       [smbfs] [patch] Preserve access and modification time when cp to a smbfs destination path
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-fs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Dec 31 11:50:02 GMT 2005
>Closed-Date:    
>Last-Modified:  Mon May 18 04:21:55 UTC 2009
>Originator:     Gilbert Cao <hika@bsdmon.com>
>Release:        FreeBSD 6.0-STABLE i386
>Organization:
BSDMon
>Environment:
On the smb client machine,
FreeBSD sdf1.bsdmon.com 6.0-STABLE FreeBSD 6.0-STABLE #0: Sun Nov 20 16:47:51 CET 2005 root@sdf1.bsdmon.com:/usr/obj/usr/src/sys/SDF1BSD i386
cp utility program ($FreeBSD: src/bin/cp/utils.c,v 1.45.2.1 2005/11/12 21:21:45 csjp Exp $)

On the smb server machine,
FreeBSD bigfugu 5.4-STABLE FreeBSD 5.4-STABLE #6: Sat Aug 20 16:03:24 CEST 2005 root@bigfugu:/usr/obj/usr/src/sys/BIGFUGU i386
Samba port version : samba-3.0.8,1 or above

>Description:
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.

>How-To-Repeat:
On the smb client side, simply
$ mount_smbfs //user@machine/share /path/to/smbmountdir
$ cp -p file /path/to/smbmountdir

and then, compare the file's modification time on both smb client and server side.
The modification time in the smb server side has been set to the current time,
not the source file's modification time.

>Fix:
The following patch has fixed the problem as 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.

--- patch_cp_utils.diff begins here ---
--- ./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);
 }
 
--- patch_cp_utils.diff ends here ---


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->ehaupt 
Responsible-Changed-By: ehaupt 
Responsible-Changed-When: Sat Dec 31 15:56:25 CET 2005 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=91134 
Responsible-Changed-From-To: ehaupt->freebsd-bugs 
Responsible-Changed-By: ehaupt 
Responsible-Changed-When: Sat Dec 31 16:05:37 CET 2005 
Responsible-Changed-Why:  
Back to freebsd-bugs, wrongly taken PR. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=91134 

From: Bruce Evans <bde@zeta.org.au>
To: Gilbert Cao <hika@bsdmon.com>
Cc: FreeBSD-gnats-submit@freebsd.org, freebsd-bugs@freebsd.org
Subject: Re: bin/91134: [PATCH] Preserve access and modification time when
 cp to a smbfs destination path
Date: Sun, 1 Jan 2006 11:06:48 +1100 (EST)

 On Sat, 31 Dec 2005, Gilbert Cao wrote:
 
 >> Fix:
 > The following patch has fixed the problem as 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.
 
 This is a bug in smbfs.  cp only works on POSIX file systems.
 
 > --- patch_cp_utils.diff begins here ---
 > --- ./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);
 > }
 
 It can't be right to always close the file.  This leaves the fd >= 0 case
 in setfile() unused and bogotifies all the code that handles this case,
 even for POSIX file systems where this code just works.  However, I can't
 see any reason to have the fd >= 0 case except to avoid minor races for
 regular files only.
 
 Bruce

From: Gilbert Cao <hika@bsdmon.com>
To: Bruce Evans <bde@zeta.org.au>
Cc: FreeBSD-gnats-submit@freebsd.org, freebsd-bugs@freebsd.org
Subject: Re: bin/91134: [PATCH] Preserve access and modification time when cp to a smbfs destination path
Date: Mon, 2 Jan 2006 20:27:19 +0100

 --6Nae48J/T25AfBN4
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 On Sun, Jan 01, 2006 at 11:06:48AM +1100, Bruce Evans wrote:
 > On Sat, 31 Dec 2005, Gilbert Cao wrote:
 >=20
 > >>Fix:
 > >The following patch has fixed the problem as I have finally found the=20
 > >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, i=
 f=20
 > >its
 > >file descriptor is not closed yet, and this is only the case in a SMB=20
 > >destination path.
 >=20
 > This is a bug in smbfs.  cp only works on POSIX file systems.
 
 OK.
 
 >=20
 > It can't be right to always close the file.  This leaves the fd >=3D 0 ca=
 se
 > in setfile() unused and bogotifies all the code that handles this case,
 > even for POSIX file systems where this code just works.  However, I can't
 > see any reason to have the fd >=3D 0 case except to avoid minor races for
 > regular files only.
 
 I guess I will maintain my own version of cp, until the bug in smbfs will
 be correct, right ?
 I don't know if I will be qualified enough to debug smbfs ;)
 
 >=20
 > Bruce
 
 --=20
 --------------------------------
  (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
 --------------------------------
 
 --6Nae48J/T25AfBN4
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.2 (FreeBSD)
 
 iD8DBQFDuX6WSyQfFTqAEpcRApx9AKC0D3Vw4857cfZzIcsRo3v/JpLuNwCeKUj6
 p2CJJm7CAzbrkDYL+qCG0sk=
 =VeT2
 -----END PGP SIGNATURE-----
 
 --6Nae48J/T25AfBN4--
Responsible-Changed-From-To: freebsd-bugs->freebsd-fs 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Mon May 18 04:21:44 UTC 2009 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=91134 
>Unformatted:
