~ubuntu-branches/debian/squeeze/glib2.0/squeeze

« back to all changes in this revision

Viewing changes to gio/glocalfileoutputstream.c

  • Committer: Bazaar Package Importer
  • Author(s): Gustavo Noronha Silva
  • Date: 2009-02-15 13:00:43 UTC
  • mfrom: (1.3.1 upstream) (69.1.10 intrepid)
  • Revision ID: james.westby@ubuntu.com-20090215130043-q47fbt3owmt42m2f
Tags: 2.18.4-2
* Release to unstable
* debian/rules:
- bump SHVER, since we are already forcing a 2.18.0 dependecy on the
  symbols introduced in the development versions
* debian/control.in:
- added Homepage and Vcs-* control fields

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Author: Alexander Larsson <alexl@redhat.com>
21
21
 */
22
22
 
23
 
#include <config.h>
 
23
#include "config.h"
24
24
 
25
25
#include <sys/types.h>
26
26
#include <sys/stat.h>
35
35
#include <glib/gstdio.h>
36
36
#include "glibintl.h"
37
37
#include "gioerror.h"
 
38
#include "gcancellable.h"
38
39
#include "glocalfileoutputstream.h"
39
40
#include "glocalfileinfo.h"
40
41
 
108
109
  g_free (file->priv->original_filename);
109
110
  g_free (file->priv->backup_filename);
110
111
  g_free (file->priv->etag);
111
 
  
112
 
  if (G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize)
113
 
    (*G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize) (object);
 
112
 
 
113
  G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
114
114
}
115
115
 
116
116
static void
185
185
                                  GError        **error)
