[GRASS-dev] Re: mkdir function

Markus Neteler neteler at itc.it
Wed Dec 13 12:09:01 EST 2006


On Thu, Dec 07, 2006 at 12:02:09PM +0000, Glynn Clements wrote:
> Markus Neteler wrote:
...
> > Probably a search/replace of ' mkdir' would do, using G_mkdir() from
> > ./lib/gis/paths.c
> > ? Or we need to modify G_mkdir() to accepts two parameters.
> 
> No, G_mkdir() is fine.
> 
> On Unix, the mode argument should always be 0777, so the user has
> complete control of the permissions via their umask; anything which
> uses a more restrictive mode is second-guessing the user.
> 
> The only programs which should be setting permissions explicitly are
> those which provide some other mechanism for the user to control the
> permissions.

I have done the changes to G_mkdir() accordingly.
Also added a G_lstat() in lib/gis/paths.c for a similar
problem.

Find attached a diff for review.

The following 3 functions are too complicated for me (!)
to fix:
G_mkdir TODOS:
 lib/g3d/g3dwindowio.c
 lib/gis/mapset_msc.c
 lib/raster/io_fifo.c

Feedback welcome,
Markus
-------------- next part --------------
Index: general/manage/lib/do_copy.c
===================================================================
RCS file: /grassrepository/grass6/general/manage/lib/do_copy.c,v
retrieving revision 1.9
diff -u -r1.9 do_copy.c
--- general/manage/lib/do_copy.c	18 Mar 2006 11:04:16 -0000	1.9
+++ general/manage/lib/do_copy.c	13 Dec 2006 17:03:15 -0000
@@ -9,11 +9,6 @@
 #include <grass/Vect.h>
 #include "list.h"
 
