summaryrefslogtreecommitdiff
path: root/gvfs/glocalfileoutputstream.c
blob: dd735da5fda0e481d55b0305690174c38289b7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#endif

#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include "gvfserror.h"
#include <glocalfileoutputstream.h>

G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);

/* Some of the file replacement code was based on the code from gedit,
 * relicenced to LGPL with permissions from the authors.
 */
  

#define BACKUP_EXTENSION "~"

struct _GLocalFileOutputStreamPrivate {
  char *filename;
  char *tmp_filename;
  GOutputStreamOpenMode open_mode;
  time_t original_mtime;
  gboolean create_backup;
  int fd;
};

static gssize     g_local_file_output_stream_write         (GOutputStream          *stream,
							    void                   *buffer,
							    gsize                   count,
							    GError                **error);
static gboolean   g_local_file_output_stream_close         (GOutputStream          *stream,
							    GError                **error);
static GFileInfo *g_local_file_output_stream_get_file_info (GFileOutputStream      *stream,
							    GFileInfoRequestFlags   requested,
							    char                   *attributes,
							    GError                **error);


static void
g_local_file_output_stream_finalize (GObject *object)
{
  GLocalFileOutputStream *file;
  
  file = G_LOCAL_FILE_OUTPUT_STREAM (object);
  
  g_free (file->priv->filename);
  g_free (file->priv->tmp_filename);
  
  if (G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize)
    (*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 = g_local_file_output_stream_write;
  stream_class->close = g_local_file_output_stream_close;
  file_stream_class->get_file_info = g_local_file_output_stream_get_file_info;
}

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);
}

GFileOutputStream *
g_local_file_output_stream_new (const char *filename,
				GOutputStreamOpenMode open_mode)
{
  GLocalFileOutputStream *stream;

  stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);

  stream->priv->filename = g_strdup (filename);
  stream->priv->open_mode = open_mode;
  stream->priv->fd = -1;
  
  return G_FILE_OUTPUT_STREAM (stream);
}

void
g_local_file_output_stream_set_original_mtime (GLocalFileOutputStream *stream,
					       time_t                  original_mtime)
{
  stream->priv->original_mtime = original_mtime;
}

void
g_local_file_output_stream_set_create_backup  (GLocalFileOutputStream *stream,
					       gboolean                create_backup)
{
  stream->priv->create_backup = create_backup;
}

static char *
create_backup_filename (const char *filename)
{
  return g_strconcat (filename, BACKUP_EXTENSION, NULL);
}

#define BUFSIZE	8192 /* size of normal write buffer */

static gboolean
copy_file_data (gint     sfd,
		gint     dfd,
		GError **error)
{
  gboolean ret = TRUE;
  gpointer buffer;
  const gchar *write_buffer;
  ssize_t bytes_read;
  ssize_t bytes_to_write;
  ssize_t bytes_written;
  
  buffer = g_malloc (BUFSIZE);
  
  do
    {
      bytes_read = read (sfd, buffer, BUFSIZE);
      if (bytes_read == -1)
	{
	  if (errno == EINTR)
	    continue;
	  
	  g_vfs_error_from_errno (error, errno);
	  ret = FALSE;
	  break;
	}
      
      bytes_to_write = bytes_read;
      write_buffer = buffer;
      
      do
	{
	  bytes_written = write (dfd, write_buffer, bytes_to_write);
	  if (bytes_written == -1)
	    {
	      if (errno == EINTR)
		continue;
	      
	      g_vfs_error_from_errno (error, errno);
	      ret = FALSE;
	      break;
	    }
	  
	  bytes_to_write -= bytes_written;
	  write_buffer += bytes_written;
	}
      while (bytes_to_write > 0);
      
    } while ((bytes_read != 0) && (ret == TRUE));

  g_free (buffer);
  
  return ret;
}

