[Samba] Desktop.ini (part) solution

Steve Hardy s.a.hardy at connectux.com
Mon Jan 6 14:45:01 GMT 2003


Aha!

Although I'm not an everyday samba programmer, I have found a descrepancy
between a windows code snippet running on a windows server, and on a samba
(2.2.7a, but also earlier) systems. This causes the dreaded Desktop.ini
problem (yay!) and probably some others too!

The test code in windows:

#include "stdafx.h"
#include <windows.h>

int main(int argc, char* argv[])
{
	HANDLE f;

	f =
CreateFile("H:\\test.x",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FI
LE_ATTRIBUTE_NORMAL,NULL);
	CloseHandle(f);

	f =
CreateFile("H:\\test.x",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FI
LE_ATTRIBUTE_HIDDEN,NULL);
	CloseHandle(f);

	return 0;
}

Now in windows, the first call creates the file 'test.x' with NORMAL
attributes. Then, the second call 'truncates' the file, but with HIDDEN
attributes. If you run this on C: you will find test.x has HIDDEN attributes
after the second call. This is NOT how un*x behaves, as passing a mode with
O_TRUNC has no effect on an existing file (you can test this, the result is
different if you place test.x on a samba share). Also, if you reverse the
operation (ie hidden first, then normal), the file stays hidden, so the
flags seem to be OR'ed with eachother. The quick and dirty fix is to
unlink() the file just before opening it with O_TRUNC and O_CREATE, which
partly corrects the problems as we should really OR the flags. Here is my
quick fix diff from 2.2.7a:

diff -urN samba-2.2.7a/source/smbd/open.c
samba-2.2.7a-truncfix/source/smbd/open.c
--- samba-2.2.7a/source/smbd/open.c	Tue Dec 10 15:58:17 2002
+++ samba-2.2.7a-truncfix/source/smbd/open.c	Mon Jan  6 16:40:30 2003
@@ -910,6 +910,7 @@
 			 * we can do. We also ensure we're not going to create or tuncate
 			 * the file as we only want an access decision at this stage. JRA.
 			 */
+
 			fsp_open = open_file(fsp,conn,fname,psbuf,
 						flags|(flags2&~(O_TRUNC|O_CREAT)),mode,desired_access);

@@ -927,6 +928,13 @@
 		/*
 		 * We exit this block with the share entry *locked*.....
 		 */
+		DEBUG(8,("file %s existed before create\n"));
+		/* This is to fix permissions changing on windows systems */
+		if(flags2 & O_TRUNC) {
+			DEBUG(8,("removing file %s before mode %d, flags %d for permission
reset\n",fname,mode,flags));
+			unlink(fname);
+		}
+
 	}

 	/*







More information about the samba mailing list