-#ifdef __MINGW32__
-#define mkdir(name, mode) mkdir(name)
-#define lstat(path, sb) stat(path, sb)
-#endif
-
 static int recursive_copy(const char *src, const char *dst);
 
 /*
@@ -119,13 +114,13 @@
 	size_t len, len2;
 	mode_t mode;
 
-	if(lstat(src, &sb))
+	if(G_lstat(src, &sb))
 		return 1;
 
 	/* src is a file */
 	if(!S_ISDIR((mode = sb.st_mode)))
 	{
-		if(!lstat(dst, &sb) && S_ISDIR(sb.st_mode))
+		if(!G_lstat(dst, &sb) && S_ISDIR(sb.st_mode))
 		{
 			const char *p = strrchr(src, '/');
 			/* src => dst/src */
@@ -156,15 +151,15 @@
 
 	/* src is a directory */
 
-	if(lstat(dst, &sb))
+	if(G_lstat(dst, &sb))
 	{
-		if(mkdir(dst, mode & 0777))
+		if(G_mkdir(dst))
 			return 1;
 	}else
 	/* if dst already exists and it's a file, try to remove it */
 	if(!S_ISDIR(sb.st_mode))
 	{
-		if(remove(dst) || mkdir(dst, mode & 0777))
+		if(remove(dst) || G_mkdir(dst))
 			return 1;
 	}
 
Index: lib/db/dbmi_driver/d_mkdir.c
===================================================================
RCS file: /grassrepository/grass6/lib/db/dbmi_driver/d_mkdir.c,v
retrieving revision 1.6
diff -u -r1.6 d_mkdir.c
--- lib/db/dbmi_driver/d_mkdir.c	9 Feb 2006 03:08:55 -0000	1.6
+++ lib/db/dbmi_driver/d_mkdir.c	13 Dec 2006 17:03:16 -0000
@@ -37,13 +37,8 @@
     if (db_isdir(path) == DB_OK)
 	return DB_OK;
 
-#ifdef __MINGW32__
-    if (mkdir (path) == 0)
+    if (G_mkdir (path) == 0)
 	return DB_OK;
-#else
-    if (mkdir (path, mode) == 0)
-	return DB_OK;
-#endif
 
     db_syserror(path);
 
Index: lib/gis/remove.c
===================================================================
RCS file: /grassrepository/grass6/lib/gis/remove.c,v
retrieving revision 2.4
diff -u -r2.4 remove.c
--- lib/gis/remove.c	4 Apr 2006 09:27:13 -0000	2.4
+++ lib/gis/remove.c	13 Dec 2006 17:03:16 -0000
@@ -21,10 +21,6 @@
 #include <dirent.h>
 #include <grass/gis.h>
 
-#ifdef __MINGW32__
-#define lstat(path, sb) stat(path, sb)
-#endif
-
 static int recursive_remove(const char *path);
 
 /*!
@@ -76,7 +72,7 @@
 	struct stat sb;
 	char path2[4096];
 
-	if(lstat(path, &sb))
+	if(G_lstat(path, &sb))
 		return 1;
 	if(!S_ISDIR(sb.st_mode))
 		return remove(path) == 0 ? 0 : 1;
Index: lib/gis/unix_socks.c
===================================================================
RCS file: /grassrepository/grass6/lib/gis/unix_socks.c,v
retrieving revision 1.8
diff -u -r1.8 unix_socks.c
--- lib/gis/unix_socks.c	9 Feb 2006 03:08:56 -0000	1.8
+++ lib/gis/unix_socks.c	13 Dec 2006 17:03:16 -0000
@@ -81,9 +81,9 @@
     
     sprintf (path, "%s-%s-%s", prefix, user, lock);
 
-    if ((status = lstat (path, &theStat)) != 0)
+    if ((status = G_lstat (path, &theStat)) != 0)
     {
-        status = mkdir (path, S_IRWXU);
+        status = G_mkdir (path);
     }
     else 
     {
Index: lib/gis/user_config.c
===================================================================
RCS file: /grassrepository/grass6/lib/gis/user_config.c,v
retrieving revision 2.5
diff -u -r2.5 user_config.c
--- lib/gis/user_config.c	7 Jul 2006 10:16:35 -0000	2.5
+++ lib/gis/user_config.c	13 Dec 2006 17:03:16 -0000
@@ -39,10 +39,6 @@
 #include <errno.h>
 #include <grass/gis.h>
 
-#ifdef __MINGW32__
-# define mkdir(name, mode) ((mkdir) (name))
-#endif
-
 /**************************************************************************
  * _make_toplevel(): make user's toplevel config directory if it doesn't
  * already exist.  Adjust perms to 1700. Returns the toplevel directory
@@ -90,14 +86,14 @@
     sprintf (path, "%s%s", my_passwd->pw_dir, "/.grass");
 #endif
 
-    status = lstat (path, &buf);
+    status = G_lstat (path, &buf);
 
     /* If errno == ENOENT, the directory doesn't exist */
     if (status != 0)
     {
         if (errno == ENOENT)
         {
-            status = mkdir (path, S_IRWXU); /* drwx------ */ 
+            status = G_mkdir (path);
     
             if (status != 0)  /* mkdir failed */
             {
@@ -245,11 +241,11 @@
     {
         sprintf (path, "%s/%s", top, cp);
         errno = 0;
-        status = lstat (path, &buf);
+        status = G_lstat (path, &buf);
         if (status != 0)
         {
             /* the element doesn't exist */
-            status = mkdir (path, S_IRWXU); /* drwx------ */ 
+            status = G_mkdir (path);
             if (status != 0)
             {
                 /* Some kind of problem... */
Index: lib/gis/win32_pipes.c
===================================================================
RCS file: /grassrepository/grass6/lib/gis/win32_pipes.c,v
retrieving revision 2.3
diff -u -r2.3 win32_pipes.c
--- lib/gis/win32_pipes.c	14 Feb 2006 09:02:57 -0000	2.3
+++ lib/gis/win32_pipes.c	13 Dec 2006 17:03:16 -0000
@@ -64,9 +64,9 @@
     path = G_malloc (len);
     sprintf (path, "%s%s", prefix, user);
 
-    if ((status = lstat (path, &theStat)) != 0)
+    if ((status = G_lstat (path, &theStat)) != 0)
     {
-        status = mkdir ( path );
+        status = G_mkdir ( path );
     }
     else 
     {
Index: lib/init/mke_mapset.c
===================================================================
RCS file: /grassrepository/grass6/lib/init/mke_mapset.c,v
retrieving revision 2.5
diff -u -r2.5 mke_mapset.c
--- lib/init/mke_mapset.c	27 Oct 2006 22:27:00 -0000	2.5
+++ lib/init/mke_mapset.c	13 Dec 2006 17:03:16 -0000
@@ -10,10 +10,6 @@
 
 #include <grass/gis.h>
 
-#ifdef __MINGW32__
-# define mkdir(name, mode) ((mkdir) (name))
-#endif
-
 int make_mapset (char *location, char *mapset)
 {
 	char buffer[2048] ;
@@ -22,7 +18,7 @@
 
 /* create the mapset directory */
 	sprintf(buffer,"%s/%s",location, mapset) ;
-	mkdir(buffer, 0777) ;
+	G_mkdir(buffer) ;
 
 /* give the mapset a default window for the entire location */
 	sprintf(buffer,"cat '%s'/PERMANENT/DEFAULT_WIND  > '%s'/'%s'/WIND",
@@ -42,7 +38,7 @@
 	
 /* Make the dbf/ subdirectory */
 	sprintf( buffer, "%s/%s/dbf", location, mapset );
-	if( mkdir( buffer, 0777 ) != 0 )
+	if( G_mkdir( buffer ) != 0 )
 		return -1;
 
 	return(0) ;
Index: raster/r.le/r.le.patch/main.c
===================================================================
RCS file: /grassrepository/grass6/raster/r.le/r.le.patch/main.c,v
retrieving revision 2.6
diff -u -r2.6 main.c
--- raster/r.le/r.le.patch/main.c	17 Oct 2006 12:03:22 -0000	2.6
+++ raster/r.le/r.le.patch/main.c	13 Dec 2006 17:03:17 -0000
@@ -146,7 +146,7 @@
  					   r.le.out subdirectory */
 
   if (choice->wrum != 'm')
-     G_system("mkdir r.le.out");
+     G_mkdir("r.le.out");
 
   patch_fore();
   G_free(choice);
Index: raster/r.le/r.le.pixel/main.c
===================================================================
RCS file: /grassrepository/grass6/raster/r.le/r.le.pixel/main.c,v
retrieving revision 2.7
diff -u -r2.7 main.c
--- raster/r.le/r.le.pixel/main.c	19 Oct 2006 04:22:44 -0000	2.7
+++ raster/r.le/r.le.pixel/main.c	13 Dec 2006 17:03:17 -0000
@@ -120,7 +120,7 @@
  					   r.le.out subdirectory */
 
   if (choice->wrum != 'm')
-     G_system("mkdir r.le.out");
+     G_mkdir("r.le.out");
 
   texture_fore();
   G_free (choice);
Index: raster/r.le/r.le.setup/main.c
===================================================================
RCS file: /grassrepository/grass6/raster/r.le/r.le.setup/main.c,v
retrieving revision 2.11
diff -u -r2.11 main.c
--- raster/r.le/r.le.setup/main.c	19 Oct 2006 04:22:45 -0000	2.11
+++ raster/r.le/r.le.setup/main.c	13 Dec 2006 17:03:17 -0000
@@ -134,7 +134,7 @@
   DIR    *dp;
 
   if(!(dp = opendir("r.le.para")))      
-     G_system("mkdir r.le.para");
+     G_mkdir("r.le.para");
   else 
      closedir(dp);
   return;
Index: raster/r.li/r.li.daemon/daemon.c
===================================================================
RCS file: /grassrepository/grass6/raster/r.li/r.li.daemon/daemon.c,v
retrieving revision 1.4
diff -u -r1.4 daemon.c
--- raster/r.li/r.li.daemon/daemon.c	13 Dec 2006 14:21:40 -0000	1.4
+++ raster/r.li/r.li.daemon/daemon.c	13 Dec 2006 17:03:18 -0000
@@ -117,11 +117,11 @@
 		/*check if ~/.r.li/output exist*/
 		sprintf(out, "%s/.r.li/", getenv("HOME"));	
 		
-		doneDir = mkdir(out, 0755);
+		doneDir = G_mkdir(out);
 		if (doneDir == -1 && errno != EEXIST)
 			G_fatal_error( _("Cannot create .r.li directory"));
 		sprintf(out, "%s/.r.li/output", getenv("HOME"));
-		doneDir = mkdir(out, 0755);
+		doneDir = G_mkdir(out);
 		if (doneDir == -1 && errno != EEXIST)
 			G_fatal_error( _("Cannot create .r.li/output directory"));
 		sprintf(out, "%s/.r.li/output/%s", getenv("HOME"), output);


More information about the grass-dev mailing list