[PATCH] Kill mktemp()

Andrew Bartlett abartlet at pcug.org.au
Wed Apr 18 02:49:08 GMT 2001


The attached patch finishes off the final use of mktemp() in samba,
making my compiles much less noisy :-).  smbds running with this patch
appear to behave normally, but I'm unsure how to exercise this
particular functionality - particularly as my test machine does not
print  :-).  In any case, the patch still needs a good look over by
somebody much more familiar with reply.c, particularly regarding the
error bailouts.

In any case, its much better here than in my personal patch queue.

Andrew Bartlett
abartlet at pcug.org.au
-- 
Andrew Bartlett
abartlet at pcug.org.au
-------------- next part --------------
Index: source/lib/util.c
===================================================================
RCS file: /cvsroot/samba/source/lib/util.c,v
retrieving revision 1.287.4.14
diff -u -r1.287.4.14 util.c
--- source/lib/util.c	2001/04/16 01:55:08	1.287.4.14
+++ source/lib/util.c	2001/04/18 00:41:36
@@ -1712,33 +1712,22 @@
 	return True;
 }
 
-
 /*****************************************************************
-like mktemp() but make sure that no % characters are used
-% characters are bad for us because of the macro subs
+we now use mktsemp() but we neeed to make sure that no % characters 
+are used % characters are bad for us because of the macro subs
  *****************************************************************/  
-char *smbd_mktemp(char *template)
+char *smbd_clean_for_mkstemp(char *template)
 {
-	char *p = mktemp(template);
+
 	char *p2;
-	SMB_STRUCT_STAT st;
 
-	if (!p) return NULL;
+	if (!template) return NULL;
 
-	while ((p2=strchr(p,'%'))) {
-		p2[0] = 'A';
-		while (sys_stat(p,&st) == 0 && p2[0] < 'Z') {
-			/* damn, it exists */
-			p2[0]++;
-		}
-		if (p2[0] == 'Z') {
-			/* oh well ... better return something */
-			p2[0] = '%';
-			return p;
-		}
+	while ((p2=strchr(template,'%'))) {
+		p2[0] = 'X';
 	}
 
-	return p;
+	return template;
 }
 
 /*****************************************************************
Index: source/smbd/reply.c
===================================================================
RCS file: /cvsroot/samba/source/smbd/reply.c,v
retrieving revision 1.240.2.26
diff -u -r1.240.2.26 reply.c
--- source/smbd/reply.c	2001/04/13 04:09:39	1.240.2.26
+++ source/smbd/reply.c	2001/04/18 00:42:11
@@ -1841,6 +1820,7 @@
   mode_t unixmode;
   BOOL bad_path = False;
   files_struct *fsp;
+  int mkstempfile;
   int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
   SMB_STRUCT_STAT sbuf;
   START_PROFILE(SMBctemp);
@@ -1855,15 +1835,41 @@
   
   unixmode = unix_mode(conn,createmode,fname);
   
-  pstrcpy(fname2,(char *)smbd_mktemp(fname));
-  /* This file should not exist. */
+  pstrcpy(fname2,(char *)smbd_clean_for_mkstemp(fname));
+
+  mkstempfile = smb_mkstemp(fname2);
+
+  if (mkstempfile == -1) {
+   /* This file should not exist. */
+   if((errno == ENOENT) && bad_path)
+    {
+      unix_ERR_class = ERRDOS;
+      unix_ERR_code = ERRbadpath;
+    }
+    END_PROFILE(SMBctemp);
+    return(UNIXERROR(ERRDOS,ERRnoaccess));
+  }
+
+  if (!fchmod(mkstempfile, unixmode)) {
+    /* Error */
+   if((errno == ENOENT) && bad_path)
+    {
+      unix_ERR_class = ERRDOS;
+      unix_ERR_code = ERRbadpath;
+    }
+    END_PROFILE(SMBctemp);
+    return(UNIXERROR(ERRDOS,ERRnoaccess));
+  }    
+
+  close(mkstempfile);
+
   ZERO_STRUCT(sbuf);
   vfs_stat(conn,fname2,&sbuf);
 
   /* Open file in dos compatibility share mode. */
-  /* We should fail if file exists. */
+  /* This file already exists (mkstemp above). */
   fsp = open_file_shared(conn,fname2,&sbuf,SET_DENY_MODE(DENY_FCB)|SET_OPEN_MODE(DOS_OPEN_FCB), 
-                   (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL), unixmode, oplock_request, NULL, NULL);
+                   (0), unixmode, oplock_request, NULL, NULL);
 
   if (!fsp)
   {


More information about the samba-technical mailing list