* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
#include <errno.h>
#include <string.h>
#include "gioerror.h"
#include "gcancellable.h"
#include "glocalfileoutputstream.h"
+#include "gfileinfo.h"
#include "glocalfileinfo.h"
#ifdef G_OS_UNIX
+#include <unistd.h>
#include "gfiledescriptorbased.h"
#endif
#define O_BINARY 0
#endif
+struct _GLocalFileOutputStreamPrivate {
+ char *tmp_filename;
+ char *original_filename;
+ char *backup_filename;
+ char *etag;
+ guint sync_on_close : 1;
+ guint do_close : 1;
+ int fd;
+};
#ifdef G_OS_UNIX
static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
#define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
#ifdef G_OS_UNIX
G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
+ G_ADD_PRIVATE (GLocalFileOutputStream)
G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
- g_file_descriptor_based_iface_init)
- );
+ g_file_descriptor_based_iface_init))
#else
-G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,);
+G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
+ G_ADD_PRIVATE (GLocalFileOutputStream))
#endif
#define BACKUP_EXTENSION "~"
-struct _GLocalFileOutputStreamPrivate {
- char *tmp_filename;
- char *original_filename;
- char *backup_filename;
- char *etag;
- guint sync_on_close : 1;
- guint do_close : 1;
- int fd;
-};
-
static gssize g_local_file_output_stream_write (GOutputStream *stream,
const void *buffer,
gsize count,
G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
}
-
static void
g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
-
- g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
-
+
gobject_class->finalize = g_local_file_output_stream_finalize;
stream_class->write_fn = g_local_file_output_stream_write;
static void
g_local_file_output_stream_init (GLocalFileOutputStream *stream)
{
- stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
- G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
- GLocalFileOutputStreamPrivate);
+ stream->priv = g_local_file_output_stream_get_instance_private (stream);
stream->priv->do_close = TRUE;
}
if (g_cancellable_set_error_if_cancelled (cancellable, error))
goto err_out;
-#ifdef HAVE_LINK
+#ifdef G_OS_UNIX
/* create original -> backup link, the original is then renamed over */
if (g_unlink (file->priv->backup_filename) != 0 &&
errno != ENOENT)
return G_FILE_OUTPUT_STREAM (stream);
}
+static void
+set_error_from_open_errno (const char *filename,
+ GError **error)
+{
+ int errsv = errno;
+
+ if (errsv == EINVAL)
+ /* This must be an invalid filename, on e.g. FAT */
+ g_set_error_literal (error, G_IO_ERROR,
+ G_IO_ERROR_INVALID_FILENAME,
+ _("Invalid filename"));
+ else
+ {
+ char *display_name = g_filename_display_name (filename);
+ g_set_error (error, G_IO_ERROR,
+ g_io_error_from_errno (errsv),
+ _("Error opening file '%s': %s"),
+ display_name, g_strerror (errsv));
+ g_free (display_name);
+ }
+}
+
static GFileOutputStream *
output_stream_open (const char *filename,
gint open_flags,
fd = g_open (filename, open_flags, mode);
if (fd == -1)
{
- int errsv = errno;
-
- if (errsv == EINVAL)
- /* This must be an invalid filename, on e.g. FAT */
- g_set_error_literal (error, G_IO_ERROR,
- G_IO_ERROR_INVALID_FILENAME,
- _("Invalid filename"));
- else
- {
- char *display_name = g_filename_display_name (filename);
- g_set_error (error, G_IO_ERROR,
- g_io_error_from_errno (errsv),
- _("Error opening file '%s': %s"),
- display_name, g_strerror (errsv));
- g_free (display_name);
- }
+ set_error_from_open_errno (filename, error);
return NULL;
}
return output_stream_open (filename, open_flags, 0666, cancellable, error);
}
+static gint
+mode_from_flags_or_info (GFileCreateFlags flags,
+ GFileInfo *reference_info)
+{
+ if (flags & G_FILE_CREATE_PRIVATE)
+ return 0600;
+ else if (reference_info && g_file_info_has_attribute (reference_info, "unix::mode"))
+ return g_file_info_get_attribute_uint32 (reference_info, "unix::mode") & (~S_IFMT);
+ else
+ return 0666;
+}
+
GFileOutputStream *
_g_local_file_output_stream_create (const char *filename,
gboolean readable,
GFileCreateFlags flags,
+ GFileInfo *reference_info,
GCancellable *cancellable,
GError **error)
{
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
- if (flags & G_FILE_CREATE_PRIVATE)
- mode = 0600;
- else
- mode = 0666;
+ mode = mode_from_flags_or_info (flags, reference_info);
open_flags = O_CREAT | O_EXCL | O_BINARY;
if (readable)
gboolean create_backup,
char **temp_filename,
GFileCreateFlags flags,
+ GFileInfo *reference_info,
GCancellable *cancellable,
GError **error)
{
int res;
int mode;
- if (flags & G_FILE_CREATE_PRIVATE)
- mode = 0600;
- else
- mode = 0666;
+ mode = mode_from_flags_or_info (flags, reference_info);
/* We only need read access to the original file if we are creating a backup.
* We also add O_CREATE to avoid a race if the file was just removed */
const char *etag,
gboolean create_backup,
GFileCreateFlags flags,
+ GFileInfo *reference_info,
GCancellable *cancellable,
GError **error)
{
temp_file = NULL;
- if (flags & G_FILE_CREATE_PRIVATE)
- mode = 0600;
- else
- mode = 0666;
+ mode = mode_from_flags_or_info (flags, reference_info);
sync_on_close = FALSE;
/* If the file doesn't exist, create it */
{
/* The file already exists */
fd = handle_overwrite_open (filename, readable, etag,
- create_backup, &temp_file,
- flags, cancellable, error);
+ create_backup, &temp_file,
+ flags, reference_info,
+ cancellable, error);
if (fd == -1)
return NULL;
}
else if (fd == -1)
{
- int errsv = errno;
-
- if (errsv == EINVAL)
- /* This must be an invalid filename, on e.g. FAT */
- g_set_error_literal (error, G_IO_ERROR,
- G_IO_ERROR_INVALID_FILENAME,
- _("Invalid filename"));
- else
- {
- char *display_name = g_filename_display_name (filename);
- g_set_error (error, G_IO_ERROR,
- g_io_error_from_errno (errsv),
- _("Error opening file '%s': %s"),
- display_name, g_strerror (errsv));
- g_free (display_name);
- }
+ set_error_from_open_errno (filename, error);
return NULL;
}