+static gboolean
+set_umask_permissions (int fd,
+ GError **err)
+{
+#ifdef G_OS_WIN32
+
+ return TRUE;
+
+#else
+ /* All of this function is just to work around the fact that
+ * there is no way to get the umask without changing it.
+ *
+ * We can't just change-and-reset the umask because that would
+ * lead to a race condition if another thread tried to change
+ * the umask in between the getting and the setting of the umask.
+ * So we have to do the whole thing in a child process.
+ */
+
+ int save_errno;
+ pid_t pid;
+
+ pid = fork ();
+
+ if (pid == -1)
+ {
+ save_errno = errno;
+ g_set_error (err,
+ G_FILE_ERROR,
+ g_file_error_from_errno (save_errno),
+ _("Could not change file mode: fork() failed: %s"),
+ g_strerror (save_errno));
+
+ return FALSE;
+ }
+ else if (pid == 0)
+ {
+ /* child */
+ mode_t mask = umask (0666);
+
+ errno = 0;
+ if (fchmod (fd, 0666 & ~mask) == -1)
+ _exit (errno);
+ else
+ _exit (0);
+
+ return TRUE; /* To quiet gcc */
+ }
+ else
+ {
+ /* parent */
+ int status;
+
+ errno = 0;
+ if (waitpid (pid, &status, 0) == -1)
+ {
+ save_errno = errno;
+
+ g_set_error (err,
+ G_FILE_ERROR,
+ g_file_error_from_errno (save_errno),
+ _("Could not change file mode: waitpid() failed: %s"),
+ g_strerror (save_errno));
+
+ return FALSE;
+ }
+
+ if (WIFEXITED (status))
+ {
+ save_errno = WEXITSTATUS (status);
+
+ if (save_errno == 0)
+ {
+ return TRUE;
+ }
+ else
+ {
+ g_set_error (err,
+ G_FILE_ERROR,
+ g_file_error_from_errno (save_errno),
+ _("Could not change file mode: chmod() failed: %s"),
+ g_strerror (save_errno));
+
+ return FALSE;
+ }
+ }
+ else if (WIFSIGNALED (status))
+ {
+ g_set_error (err,
+ G_FILE_ERROR,
+ G_FILE_ERROR_FAILED,
+ _("Could not change file mode: Child terminated by signal: %s"),
+ g_strsignal (WTERMSIG (status)));
+
+ return FALSE;
+ }
+ else
+ {
+ /* This shouldn't happen */
+ g_set_error (err,
+ G_FILE_ERROR,
+ G_FILE_ERROR_FAILED,
+ _("Could not change file mode: Child terminated abnormally"));
+ return FALSE;
+ }
+ }
+#endif
+}
+