This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
/
gfileutils.c
1921 lines (1612 loc) · 45.1 KB
/
gfileutils.c
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* gfileutils.c - File utility functions
*
* Copyright 2000 Red Hat, Inc.
*
* GLib is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* GLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with GLib; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "glib.h"
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#ifdef G_OS_WIN32
#include <windows.h>
#include <io.h>
#endif /* G_OS_WIN32 */
#ifndef S_ISLNK
#define S_ISLNK(x) 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#include "gstdio.h"
#include "glibintl.h"
#include "galias.h"
static gint create_temp_file (gchar *tmpl,
int permissions);
/**
* g_mkdir_with_parents:
* @pathname: a pathname in the GLib file name encoding
* @mode: permissions to use for newly created directories
*
* Create a directory if it doesn't already exist. Create intermediate
* parent directories as needed, too.
*
* Returns: 0 if the directory already exists, or was successfully
* created. Returns -1 if an error occurred, with errno set.
*
* Since: 2.8
*/
int
g_mkdir_with_parents (const gchar *pathname,
int mode)
{
gchar *fn, *p;
if (pathname == NULL || *pathname == '\0')
{
errno = EINVAL;
return -1;
}
fn = g_strdup (pathname);
if (g_path_is_absolute (fn))
p = (gchar *) g_path_skip_root (fn);
else
p = fn;
do
{
while (*p && !G_IS_DIR_SEPARATOR (*p))
p++;
if (!*p)
p = NULL;
else
*p = '\0';
if (!g_file_test (fn, G_FILE_TEST_EXISTS))
{
if (g_mkdir (fn, mode) == -1)
{
int errno_save = errno;
g_free (fn);
errno = errno_save;
return -1;
}
}
else if (!g_file_test (fn, G_FILE_TEST_IS_DIR))
{
g_free (fn);
errno = ENOTDIR;
return -1;
}
if (p)
{
*p++ = G_DIR_SEPARATOR;
while (*p && G_IS_DIR_SEPARATOR (*p))
p++;
}
}
while (p);
g_free (fn);
return 0;
}
/**
* g_file_test:
* @filename: a filename to test in the GLib file name encoding
* @test: bitfield of #GFileTest flags
*
* Returns %TRUE if any of the tests in the bitfield @test are
* %TRUE. For example, <literal>(G_FILE_TEST_EXISTS |
* G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists;
* the check whether it's a directory doesn't matter since the existence
* test is %TRUE. With the current set of available tests, there's no point
* passing in more than one test at a time.
*
* Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
* so for a symbolic link to a regular file g_file_test() will return
* %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
*
* Note, that for a dangling symbolic link g_file_test() will return
* %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
*
* You should never use g_file_test() to test whether it is safe
* to perform an operation, because there is always the possibility
* of the condition changing before you actually perform the operation.
* For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
* to know whether it is safe to write to a file without being
* tricked into writing into a different location. It doesn't work!
* |[
* /* DON'T DO THIS */
* if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK))
* {
* fd = g_open (filename, O_WRONLY);
* /* write to fd */
* }
* ]|
*
* Another thing to note is that %G_FILE_TEST_EXISTS and
* %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
* system call. This usually doesn't matter, but if your program
* is setuid or setgid it means that these tests will give you
* the answer for the real user ID and group ID, rather than the
* effective user ID and group ID.
*
* On Windows, there are no symlinks, so testing for
* %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
* %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
* its name indicates that it is executable, checking for well-known
* extensions and those listed in the %PATHEXT environment variable.
*
* Return value: whether a test was %TRUE
**/
gboolean
g_file_test (const gchar *filename,
GFileTest test)
{
#ifdef G_OS_WIN32
/* stuff missing in std vc6 api */
# ifndef INVALID_FILE_ATTRIBUTES
# define INVALID_FILE_ATTRIBUTES -1
# endif
# ifndef FILE_ATTRIBUTE_DEVICE
# define FILE_ATTRIBUTE_DEVICE 64
# endif
int attributes;
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
if (wfilename == NULL)
return FALSE;
attributes = GetFileAttributesW (wfilename);
g_free (wfilename);
if (attributes == INVALID_FILE_ATTRIBUTES)
return FALSE;
if (test & G_FILE_TEST_EXISTS)
return TRUE;
if (test & G_FILE_TEST_IS_REGULAR)
return (attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0;
if (test & G_FILE_TEST_IS_DIR)
return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (test & G_FILE_TEST_IS_EXECUTABLE)
{
const gchar *lastdot = strrchr (filename, '.');
const gchar *pathext = NULL, *p;
int extlen;
if (lastdot == NULL)
return FALSE;
if (_stricmp (lastdot, ".exe") == 0 ||
_stricmp (lastdot, ".cmd") == 0 ||
_stricmp (lastdot, ".bat") == 0 ||
_stricmp (lastdot, ".com") == 0)
return TRUE;
/* Check if it is one of the types listed in %PATHEXT% */
pathext = g_getenv ("PATHEXT");
if (pathext == NULL)
return FALSE;
pathext = g_utf8_casefold (pathext, -1);
lastdot = g_utf8_casefold (lastdot, -1);
extlen = strlen (lastdot);
p = pathext;
while (TRUE)
{
const gchar *q = strchr (p, ';');
if (q == NULL)
q = p + strlen (p);
if (extlen == q - p &&
memcmp (lastdot, p, extlen) == 0)
{
g_free ((gchar *) pathext);
g_free ((gchar *) lastdot);
return TRUE;
}
if (*q)
p = q + 1;
else
break;
}
g_free ((gchar *) pathext);
g_free ((gchar *) lastdot);
return FALSE;
}
return FALSE;
#else
if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
return TRUE;
if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
{
if (getuid () != 0)
return TRUE;
/* For root, on some POSIX systems, access (filename, X_OK)
* will succeed even if no executable bits are set on the
* file. We fall through to a stat test to avoid that.
*/
}
else
test &= ~G_FILE_TEST_IS_EXECUTABLE;
if (test & G_FILE_TEST_IS_SYMLINK)
{
struct stat s;
if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
return TRUE;
}
if (test & (G_FILE_TEST_IS_REGULAR |
G_FILE_TEST_IS_DIR |
G_FILE_TEST_IS_EXECUTABLE))
{
struct stat s;
if (stat (filename, &s) == 0)
{
if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
return TRUE;
if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
return TRUE;
/* The extra test for root when access (file, X_OK) succeeds.
*/
if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
((s.st_mode & S_IXOTH) ||
(s.st_mode & S_IXUSR) ||
(s.st_mode & S_IXGRP)))
return TRUE;
}
}
return FALSE;
#endif
}
GQuark
g_file_error_quark (void)
{
return g_quark_from_static_string ("g-file-error-quark");
}
/**
* g_file_error_from_errno:
* @err_no: an "errno" value
*
* Gets a #GFileError constant based on the passed-in @errno.
* For example, if you pass in %EEXIST this function returns
* #G_FILE_ERROR_EXIST. Unlike @errno values, you can portably
* assume that all #GFileError values will exist.
*
* Normally a #GFileError value goes into a #GError returned
* from a function that manipulates files. So you would use
* g_file_error_from_errno() when constructing a #GError.
*
* Return value: #GFileError corresponding to the given @errno
**/
GFileError
g_file_error_from_errno (gint err_no)
{
switch (err_no)
{
#ifdef EEXIST
case EEXIST:
return G_FILE_ERROR_EXIST;
break;
#endif
#ifdef EISDIR
case EISDIR:
return G_FILE_ERROR_ISDIR;
break;
#endif
#ifdef EACCES
case EACCES:
return G_FILE_ERROR_ACCES;
break;
#endif
#ifdef ENAMETOOLONG
case ENAMETOOLONG:
return G_FILE_ERROR_NAMETOOLONG;
break;
#endif
#ifdef ENOENT
case ENOENT:
return G_FILE_ERROR_NOENT;
break;
#endif
#ifdef ENOTDIR
case ENOTDIR:
return G_FILE_ERROR_NOTDIR;
break;
#endif
#ifdef ENXIO
case ENXIO:
return G_FILE_ERROR_NXIO;
break;
#endif
#ifdef ENODEV
case ENODEV:
return G_FILE_ERROR_NODEV;
break;
#endif
#ifdef EROFS
case EROFS:
return G_FILE_ERROR_ROFS;
break;
#endif
#ifdef ETXTBSY
case ETXTBSY:
return G_FILE_ERROR_TXTBSY;
break;
#endif
#ifdef EFAULT
case EFAULT:
return G_FILE_ERROR_FAULT;
break;
#endif
#ifdef ELOOP
case ELOOP:
return G_FILE_ERROR_LOOP;
break;
#endif
#ifdef ENOSPC
case ENOSPC:
return G_FILE_ERROR_NOSPC;
break;
#endif
#ifdef ENOMEM
case ENOMEM:
return G_FILE_ERROR_NOMEM;
break;
#endif
#ifdef EMFILE
case EMFILE:
return G_FILE_ERROR_MFILE;
break;
#endif
#ifdef ENFILE
case ENFILE:
return G_FILE_ERROR_NFILE;
break;
#endif
#ifdef EBADF
case EBADF:
return G_FILE_ERROR_BADF;
break;
#endif
#ifdef EINVAL
case EINVAL:
return G_FILE_ERROR_INVAL;
break;
#endif
#ifdef EPIPE
case EPIPE:
return G_FILE_ERROR_PIPE;
break;
#endif
#ifdef EAGAIN
case EAGAIN:
return G_FILE_ERROR_AGAIN;
break;
#endif
#ifdef EINTR
case EINTR:
return G_FILE_ERROR_INTR;
break;
#endif
#ifdef EIO
case EIO:
return G_FILE_ERROR_IO;
break;
#endif
#ifdef EPERM
case EPERM:
return G_FILE_ERROR_PERM;
break;
#endif
#ifdef ENOSYS
case ENOSYS:
return G_FILE_ERROR_NOSYS;
break;
#endif
default:
return G_FILE_ERROR_FAILED;
break;
}
}
static gboolean
get_contents_stdio (const gchar *display_filename,
FILE *f,
gchar **contents,
gsize *length,
GError **error)
{
gchar buf[4096];
gsize bytes;
gchar *str = NULL;
gsize total_bytes = 0;
gsize total_allocated = 0;
gchar *tmp;
g_assert (f != NULL);
while (!feof (f))
{
gint save_errno;
bytes = fread (buf, 1, sizeof (buf), f);
save_errno = errno;
while ((total_bytes + bytes + 1) > total_allocated)
{
if (str)
total_allocated *= 2;
else
total_allocated = MIN (bytes + 1, sizeof (buf));
tmp = g_try_realloc (str, total_allocated);
if (tmp == NULL)
{
g_set_error (error,
G_FILE_ERROR,
G_FILE_ERROR_NOMEM,
_("Could not allocate %lu bytes to read file \"%s\""),
(gulong) total_allocated,
display_filename);
goto error;
}
str = tmp;
}
if (ferror (f))
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Error reading file '%s': %s"),
display_filename,
g_strerror (save_errno));
goto error;
}
memcpy (str + total_bytes, buf, bytes);
if (total_bytes + bytes < total_bytes)
{
g_set_error (error,
G_FILE_ERROR,
G_FILE_ERROR_FAILED,
_("File \"%s\" is too large"),
display_filename);
goto error;
}
total_bytes += bytes;
}
fclose (f);
if (total_allocated == 0)
{
str = g_new (gchar, 1);
total_bytes = 0;
}
str[total_bytes] = '\0';
if (length)
*length = total_bytes;
*contents = str;
return TRUE;
error:
g_free (str);
fclose (f);
return FALSE;
}
#ifndef G_OS_WIN32
static gboolean
get_contents_regfile (const gchar *display_filename,
struct stat *stat_buf,
gint fd,
gchar **contents,
gsize *length,
GError **error)
{
gchar *buf;
gsize bytes_read;
gsize size;
gsize alloc_size;
size = stat_buf->st_size;
alloc_size = size + 1;
buf = g_try_malloc (alloc_size);
if (buf == NULL)
{
g_set_error (error,
G_FILE_ERROR,
G_FILE_ERROR_NOMEM,
_("Could not allocate %lu bytes to read file \"%s\""),
(gulong) alloc_size,
display_filename);
goto error;
}
bytes_read = 0;
while (bytes_read < size)
{
gssize rc;
rc = read (fd, buf + bytes_read, size - bytes_read);
if (rc < 0)
{
if (errno != EINTR)
{
int save_errno = errno;
g_free (buf);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to read from file '%s': %s"),
display_filename,
g_strerror (save_errno));
goto error;
}
}
else if (rc == 0)
break;
else
bytes_read += rc;
}
buf[bytes_read] = '\0';
if (length)
*length = bytes_read;
*contents = buf;
close (fd);
return TRUE;
error:
close (fd);
return FALSE;
}
static gboolean
get_contents_posix (const gchar *filename,
gchar **contents,
gsize *length,
GError **error)
{
struct stat stat_buf;
gint fd;
gchar *display_filename = g_filename_display_name (filename);
/* O_BINARY useful on Cygwin */
fd = open (filename, O_RDONLY|O_BINARY);
if (fd < 0)
{
int save_errno = errno;
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open file '%s': %s"),
display_filename,
g_strerror (save_errno));
g_free (display_filename);
return FALSE;
}
/* I don't think this will ever fail, aside from ENOMEM, but. */
if (fstat (fd, &stat_buf) < 0)
{
int save_errno = errno;
close (fd);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to get attributes of file '%s': fstat() failed: %s"),
display_filename,
g_strerror (save_errno));
g_free (display_filename);
return FALSE;
}
if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
{
gboolean retval = get_contents_regfile (display_filename,
&stat_buf,
fd,
contents,
length,
error);
g_free (display_filename);
return retval;
}
else
{
FILE *f;
gboolean retval;
f = fdopen (fd, "r");
if (f == NULL)
{
int save_errno = errno;
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open file '%s': fdopen() failed: %s"),
display_filename,
g_strerror (save_errno));
g_free (display_filename);
return FALSE;
}
retval = get_contents_stdio (display_filename, f, contents, length, error);
g_free (display_filename);
return retval;
}
}
#else /* G_OS_WIN32 */
static gboolean
get_contents_win32 (const gchar *filename,
gchar **contents,
gsize *length,
GError **error)
{
FILE *f;
gboolean retval;
gchar *display_filename = g_filename_display_name (filename);
int save_errno;
f = g_fopen (filename, "rb");
save_errno = errno;
if (f == NULL)
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open file '%s': %s"),
display_filename,
g_strerror (save_errno));
g_free (display_filename);
return FALSE;
}
retval = get_contents_stdio (display_filename, f, contents, length, error);
g_free (display_filename);
return retval;
}
#endif
/**
* g_file_get_contents:
* @filename: name of a file to read contents from, in the GLib file name encoding
* @contents: location to store an allocated string
* @length: location to store length in bytes of the contents, or %NULL
* @error: return location for a #GError, or %NULL
*
* Reads an entire file into allocated memory, with good error
* checking.
*
* If the call was successful, it returns %TRUE and sets @contents to the file
* contents and @length to the length of the file contents in bytes. The string
* stored in @contents will be nul-terminated, so for text files you can pass
* %NULL for the @length argument. If the call was not successful, it returns
* %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible error
* codes are those in the #GFileError enumeration. In the error case,
* @contents is set to %NULL and @length is set to zero.
*
* Return value: %TRUE on success, %FALSE if an error occurred
**/
gboolean
g_file_get_contents (const gchar *filename,
gchar **contents,
gsize *length,
GError **error)
{
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (contents != NULL, FALSE);
*contents = NULL;
if (length)
*length = 0;
#ifdef G_OS_WIN32
return get_contents_win32 (filename, contents, length, error);
#else
return get_contents_posix (filename, contents, length, error);
#endif
}
static gboolean
rename_file (const char *old_name,
const char *new_name,
GError **err)
{
errno = 0;
if (g_rename (old_name, new_name) == -1)
{
int save_errno = errno;
gchar *display_old_name = g_filename_display_name (old_name);
gchar *display_new_name = g_filename_display_name (new_name);
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to rename file '%s' to '%s': g_rename() failed: %s"),
display_old_name,
display_new_name,
g_strerror (save_errno));
g_free (display_old_name);
g_free (display_new_name);
return FALSE;
}
return TRUE;
}
static gchar *
write_to_temp_file (const gchar *contents,
gssize length,
const gchar *template,
GError **err)
{
gchar *tmp_name;
gchar *display_name;
gchar *retval;
FILE *file;
gint fd;
int save_errno;
retval = NULL;
tmp_name = g_strdup_printf ("%s.XXXXXX", template);
errno = 0;
fd = create_temp_file (tmp_name, 0666);
save_errno = errno;
display_name = g_filename_display_name (tmp_name);
if (fd == -1)
{
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to create file '%s': %s"),
display_name, g_strerror (save_errno));
goto out;
}
errno = 0;
file = fdopen (fd, "wb");
if (!file)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open file '%s' for writing: fdopen() failed: %s"),
display_name,
g_strerror (save_errno));
close (fd);
g_unlink (tmp_name);
goto out;
}
if (length > 0)
{
gsize n_written;
errno = 0;
n_written = fwrite (contents, 1, length, file);
if (n_written < length)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to write file '%s': fwrite() failed: %s"),
display_name,
g_strerror (save_errno));
fclose (file);
g_unlink (tmp_name);
goto out;
}
}
errno = 0;
if (fclose (file) == EOF)
{
save_errno = 0;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to close file '%s': fclose() failed: %s"),
display_name,
g_strerror (save_errno));
g_unlink (tmp_name);
goto out;
}
retval = g_strdup (tmp_name);
out:
g_free (tmp_name);
g_free (display_name);
return retval;
}
/**
* g_file_set_contents:
* @filename: name of a file to write @contents to, in the GLib file name
* encoding
* @contents: string to write to the file
* @length: length of @contents, or -1 if @contents is a nul-terminated string
* @error: return location for a #GError, or %NULL
*
* Writes all of @contents to a file named @filename, with good error checking.
* If a file called @filename already exists it will be overwritten.
*
* This write is atomic in the sense that it is first written to a temporary
* file which is then renamed to the final name. Notes:
* <itemizedlist>
* <listitem>
* On Unix, if @filename already exists hard links to @filename will break.
* Also since the file is recreated, existing permissions, access control
* lists, metadata etc. may be lost. If @filename is a symbolic link,
* the link itself will be replaced, not the linked file.
* </listitem>
* <listitem>
* On Windows renaming a file will not remove an existing file with the
* new name, so on Windows there is a race condition between the existing
* file being removed and the temporary file being renamed.
* </listitem>
* <listitem>
* On Windows there is no way to remove a file that is open to some
* process, or mapped into memory. Thus, this function will fail if
* @filename already exists and is open.
* </listitem>