186
186
{
187
187
  GLocalFileOutputStream *file;
188
 
  struct stat final_stat;
 
188
  GLocalFileStat final_stat;
189
189
  int res;
190
190
 
191
191
  file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
192
192
 
 
193
#ifdef G_OS_WIN32
 
194
 
 
195
  /* Must close before renaming on Windows, so just do the close first
 
196
   * in all cases for now.
 
197
   */
 
198
  if (_fstati64 (file->priv->fd, &final_stat) == 0)
 
199
    file->priv->etag = _g_local_file_info_create_etag (&final_stat);
 
200
 
 
201
  res = close (file->priv->fd);
 
202
  if (res == -1)
 
203
    {
 
204
      int errsv = errno;
 
205
      
 
206
      g_set_error (error, G_IO_ERROR,
 
207
                   g_io_error_from_errno (errsv),
 
208
                   _("Error closing file: %s"),
 
209
                   g_strerror (errsv));
 
210
      return FALSE;
 
211
    }
 
212
 
 
213
#endif
 
214
 
193
215
  if (file->priv->tmp_filename)
194
216
    {
195
217
      /* We need to move the temp file to its final place,
264
286
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
265
287
    goto err_out;
266
288
      
 
289
#ifndef G_OS_WIN32              /* Already did the fstat() and close() above on Win32 */
 
290
 
267
291
  if (fstat (file->priv->fd, &final_stat) == 0)
268
292
    file->priv->etag = _g_local_file_info_create_etag (&final_stat);
269
293
 
284
308
  
285
309
  return res != -1;
286
310
 
 
311
#else
 
312
 
 
313
  return TRUE;
 
314
 
 
315
#endif
 
316
 
287
317
 err_out:
 
318
 
 
319
#ifndef G_OS_WIN32
288
320
  /* A simple try to close the fd in case we fail before the actual close */
289
321
  close (file->priv->fd);
 
322
#endif
290
323
  return FALSE;
291
324
}
292
325
 
467
500
 
468
501
      if (errsv == EINVAL)
469
502
        /* This must be an invalid filename, on e.g. FAT */
470
 
        g_set_error (error, G_IO_ERROR,
471
 
                     G_IO_ERROR_INVALID_FILENAME,
472
 
                     _("Invalid filename"));
 
503
        g_set_error_literal (error, G_IO_ERROR,
 
504
                             G_IO_ERROR_INVALID_FILENAME,
 
505
                             _("Invalid filename"));
473
506
      else
474
507
        {
475
508
          char *display_name = g_filename_display_name (filename);
512
545
 
513
546
      if (errsv == EINVAL)
514
547
        /* This must be an invalid filename, on e.g. FAT */
515
 
        g_set_error (error, G_IO_ERROR,
516
 
                     G_IO_ERROR_INVALID_FILENAME,
517
 
                     _("Invalid filename"));
 
548
        g_set_error_literal (error, G_IO_ERROR,
 
549
                             G_IO_ERROR_INVALID_FILENAME,
 
550
                             _("Invalid filename"));
518
551
      else
519
552
        {
520
553
          char *display_name = g_filename_display_name (filename);
615
648
                       GError       **error)
616
649
{
617
650
  int fd = -1;
618
 
  struct stat original_stat;
 
651
  GLocalFileStat original_stat;
619
652
  char *current_etag;
620
653
  gboolean is_symlink;
621
654
  int open_flags;
 
655
  int res;
622
656
 
623
657
  /* We only need read access to the original file if we are creating a backup.
624
658
   * We also add O_CREATE to avoid a race if the file was just removed */
657
691
      return -1;
658
692
    }
659
693
  
660
 
  if (fstat (fd, &original_stat) != 0) 
 
694
#ifdef G_OS_WIN32
 
695
  res = _fstati64 (fd, &original_stat);
 
696
#else
 
697
  res = fstat (fd, &original_stat);
 
698
#endif
 
699
 
 
700
  if (res != 0) 
661
701
    {
662
702
      int errsv = errno;
663
703
      char *display_name = g_filename_display_name (filename);
673
713
  if (!S_ISREG (original_stat.st_mode))
674
714
    {
675
715
      if (S_ISDIR (original_stat.st_mode))
676
 
        g_set_error (error,
677
 
                     G_IO_ERROR,
678
 
                     G_IO_ERROR_IS_DIRECTORY,
679
 
                     _("Target file is a directory"));
 
716
        g_set_error_literal (error,
 
717
                             G_IO_ERROR,
 
718
                             G_IO_ERROR_IS_DIRECTORY,
 
719
                             _("Target file is a directory"));
680
720
      else
681
 
        g_set_error (error,
682
 
                     G_IO_ERROR,
683
 
                     G_IO_ERROR_NOT_REGULAR_FILE,
684
 
                     _("Target file is not a regular file"));
 
721
        g_set_error_literal (error,
 
722
                             G_IO_ERROR,
 
723
                             G_IO_ERROR_NOT_REGULAR_FILE,
 
724
                             _("Target file is not a regular file"));
685
725
      goto err_out;
686
726
    }
687
727
  
690
730
      current_etag = _g_local_file_info_create_etag (&original_stat);
691
731
      if (strcmp (etag, current_etag) != 0)
692
732
        {
693
 
          g_set_error (error,
694
 
                       G_IO_ERROR,
695
 
                       G_IO_ERROR_WRONG_ETAG,
696
 
                       _("The file was externally modified"));
 
733
          g_set_error_literal (error,
 
734
                               G_IO_ERROR,
 
735
                               G_IO_ERROR_WRONG_ETAG,
 
736
                               _("The file was externally modified"));
697
737
          g_free (current_etag);
698
738
          goto err_out;
699
739
        }
763
803
 
764
804
  if (create_backup)
765
805
    {
 
806
#if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
766
807
      struct stat tmp_statbuf;      
 
808
#endif
767
809
      char *backup_filename;
768
810
      int bfd;
769
811
      
771
813
 
772
814
      if (g_unlink (backup_filename) == -1 && errno != ENOENT)
773
815
        {
774
 
          g_set_error (error,
775
 
                       G_IO_ERROR,
776
 
                       G_IO_ERROR_CANT_CREATE_BACKUP,
777
 
                       _("Backup file creation failed"));
 
816
          g_set_error_literal (error,
 
817
                               G_IO_ERROR,
 
818
                               G_IO_ERROR_CANT_CREATE_BACKUP,
 
819
                               _("Backup file creation failed"));
778
820
          g_free (backup_filename);
779
821
          goto err_out;
780
822
        }
785
827
 
786
828
      if (bfd == -1)
787
829
        {
788
 
          g_set_error (error,
789
 
                       G_IO_ERROR,
790
 
                       G_IO_ERROR_CANT_CREATE_BACKUP,
791
 
                       _("Backup file creation failed"));
 
830
          g_set_error_literal (error,
 
831
                               G_IO_ERROR,
 
832
                               G_IO_ERROR_CANT_CREATE_BACKUP,
 
833
                               _("Backup file creation failed"));
792
834
          g_free (backup_filename);
793
835
          goto err_out;
794
836
        }
800
842
#if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
801
843
      if (fstat (bfd, &tmp_statbuf) != 0)
802
844
        {
803
 
          g_set_error (error,
804
 
                       G_IO_ERROR,
805
 
                       G_IO_ERROR_CANT_CREATE_BACKUP,
806
 
                       _("Backup file creation failed"));
 
845
          g_set_error_literal (error,
 
846
                               G_IO_ERROR,
 
847
                               G_IO_ERROR_CANT_CREATE_BACKUP,
 
848
                               _("Backup file creation failed"));
807
849
          g_unlink (backup_filename);
808
850
          g_free (backup_filename);
809
851
          goto err_out;
816
858
                      (original_stat.st_mode & 0707) |
817
859
                      ((original_stat.st_mode & 07) << 3)) != 0)
818
860
            {
819
 
              g_set_error (error,
820
 
                           G_IO_ERROR,
821
 
                           G_IO_ERROR_CANT_CREATE_BACKUP,
822
 
                           _("Backup file creation failed"));
 
861
              g_set_error_literal (error,
 
862
                                   G_IO_ERROR,
 
863
                                   G_IO_ERROR_CANT_CREATE_BACKUP,
 
864
                                   _("Backup file creation failed"));
823
865
              g_unlink (backup_filename);
824
866
              close (bfd);
825
867
              g_free (backup_filename);
830
872
 
831
873
      if (!copy_file_data (fd, bfd, NULL))
832
874
        {
833
 
          g_set_error (error,
834
 
                       G_IO_ERROR,
835
 
                       G_IO_ERROR_CANT_CREATE_BACKUP,
836
 
                       _("Backup file creation failed"));
 
875
          g_set_error_literal (error,
 
876
                               G_IO_ERROR,
 
877
                               G_IO_ERROR_CANT_CREATE_BACKUP,
 
878
                               _("Backup file creation failed"));
837
879
          g_unlink (backup_filename);
838
880
          close (bfd);
839
881
          g_free (backup_filename);
920
962
 
921
963
      if (errsv == EINVAL)
922
964
        /* This must be an invalid filename, on e.g. FAT */
923
 
        g_set_error (error, G_IO_ERROR,
924
 
                     G_IO_ERROR_INVALID_FILENAME,
925
 
                     _("Invalid filename"));
 
965
        g_set_error_literal (error, G_IO_ERROR,
 
966
                             G_IO_ERROR_INVALID_FILENAME,
 
967
                             _("Invalid filename"));
926
968
      else
927
969
        {
928
970
          char *display_name = g_filename_display_name (filename);