static void
handle_overwrite_open (GLocalFileOutputStream *file,
		       GError      **error)
{
  int fd = -1;
  struct stat original_stat;
  gboolean is_symlink;
  int open_flags;

  /* 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 */
  if (file->priv->create_backup)
    open_flags = O_RDWR | O_CREAT;
  else
    open_flags = O_WRONLY | O_CREAT;
  
  /* Some systems have O_NOFOLLOW, which lets us avoid some races
   * when finding out if the file we opened was a symlink */
#ifdef O_NOFOLLOW
  is_symlink = FALSE;
  fd = g_open (file->priv->filename, open_flags | O_NOFOLLOW, 0666);
  if (fd == -1 && errno == ELOOP)
    {
      /* Could be a symlink, or it could be a regular ELOOP error,
       * but then the next open will fail too. */
      is_symlink = TRUE;
      fd = g_open (file->priv->filename, open_flags, 0666);
    }
#else
  fd = g_open (file->priv->filename, open_flags, 0666);
  /* This is racy, but we do it as soon as possible to minimize the race */
  is_symlink = g_file_test (file->priv->filename, G_FILE_TEST_IS_SYMLINK)
#endif
    
  if (fd == -1)
    {
      g_vfs_error_from_errno (error, errno);
      goto err_out;
    }
  
  if (fstat (fd, &original_stat) != 0) 
    {
      g_vfs_error_from_errno (error, errno);
      goto err_out;
    }

  /* not a regular file */
  if (!S_ISREG (original_stat.st_mode))
    {
      if (S_ISDIR (original_stat.st_mode))
	g_set_error (error,
		     G_VFS_ERROR,
		     G_VFS_ERROR_IS_DIRECTORY,
		     _("Target file is a directory"));
      else
	g_set_error (error,
		     G_VFS_ERROR,
		     G_VFS_ERROR_NOT_REGULAR_FILE,
		     _("Target file is not a regular file"));
      goto err_out;
    }
  
  if (file->priv->original_mtime != 0 &&
      original_stat.st_mtime != file->priv->original_mtime)
    {
      g_set_error (error,
		   G_VFS_ERROR,
		   G_VFS_ERROR_WRONG_MTIME,
		   _("The file was externally modified"));
      goto err_out;
    }

  /* We use two backup strategies.
   * The first one (which is faster) consist in saving to a
   * tmp file then rename the original file to the backup and the
   * tmp file to the original name. This is fast but doesn't work
   * when the file is a link (hard or symbolic) or when we can't
   * write to the current dir or can't set the permissions on the
   * new file. 
   * The second strategy consist simply in copying the old file
   * to a backup file and rewrite the contents of the file.
   */
  
  if (!(original_stat.st_nlink > 1) && !is_symlink)
    {
      char *dirname, *tmp_filename;
      int tmpfd;
      
      dirname = g_path_get_dirname (file->priv->filename);
      tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
      g_free (dirname);

      tmpfd = g_mkstemp (tmp_filename);
      if (tmpfd == -1)
	{
	  g_free (tmp_filename);
	  goto fallback_strategy;
	}
      
      /* try to keep permissions */
      if (fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
	  fchmod (tmpfd, original_stat.st_mode) == -1)
	{
	  close (tmpfd);
	  unlink (tmp_filename);
	  g_free (tmp_filename);
	  goto fallback_strategy;
	}

      close (fd);
      file->priv->fd = tmpfd;
      file->priv->tmp_filename = tmp_filename;
      return;
   
    }

 fallback_strategy:

  if (file->priv->create_backup)
    {
      char *backup_filename;
      int bfd;
      
      backup_filename = create_backup_filename (file->priv->filename);

      if (unlink (backup_filename) == -1 && errno != ENOENT)
	{
	  g_set_error (error,
		       G_VFS_ERROR,
		       G_VFS_ERROR_CANT_CREATE_BACKUP,
		       _("Backup file creation failed"));
	  g_free (backup_filename);
	  goto err_out;
	}

      bfd = open (backup_filename,
		  O_WRONLY | O_CREAT | O_EXCL,
		  original_stat.st_mode & 0777);

      if (bfd == -1)
	{
	  g_set_error (error,
		       G_VFS_ERROR,
		       G_VFS_ERROR_CANT_CREATE_BACKUP,
		       _("Backup file creation failed"));
	  g_free (backup_filename);
	  goto err_out;
	}

      /* Try to set the group of the backup same as the
       * original file. If this fails, set the protection
       * bits for the group same as the protection bits for
       * others. */
      if (fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
	{
	  if (fchmod (bfd,
		      (original_stat.st_mode & 0707) |
		      ((original_stat.st_mode & 07) << 3)) != 0)
	    {
	      g_set_error (error,
			   G_VFS_ERROR,
			   G_VFS_ERROR_CANT_CREATE_BACKUP,
			   _("Backup file creation failed"));
	      unlink (backup_filename);
	      close (bfd);
	      g_free (backup_filename);
	      goto err_out;
	    }
	}

      if (!copy_file_data (fd, bfd, NULL))
	{
	  g_set_error (error,
		       G_VFS_ERROR,
		       G_VFS_ERROR_CANT_CREATE_BACKUP,
		       _("Backup file creation failed"));
	  unlink (backup_filename);
	  close (bfd);
	  g_free (backup_filename);
	  
	  goto err_out;
	}
      
      close (bfd);
      g_free (backup_filename);

      /* Seek back to the start of the file after the backup copy */
      if (lseek (fd, 0, SEEK_SET) == -1)
	{
	  g_vfs_error_from_errno (error, errno);
	  goto err_out;
	}
    }

  /* Truncate the file at the start */
  if (ftruncate (fd, 0) == -1)
    {
      g_vfs_error_from_errno (error, errno);
      goto err_out;
    }
    
  file->priv->fd = fd;
  
  return;

 err_out:
  if (fd != -1)
    close (fd);
  return;
}

static gboolean
g_local_file_output_stream_open (GLocalFileOutputStream *file,
				 GError      **error)
{
  if (file->priv->fd != -1)
    return TRUE;

  switch (file->priv->open_mode)
    {
    case G_OUTPUT_STREAM_OPEN_MODE_CREATE:
      file->priv->fd = g_open (file->priv->filename,
			       O_CREAT | O_EXCL | O_WRONLY,
			       0666);
      if (file->priv->fd == -1)
	g_vfs_error_from_errno (error, errno);
      break;
    case G_OUTPUT_STREAM_OPEN_MODE_APPEND:
      file->priv->fd = g_open (file->priv->filename,
			       O_CREAT | O_APPEND | O_WRONLY,
			       0666);
      if (file->priv->fd == -1)
	g_vfs_error_from_errno (error, errno);
      break;
    case G_OUTPUT_STREAM_OPEN_MODE_REPLACE:
      /* If the file doesn't exist, create it */
      file->priv->fd = g_open (file->priv->filename,
			       O_CREAT | O_EXCL | O_WRONLY,
			       0666);

      if (file->priv->fd == -1 && errno == EEXIST)
	{
	  /* The file already exists */
	  handle_overwrite_open (file, error);
	}
      else if (file->priv->fd == -1)
	g_vfs_error_from_errno (error, errno);
      break;
    default:
      g_set_error (error,
		   G_VFS_ERROR,
		   G_VFS_ERROR_INVALID_ARGUMENT,
		   _("Invalid open mode"));
    }

  return file->priv->fd != -1;
}
			  

static gssize
g_local_file_output_stream_write (GOutputStream *stream,
				  void         *buffer,
				  gsize         count,
				  GError      **error)
{
  GLocalFileOutputStream *file;
  gssize res;

  file = G_LOCAL_FILE_OUTPUT_STREAM (stream);

  if (!g_local_file_output_stream_open (file, error))
    return -1;
  
  while (1)
    {
      res = write (file->priv->fd, buffer, count);
      if (res == -1)
	{
	  if (g_output_stream_is_cancelled (stream))
	    {
	      g_set_error (error,
			   G_VFS_ERROR,
			   G_VFS_ERROR_CANCELLED,
			   _("Operation was cancelled"));
	      break;
	    }
	  
	  if (errno == EINTR)
	    continue;
	  
	  g_vfs_error_from_errno (error, errno);
	}
      
      break;
    }
  
  return res;
}

static gboolean
g_local_file_output_stream_close (GOutputStream *stream,
				  GError      **error)
{
  GLocalFileOutputStream *file;
  struct stat final_stat;
  int res;

  file = G_LOCAL_FILE_OUTPUT_STREAM (stream);

  if (file->priv->fd == -1)
    return TRUE;

  if (file->priv->tmp_filename)
    {
      /* We need to move the temp file to its final place,
       * and possibly create the backup file
       */

      if (file->priv->create_backup)
	{
	  char *backup_filename;
      
	  backup_filename = create_backup_filename (file->priv->filename);
	  
	  /* create original -> backup link, the original is then renamed over */
	  if (link (file->priv->filename, backup_filename) != 0)
	    {
	      g_vfs_error_from_errno (error, errno);
	      g_free (backup_filename);
	      goto err_out;
	    }
	}
      
      /* tmp -> original */
      if (rename (file->priv->tmp_filename, file->priv->filename) != 0)
	{
	  g_vfs_error_from_errno (error, errno);
	  goto err_out;
	}
    }
  
  if (g_file_output_stream_get_should_get_final_mtime (G_FILE_OUTPUT_STREAM (stream)) &&
      fstat (file->priv->fd, &final_stat) == 0)
    {
      g_file_output_stream_set_final_mtime (G_FILE_OUTPUT_STREAM (stream),
					    final_stat.st_mtime);
    }

  while (1)
    {
      res = close (file->priv->fd);
      if (res == -1)
	{
	  if (g_output_stream_is_cancelled (stream))
	    {
	      g_set_error (error,
			   G_VFS_ERROR,
			   G_VFS_ERROR_CANCELLED,
			   _("Operation was cancelled"));
	      break;
	    }
	  
	  if (errno == EINTR)
	    continue;
	  
	  g_vfs_error_from_errno (error, errno);
	}
      break;
    }

  return res != -1;

 err_out:
  /* A simple try to close the fd in case we fail before the actual close */
  close (file->priv->fd);
  return FALSE;
}

static GFileInfo *
g_local_file_output_stream_get_file_info (GFileOutputStream     *stream,
					  GFileInfoRequestFlags requested,
					  char                 *attributes,
					  GError              **error)
{
  GLocalFileOutputStream *file;
  GFileInfo *info;
  struct stat stat_buf;
  GFileAttributeMatcher *matcher;

  file = G_LOCAL_FILE_OUTPUT_STREAM (stream);

  if (!g_local_file_output_stream_open (file, error))
    return NULL;

  if (fstat (file->priv->fd, &stat_buf) == -1)
    {
      g_vfs_error_from_errno (error, errno);
      return NULL;
    }

  info = g_file_info_new ();

  g_file_info_set_from_stat (info, requested, &stat_buf);

  matcher = g_file_attribute_matcher_new (attributes);
  
#ifdef HAVE_SELINUX
  if (g_file_attribute_matcher_matches (matcher, "selinux", "selinux:context") &&
      is_selinux_enabled ()) {
    char *context;
    if (fgetfilecon_raw (file->priv->fd, &context) >= 0)
      {
	g_file_info_set_attribute (info, "selinux:context", context);
	freecon(context);
      }
  }
#endif
  
  g_file_attribute_matcher_free (matcher);
  
  return info;
}