You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Philip Martin <ph...@codematters.co.uk> on 2002/02/12 15:48:56 UTC

[PATCH] use umask in Unix apr_file_attrs_set

Hello

This patch makes apr_file_attrs_set respect the current umask, rather
than unconditionally setting permissions. It's much like my previous
patch, but updated to reflect recent changes in apr_file_attrs_set.

Philip


Index: file_io/unix/filestat.c
===================================================================
RCS file: /home/cvspublic/apr/file_io/unix/filestat.c,v
retrieving revision 1.50
diff -u -r1.50 filestat.c
--- file_io/unix/filestat.c	11 Feb 2002 21:03:44 -0000	1.50
+++ file_io/unix/filestat.c	12 Feb 2002 14:37:45 -0000
@@ -109,6 +109,50 @@
      */
 }
 
+/* Initialize a mutex to serialize thread access to umask(). This needs to
+ * be done in a function because the APR mutex interface doesn't provide
+ * static mutex initialisation, i.e. there is nothing equivalent to
+ * PTHREAD_MUTEX_INITIALIZER. It's difficult to see how it could, given the
+ * need for a pool.
+ */
+#if APR_HAS_THREADS
+static apr_thread_mutex_t *umask_mutex;
+#endif
+apr_status_t apr_unix_setup_umask(apr_pool_t *cont)
+{
+#if APR_HAS_THREADS
+   return apr_thread_mutex_create(&umask_mutex, APR_THREAD_MUTEX_DEFAULT, cont);
+#else
+   return APR_SUCCESS;
+#endif
+}
+
+/* Retrieve the current umask setting */
+static apr_status_t get_umask(apr_fileperms_t *umask_perms, apr_pool_t *cont)
+{
+    apr_status_t status;
+    mode_t umask_raw;
+
+#if APR_HAS_THREADS
+    status = apr_thread_mutex_lock(umask_mutex);
+    if (!APR_STATUS_IS_SUCCESS(status))
+        return status;
+#endif
+
+    umask_raw = umask(0);
+    umask(umask_raw);
+
+#if APR_HAS_THREADS
+    status = apr_thread_mutex_unlock(umask_mutex);
+    if (!APR_STATUS_IS_SUCCESS(status))
+        return status;
+#endif
+
+    *umask_perms = apr_unix_mode2perms(umask_raw);
+
+    return APR_SUCCESS;
+}
+
 APR_DECLARE(apr_status_t) apr_file_info_get(apr_finfo_t *finfo, 
                                             apr_int32_t wanted,
                                             apr_file_t *thefile)
@@ -148,7 +192,6 @@
     if (!APR_STATUS_IS_SUCCESS(status))
         return status;
 
-    /* ### TODO: should added bits be umask'd? */
     if (attr_mask & APR_FILE_ATTR_READONLY)
     {
         if (attributes & APR_FILE_ATTR_READONLY)
@@ -159,10 +202,14 @@
         }
         else
         {
-            /* ### umask this! */
-            finfo.protection |= APR_UWRITE;
-            finfo.protection |= APR_GWRITE;
-            finfo.protection |= APR_WWRITE;
+            apr_fileperms_t current_umask;
+            status = get_umask(&current_umask, cont);
+            if (!APR_STATUS_IS_SUCCESS(status))
+                return status;
+            /* set all the write permissions allowed by the umask */
+            finfo.protection |= (APR_UWRITE & ~current_umask);
+            finfo.protection |= (APR_GWRITE & ~current_umask);
+            finfo.protection |= (APR_WWRITE & ~current_umask);
         }
     }
 
@@ -170,10 +217,14 @@
     {
         if (attributes & APR_FILE_ATTR_EXECUTABLE)
         {
-            /* ### umask this! */
-            finfo.protection |= APR_UEXECUTE;
-            finfo.protection |= APR_GEXECUTE;
-            finfo.protection |= APR_WEXECUTE;
+            apr_fileperms_t current_umask;
+            status = get_umask(&current_umask, cont);
+            if (!APR_STATUS_IS_SUCCESS(status))
+                return status;
+            /* set all the execute permissions allowed by the umask */
+            finfo.protection |= (APR_UEXECUTE & ~current_umask);
+            finfo.protection |= (APR_GEXECUTE & ~current_umask);
+            finfo.protection |= (APR_WEXECUTE & ~current_umask);
         }
         else
         {
Index: include/arch/unix/fileio.h
===================================================================
RCS file: /home/cvspublic/apr/include/arch/unix/fileio.h,v
retrieving revision 1.37
diff -u -r1.37 fileio.h
--- include/arch/unix/fileio.h	21 Nov 2001 04:21:04 -0000	1.37
+++ include/arch/unix/fileio.h	12 Feb 2002 14:37:45 -0000
@@ -151,6 +151,8 @@
 
 apr_status_t apr_unix_file_cleanup(void *);
 
+apr_status_t apr_unix_setup_umask(apr_pool_t *cont);
+
 mode_t apr_unix_perms2mode(apr_fileperms_t perms);
 apr_fileperms_t apr_unix_mode2perms(mode_t mode);
 
Index: misc/unix/start.c
===================================================================
RCS file: /home/cvspublic/apr/misc/unix/start.c,v
retrieving revision 1.61
diff -u -r1.61 start.c
--- misc/unix/start.c	1 Feb 2002 17:27:38 -0000	1.61
+++ misc/unix/start.c	12 Feb 2002 14:37:45 -0000
@@ -133,6 +133,11 @@
     
     apr_signal_init(pool);
 
+#if !defined(OS2) && !defined(WIN32)
+    if ((status = apr_unix_setup_umask(pool)) != APR_SUCCESS)
+        return status;
+#endif
+
     return APR_SUCCESS;
 }