1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <gio/gio.h>
5 #include <gio/gfiledescriptorbased.h>
6 #ifdef G_OS_UNIX
7 #include <sys/stat.h>
8 #endif
9
10 static void
test_basic_for_file(GFile * file,const gchar * suffix)11 test_basic_for_file (GFile *file,
12 const gchar *suffix)
13 {
14 gchar *s;
15
16 s = g_file_get_basename (file);
17 g_assert_cmpstr (s, ==, "testfile");
18 g_free (s);
19
20 s = g_file_get_uri (file);
21 g_assert_true (g_str_has_prefix (s, "file://"));
22 g_assert_true (g_str_has_suffix (s, suffix));
23 g_free (s);
24
25 g_assert_true (g_file_has_uri_scheme (file, "file"));
26 s = g_file_get_uri_scheme (file);
27 g_assert_cmpstr (s, ==, "file");
28 g_free (s);
29 }
30
31 static void
test_basic(void)32 test_basic (void)
33 {
34 GFile *file;
35
36 file = g_file_new_for_path ("./some/directory/testfile");
37 test_basic_for_file (file, "/some/directory/testfile");
38 g_object_unref (file);
39 }
40
41 static void
test_build_filename(void)42 test_build_filename (void)
43 {
44 GFile *file;
45
46 file = g_file_new_build_filename (".", "some", "directory", "testfile", NULL);
47 test_basic_for_file (file, "/some/directory/testfile");
48 g_object_unref (file);
49
50 file = g_file_new_build_filename ("testfile", NULL);
51 test_basic_for_file (file, "/testfile");
52 g_object_unref (file);
53 }
54
55 static void
test_parent(void)56 test_parent (void)
57 {
58 GFile *file;
59 GFile *file2;
60 GFile *parent;
61 GFile *root;
62
63 file = g_file_new_for_path ("./some/directory/testfile");
64 file2 = g_file_new_for_path ("./some/directory");
65 root = g_file_new_for_path ("/");
66
67 g_assert_true (g_file_has_parent (file, file2));
68
69 parent = g_file_get_parent (file);
70 g_assert_true (g_file_equal (parent, file2));
71 g_object_unref (parent);
72
73 g_assert_null (g_file_get_parent (root));
74
75 g_object_unref (file);
76 g_object_unref (file2);
77 g_object_unref (root);
78 }
79
80 static void
test_child(void)81 test_child (void)
82 {
83 GFile *file;
84 GFile *child;
85 GFile *child2;
86
87 file = g_file_new_for_path ("./some/directory");
88 child = g_file_get_child (file, "child");
89 g_assert_true (g_file_has_parent (child, file));
90
91 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
92 g_assert_true (g_file_has_parent (child2, file));
93
94 g_object_unref (child);
95 g_object_unref (child2);
96 g_object_unref (file);
97 }
98
99 static void
test_empty_path(void)100 test_empty_path (void)
101 {
102 GFile *file = NULL;
103
104 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2328");
105 g_test_summary ("Check that creating a file with an empty path results in errors");
106
107 /* Creating the file must always succeed. */
108 file = g_file_new_for_path ("");
109 g_assert_nonnull (file);
110
111 /* But then querying its path should indicate it’s invalid. */
112 g_assert_null (g_file_get_path (file));
113 g_assert_null (g_file_get_basename (file));
114 g_assert_null (g_file_get_parent (file));
115
116 g_object_unref (file);
117 }
118
119 static void
test_type(void)120 test_type (void)
121 {
122 GFile *datapath_f;
123 GFile *file;
124 GFileType type;
125 GError *error = NULL;
126
127 datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
128
129 file = g_file_get_child (datapath_f, "g-icon.c");
130 type = g_file_query_file_type (file, 0, NULL);
131 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
132 g_object_unref (file);
133
134 file = g_file_get_child (datapath_f, "cert-tests");
135 type = g_file_query_file_type (file, 0, NULL);
136 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
137
138 g_file_read (file, NULL, &error);
139 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
140 g_error_free (error);
141 g_object_unref (file);
142
143 g_object_unref (datapath_f);
144 }
145
146 static void
test_parse_name(void)147 test_parse_name (void)
148 {
149 GFile *file;
150 gchar *name;
151
152 file = g_file_new_for_uri ("file://somewhere");
153 name = g_file_get_parse_name (file);
154 g_assert_cmpstr (name, ==, "file://somewhere");
155 g_object_unref (file);
156 g_free (name);
157
158 file = g_file_parse_name ("~foo");
159 name = g_file_get_parse_name (file);
160 g_assert_nonnull (name);
161 g_object_unref (file);
162 g_free (name);
163 }
164
165 typedef struct
166 {
167 GMainContext *context;
168 GFile *file;
169 GFileMonitor *monitor;
170 GOutputStream *ostream;
171 GInputStream *istream;
172 gint buffersize;
173 gint monitor_created;
174 gint monitor_deleted;
175 gint monitor_changed;
176 gchar *monitor_path;
177 gint pos;
178 const gchar *data;
179 gchar *buffer;
180 guint timeout;
181 gboolean file_deleted;
182 gboolean timed_out;
183 } CreateDeleteData;
184
185 static void
monitor_changed(GFileMonitor * monitor,GFile * file,GFile * other_file,GFileMonitorEvent event_type,gpointer user_data)186 monitor_changed (GFileMonitor *monitor,
187 GFile *file,
188 GFile *other_file,
189 GFileMonitorEvent event_type,
190 gpointer user_data)
191 {
192 CreateDeleteData *data = user_data;
193 gchar *path;
194 const gchar *peeked_path;
195
196 path = g_file_get_path (file);
197 peeked_path = g_file_peek_path (file);
198 g_assert_cmpstr (data->monitor_path, ==, path);
199 g_assert_cmpstr (path, ==, peeked_path);
200 g_free (path);
201
202 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
203 data->monitor_created++;
204 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
205 data->monitor_deleted++;
206 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
207 data->monitor_changed++;
208
209 g_main_context_wakeup (data->context);
210 }
211
212 static void
iclosed_cb(GObject * source,GAsyncResult * res,gpointer user_data)213 iclosed_cb (GObject *source,
214 GAsyncResult *res,
215 gpointer user_data)
216 {
217 CreateDeleteData *data = user_data;
218 GError *error;
219 gboolean ret;
220
221 error = NULL;
222 ret = g_input_stream_close_finish (data->istream, res, &error);
223 g_assert_no_error (error);
224 g_assert_true (ret);
225
226 g_assert_true (g_input_stream_is_closed (data->istream));
227
228 ret = g_file_delete (data->file, NULL, &error);
229 g_assert_true (ret);
230 g_assert_no_error (error);
231
232 data->file_deleted = TRUE;
233 g_main_context_wakeup (data->context);
234 }
235
236 static void
read_cb(GObject * source,GAsyncResult * res,gpointer user_data)237 read_cb (GObject *source,
238 GAsyncResult *res,
239 gpointer user_data)
240 {
241 CreateDeleteData *data = user_data;
242 GError *error;
243 gssize size;
244
245 error = NULL;
246 size = g_input_stream_read_finish (data->istream, res, &error);
247 g_assert_no_error (error);
248
249 data->pos += size;
250 if (data->pos < strlen (data->data))
251 {
252 g_input_stream_read_async (data->istream,
253 data->buffer + data->pos,
254 strlen (data->data) - data->pos,
255 0,
256 NULL,
257 read_cb,
258 data);
259 }
260 else
261 {
262 g_assert_cmpstr (data->buffer, ==, data->data);
263 g_assert_false (g_input_stream_is_closed (data->istream));
264 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
265 }
266 }
267
268 static void
ipending_cb(GObject * source,GAsyncResult * res,gpointer user_data)269 ipending_cb (GObject *source,
270 GAsyncResult *res,
271 gpointer user_data)
272 {
273 CreateDeleteData *data = user_data;
274 GError *error;
275
276 error = NULL;
277 g_input_stream_read_finish (data->istream, res, &error);
278 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
279 g_error_free (error);
280 }
281
282 static void
skipped_cb(GObject * source,GAsyncResult * res,gpointer user_data)283 skipped_cb (GObject *source,
284 GAsyncResult *res,
285 gpointer user_data)
286 {
287 CreateDeleteData *data = user_data;
288 GError *error;
289 gssize size;
290
291 error = NULL;
292 size = g_input_stream_skip_finish (data->istream, res, &error);
293 g_assert_no_error (error);
294 g_assert_cmpint (size, ==, data->pos);
295
296 g_input_stream_read_async (data->istream,
297 data->buffer + data->pos,
298 strlen (data->data) - data->pos,
299 0,
300 NULL,
301 read_cb,
302 data);
303 /* check that we get a pending error */
304 g_input_stream_read_async (data->istream,
305 data->buffer + data->pos,
306 strlen (data->data) - data->pos,
307 0,
308 NULL,
309 ipending_cb,
310 data);
311 }
312
313 static void
opened_cb(GObject * source,GAsyncResult * res,gpointer user_data)314 opened_cb (GObject *source,
315 GAsyncResult *res,
316 gpointer user_data)
317 {
318 GFileInputStream *base;
319 CreateDeleteData *data = user_data;
320 GError *error;
321
322 error = NULL;
323 base = g_file_read_finish (data->file, res, &error);
324 g_assert_no_error (error);
325
326 if (data->buffersize == 0)
327 data->istream = G_INPUT_STREAM (g_object_ref (base));
328 else
329 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
330 g_object_unref (base);
331
332 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
333
334 /* copy initial segment directly, then skip */
335 memcpy (data->buffer, data->data, 10);
336 data->pos = 10;
337
338 g_input_stream_skip_async (data->istream,
339 10,
340 0,
341 NULL,
342 skipped_cb,
343 data);
344 }
345
346 static void
oclosed_cb(GObject * source,GAsyncResult * res,gpointer user_data)347 oclosed_cb (GObject *source,
348 GAsyncResult *res,
349 gpointer user_data)
350 {
351 CreateDeleteData *data = user_data;
352 GError *error;
353 gboolean ret;
354
355 error = NULL;
356 ret = g_output_stream_close_finish (data->ostream, res, &error);
357 g_assert_no_error (error);
358 g_assert_true (ret);
359 g_assert_true (g_output_stream_is_closed (data->ostream));
360
361 g_file_read_async (data->file, 0, NULL, opened_cb, data);
362 }
363
364 static void
written_cb(GObject * source,GAsyncResult * res,gpointer user_data)365 written_cb (GObject *source,
366 GAsyncResult *res,
367 gpointer user_data)
368 {
369 CreateDeleteData *data = user_data;
370 gssize size;
371 GError *error;
372
373 error = NULL;
374 size = g_output_stream_write_finish (data->ostream, res, &error);
375 g_assert_no_error (error);
376
377 data->pos += size;
378 if (data->pos < strlen (data->data))
379 {
380 g_output_stream_write_async (data->ostream,
381 data->data + data->pos,
382 strlen (data->data) - data->pos,
383 0,
384 NULL,
385 written_cb,
386 data);
387 }
388 else
389 {
390 g_assert_false (g_output_stream_is_closed (data->ostream));
391 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
392 }
393 }
394
395 static void
opending_cb(GObject * source,GAsyncResult * res,gpointer user_data)396 opending_cb (GObject *source,
397 GAsyncResult *res,
398 gpointer user_data)
399 {
400 CreateDeleteData *data = user_data;
401 GError *error;
402
403 error = NULL;
404 g_output_stream_write_finish (data->ostream, res, &error);
405 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
406 g_error_free (error);
407 }
408
409 static void
created_cb(GObject * source,GAsyncResult * res,gpointer user_data)410 created_cb (GObject *source,
411 GAsyncResult *res,
412 gpointer user_data)
413 {
414 GFileOutputStream *base;
415 CreateDeleteData *data = user_data;
416 GError *error;
417
418 error = NULL;
419 base = g_file_create_finish (G_FILE (source), res, &error);
420 g_assert_no_error (error);
421 g_assert_true (g_file_query_exists (data->file, NULL));
422
423 if (data->buffersize == 0)
424 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
425 else
426 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
427 g_object_unref (base);
428
429 g_output_stream_write_async (data->ostream,
430 data->data,
431 strlen (data->data),
432 0,
433 NULL,
434 written_cb,
435 data);
436 /* check that we get a pending error */
437 g_output_stream_write_async (data->ostream,
438 data->data,
439 strlen (data->data),
440 0,
441 NULL,
442 opending_cb,
443 data);
444 }
445
446 static gboolean
stop_timeout(gpointer user_data)447 stop_timeout (gpointer user_data)
448 {
449 CreateDeleteData *data = user_data;
450
451 data->timed_out = TRUE;
452 g_main_context_wakeup (data->context);
453
454 return G_SOURCE_REMOVE;
455 }
456
457 /*
458 * This test does a fully async create-write-read-delete.
459 * Callbackistan.
460 */
461 static void
test_create_delete(gconstpointer d)462 test_create_delete (gconstpointer d)
463 {
464 GError *error;
465 CreateDeleteData *data;
466 GFileIOStream *iostream;
467
468 data = g_new0 (CreateDeleteData, 1);
469
470 data->buffersize = GPOINTER_TO_INT (d);
471 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
472 data->pos = 0;
473
474 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
475 &iostream, NULL);
476 g_assert_nonnull (data->file);
477 g_object_unref (iostream);
478
479 data->monitor_path = g_file_get_path (data->file);
480 remove (data->monitor_path);
481
482 g_assert_false (g_file_query_exists (data->file, NULL));
483
484 error = NULL;
485 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
486 g_assert_no_error (error);
487
488 /* This test doesn't work with GPollFileMonitor, because it assumes
489 * that the monitor will notice a create immediately followed by a
490 * delete, rather than coalescing them into nothing.
491 */
492 /* This test also doesn't work with GKqueueFileMonitor because of
493 * the same reason. Kqueue is able to return a kevent when a file is
494 * created or deleted in a directory. However, the kernel doesn't tell
495 * the program file names, so GKqueueFileMonitor has to calculate the
496 * difference itself. This is usually too slow for rapid file creation
497 * and deletion tests.
498 */
499 if (strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor") == 0 ||
500 strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GKqueueFileMonitor") == 0)
501 {
502 g_test_skip ("skipping test for this GFileMonitor implementation");
503 goto skip;
504 }
505
506 g_file_monitor_set_rate_limit (data->monitor, 100);
507
508 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
509
510 /* Use the global default main context */
511 data->context = NULL;
512 data->timeout = g_timeout_add_seconds (10, stop_timeout, data);
513
514 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
515
516 while (!data->timed_out &&
517 (data->monitor_created == 0 ||
518 data->monitor_deleted == 0 ||
519 data->monitor_changed == 0 ||
520 !data->file_deleted))
521 g_main_context_iteration (data->context, TRUE);
522
523 g_source_remove (data->timeout);
524
525 g_assert_false (data->timed_out);
526 g_assert_true (data->file_deleted);
527 g_assert_cmpint (data->monitor_created, ==, 1);
528 g_assert_cmpint (data->monitor_deleted, ==, 1);
529 g_assert_cmpint (data->monitor_changed, >, 0);
530
531 g_assert_false (g_file_monitor_is_cancelled (data->monitor));
532 g_file_monitor_cancel (data->monitor);
533 g_assert_true (g_file_monitor_is_cancelled (data->monitor));
534
535 g_clear_pointer (&data->context, g_main_context_unref);
536 g_object_unref (data->ostream);
537 g_object_unref (data->istream);
538
539 skip:
540 g_object_unref (data->monitor);
541 g_object_unref (data->file);
542 g_free (data->monitor_path);
543 g_free (data->buffer);
544 g_free (data);
545 }
546
547 static const gchar *original_data =
548 "/**\n"
549 " * g_file_replace_contents_async:\n"
550 "**/\n";
551
552 static const gchar *replace_data =
553 "/**\n"
554 " * g_file_replace_contents_async:\n"
555 " * @file: input #GFile.\n"
556 " * @contents: string of contents to replace the file with.\n"
557 " * @length: the length of @contents in bytes.\n"
558 " * @etag: (nullable): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
559 " * @make_backup: %TRUE if a backup should be created.\n"
560 " * @flags: a set of #GFileCreateFlags.\n"
561 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
562 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
563 " * @user_data: the data to pass to callback function\n"
564 " * \n"
565 " * Starts an asynchronous replacement of @file with the given \n"
566 " * @contents of @length bytes. @etag will replace the document's\n"
567 " * current entity tag.\n"
568 " * \n"
569 " * When this operation has completed, @callback will be called with\n"
570 " * @user_user data, and the operation can be finalized with \n"
571 " * g_file_replace_contents_finish().\n"
572 " * \n"
573 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
574 " * triggering the cancellable object from another thread. If the operation\n"
575 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
576 " * \n"
577 " * If @make_backup is %TRUE, this function will attempt to \n"
578 " * make a backup of @file.\n"
579 " **/\n";
580
581 typedef struct
582 {
583 GFile *file;
584 const gchar *data;
585 GMainLoop *loop;
586 gboolean again;
587 } ReplaceLoadData;
588
589 static void replaced_cb (GObject *source,
590 GAsyncResult *res,
591 gpointer user_data);
592
593 static void
loaded_cb(GObject * source,GAsyncResult * res,gpointer user_data)594 loaded_cb (GObject *source,
595 GAsyncResult *res,
596 gpointer user_data)
597 {
598 ReplaceLoadData *data = user_data;
599 gboolean ret;
600 GError *error;
601 gchar *contents;
602 gsize length;
603
604 error = NULL;
605 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
606 g_assert_true (ret);
607 g_assert_no_error (error);
608 g_assert_cmpint (length, ==, strlen (data->data));
609 g_assert_cmpstr (contents, ==, data->data);
610
611 g_free (contents);
612
613 if (data->again)
614 {
615 data->again = FALSE;
616 data->data = "pi pa po";
617
618 g_file_replace_contents_async (data->file,
619 data->data,
620 strlen (data->data),
621 NULL,
622 FALSE,
623 0,
624 NULL,
625 replaced_cb,
626 data);
627 }
628 else
629 {
630 error = NULL;
631 ret = g_file_delete (data->file, NULL, &error);
632 g_assert_no_error (error);
633 g_assert_true (ret);
634 g_assert_false (g_file_query_exists (data->file, NULL));
635
636 g_main_loop_quit (data->loop);
637 }
638 }
639
640 static void
replaced_cb(GObject * source,GAsyncResult * res,gpointer user_data)641 replaced_cb (GObject *source,
642 GAsyncResult *res,
643 gpointer user_data)
644 {
645 ReplaceLoadData *data = user_data;
646 GError *error;
647
648 error = NULL;
649 g_file_replace_contents_finish (data->file, res, NULL, &error);
650 g_assert_no_error (error);
651
652 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
653 }
654
655 static void
test_replace_load(void)656 test_replace_load (void)
657 {
658 ReplaceLoadData *data;
659 const gchar *path;
660 GFileIOStream *iostream;
661
662 data = g_new0 (ReplaceLoadData, 1);
663 data->again = TRUE;
664 data->data = replace_data;
665
666 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
667 &iostream, NULL);
668 g_assert_nonnull (data->file);
669 g_object_unref (iostream);
670
671 path = g_file_peek_path (data->file);
672 remove (path);
673
674 g_assert_false (g_file_query_exists (data->file, NULL));
675
676 data->loop = g_main_loop_new (NULL, FALSE);
677
678 g_file_replace_contents_async (data->file,
679 data->data,
680 strlen (data->data),
681 NULL,
682 FALSE,
683 0,
684 NULL,
685 replaced_cb,
686 data);
687
688 g_main_loop_run (data->loop);
689
690 g_main_loop_unref (data->loop);
691 g_object_unref (data->file);
692 g_free (data);
693 }
694
695 static void
test_replace_cancel(void)696 test_replace_cancel (void)
697 {
698 GFile *tmpdir, *file;
699 GFileOutputStream *ostream;
700 GFileEnumerator *fenum;
701 GFileInfo *info;
702 GCancellable *cancellable;
703 gchar *path;
704 gchar *contents;
705 gsize nwrote, length;
706 guint count;
707 GError *error = NULL;
708
709 g_test_bug ("https://bugzilla.gnome.org/629301");
710
711 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
712 g_assert_no_error (error);
713 tmpdir = g_file_new_for_path (path);
714 g_free (path);
715
716 file = g_file_get_child (tmpdir, "file");
717 g_file_replace_contents (file,
718 original_data,
719 strlen (original_data),
720 NULL, FALSE, 0, NULL,
721 NULL, &error);
722 g_assert_no_error (error);
723
724 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
725 g_assert_no_error (error);
726
727 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
728 replace_data, strlen (replace_data),
729 &nwrote, NULL, &error);
730 g_assert_no_error (error);
731 g_assert_cmpint (nwrote, ==, strlen (replace_data));
732
733 /* At this point there should be two files; the original and the
734 * temporary.
735 */
736 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
737 g_assert_no_error (error);
738
739 info = g_file_enumerator_next_file (fenum, NULL, &error);
740 g_assert_no_error (error);
741 g_assert_nonnull (info);
742 g_object_unref (info);
743 info = g_file_enumerator_next_file (fenum, NULL, &error);
744 g_assert_no_error (error);
745 g_assert_nonnull (info);
746 g_object_unref (info);
747
748 g_file_enumerator_close (fenum, NULL, &error);
749 g_assert_no_error (error);
750 g_object_unref (fenum);
751
752 /* Also test the g_file_enumerator_iterate() API */
753 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
754 g_assert_no_error (error);
755 count = 0;
756
757 while (TRUE)
758 {
759 gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
760 g_assert_true (ret);
761 g_assert_no_error (error);
762 if (!info)
763 break;
764 count++;
765 }
766 g_assert_cmpint (count, ==, 2);
767
768 g_file_enumerator_close (fenum, NULL, &error);
769 g_assert_no_error (error);
770 g_object_unref (fenum);
771
772 /* Now test just getting child from the g_file_enumerator_iterate() API */
773 fenum = g_file_enumerate_children (tmpdir, "standard::name", 0, NULL, &error);
774 g_assert_no_error (error);
775 count = 0;
776
777 while (TRUE)
778 {
779 GFile *child;
780 gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
781
782 g_assert_true (ret);
783 g_assert_no_error (error);
784
785 if (!child)
786 break;
787
788 g_assert_true (G_IS_FILE (child));
789 count++;
790 }
791 g_assert_cmpint (count, ==, 2);
792
793 g_file_enumerator_close (fenum, NULL, &error);
794 g_assert_no_error (error);
795 g_object_unref (fenum);
796
797 /* Make sure the temporary gets deleted even if we cancel. */
798 cancellable = g_cancellable_new ();
799 g_cancellable_cancel (cancellable);
800 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
801 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
802 g_clear_error (&error);
803
804 g_object_unref (cancellable);
805 g_object_unref (ostream);
806
807 /* Make sure that file contents wasn't actually replaced. */
808 g_file_load_contents (file,
809 NULL,
810 &contents,
811 &length,
812 NULL,
813 &error);
814 g_assert_no_error (error);
815 g_assert_cmpstr (contents, ==, original_data);
816 g_free (contents);
817
818 g_file_delete (file, NULL, &error);
819 g_assert_no_error (error);
820 g_object_unref (file);
821
822 /* This will only succeed if the temp file was deleted. */
823 g_file_delete (tmpdir, NULL, &error);
824 g_assert_no_error (error);
825 g_object_unref (tmpdir);
826 }
827
828 static void
test_replace_symlink(void)829 test_replace_symlink (void)
830 {
831 #ifdef G_OS_UNIX
832 gchar *tmpdir_path = NULL;
833 GFile *tmpdir = NULL, *source_file = NULL, *target_file = NULL;
834 GFileOutputStream *stream = NULL;
835 const gchar *new_contents = "this is a test message which should be written to source and not target";
836 gsize n_written;
837 GFileEnumerator *enumerator = NULL;
838 GFileInfo *info = NULL;
839 gchar *contents = NULL;
840 gsize length = 0;
841 GError *local_error = NULL;
842
843 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2325");
844 g_test_summary ("Test that G_FILE_CREATE_REPLACE_DESTINATION doesn’t follow symlinks");
845
846 /* Create a fresh, empty working directory. */
847 tmpdir_path = g_dir_make_tmp ("g_file_replace_symlink_XXXXXX", &local_error);
848 g_assert_no_error (local_error);
849 tmpdir = g_file_new_for_path (tmpdir_path);
850
851 g_test_message ("Using temporary directory %s", tmpdir_path);
852 g_free (tmpdir_path);
853
854 /* Create symlink `source` which points to `target`. */
855 source_file = g_file_get_child (tmpdir, "source");
856 target_file = g_file_get_child (tmpdir, "target");
857 g_file_make_symbolic_link (source_file, "target", NULL, &local_error);
858 g_assert_no_error (local_error);
859
860 /* Ensure that `target` doesn’t exist */
861 g_assert_false (g_file_query_exists (target_file, NULL));
862
863 /* Replace the `source` symlink with a regular file using
864 * %G_FILE_CREATE_REPLACE_DESTINATION, which should replace it *without*
865 * following the symlink */
866 stream = g_file_replace (source_file, NULL, FALSE /* no backup */,
867 G_FILE_CREATE_REPLACE_DESTINATION, NULL, &local_error);
868 g_assert_no_error (local_error);
869
870 g_output_stream_write_all (G_OUTPUT_STREAM (stream), new_contents, strlen (new_contents),
871 &n_written, NULL, &local_error);
872 g_assert_no_error (local_error);
873 g_assert_cmpint (n_written, ==, strlen (new_contents));
874
875 g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, &local_error);
876 g_assert_no_error (local_error);
877
878 g_clear_object (&stream);
879
880 /* At this point, there should still only be one file: `source`. It should
881 * now be a regular file. `target` should not exist. */
882 enumerator = g_file_enumerate_children (tmpdir,
883 G_FILE_ATTRIBUTE_STANDARD_NAME ","
884 G_FILE_ATTRIBUTE_STANDARD_TYPE,
885 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
886 g_assert_no_error (local_error);
887
888 info = g_file_enumerator_next_file (enumerator, NULL, &local_error);
889 g_assert_no_error (local_error);
890 g_assert_nonnull (info);
891
892 g_assert_cmpstr (g_file_info_get_name (info), ==, "source");
893 g_assert_cmpint (g_file_info_get_file_type (info), ==, G_FILE_TYPE_REGULAR);
894
895 g_clear_object (&info);
896
897 info = g_file_enumerator_next_file (enumerator, NULL, &local_error);
898 g_assert_no_error (local_error);
899 g_assert_null (info);
900
901 g_file_enumerator_close (enumerator, NULL, &local_error);
902 g_assert_no_error (local_error);
903 g_clear_object (&enumerator);
904
905 /* Double-check that `target` doesn’t exist */
906 g_assert_false (g_file_query_exists (target_file, NULL));
907
908 /* Check the content of `source`. */
909 g_file_load_contents (source_file,
910 NULL,
911 &contents,
912 &length,
913 NULL,
914 &local_error);
915 g_assert_no_error (local_error);
916 g_assert_cmpstr (contents, ==, new_contents);
917 g_assert_cmpuint (length, ==, strlen (new_contents));
918 g_free (contents);
919
920 /* Tidy up. */
921 g_file_delete (source_file, NULL, &local_error);
922 g_assert_no_error (local_error);
923
924 g_file_delete (tmpdir, NULL, &local_error);
925 g_assert_no_error (local_error);
926
927 g_clear_object (&target_file);
928 g_clear_object (&source_file);
929 g_clear_object (&tmpdir);
930 #else /* if !G_OS_UNIX */
931 g_test_skip ("Symlink replacement tests can only be run on Unix")
932 #endif
933 }
934
935 /* FIXME: These tests have only been checked on Linux. Most of them are probably
936 * applicable on Windows, too, but that has not been tested yet.
937 * See https://gitlab.gnome.org/GNOME/glib/-/issues/2325 */
938 #ifdef __linux__
939
940 /* Different kinds of file which create_test_file() can create. */
941 typedef enum
942 {
943 FILE_TEST_SETUP_TYPE_NONEXISTENT,
944 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY,
945 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY,
946 FILE_TEST_SETUP_TYPE_DIRECTORY,
947 FILE_TEST_SETUP_TYPE_SOCKET,
948 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING,
949 FILE_TEST_SETUP_TYPE_SYMLINK_VALID,
950 } FileTestSetupType;
951
952 /* Create file `tmpdir/basename`, of type @setup_type, and chmod it to
953 * @setup_mode. Return the #GFile representing it. Abort on any errors. */
954 static GFile *
create_test_file(GFile * tmpdir,const gchar * basename,FileTestSetupType setup_type,guint setup_mode)955 create_test_file (GFile *tmpdir,
956 const gchar *basename,
957 FileTestSetupType setup_type,
958 guint setup_mode)
959 {
960 GFile *test_file = g_file_get_child (tmpdir, basename);
961 gchar *target_basename = g_strdup_printf ("%s-target", basename); /* for symlinks */
962 GFile *target_file = g_file_get_child (tmpdir, target_basename);
963 GError *local_error = NULL;
964
965 switch (setup_type)
966 {
967 case FILE_TEST_SETUP_TYPE_NONEXISTENT:
968 /* Nothing to do here. */
969 g_assert (setup_mode == 0);
970 break;
971 case FILE_TEST_SETUP_TYPE_REGULAR_EMPTY:
972 case FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY:
973 {
974 gchar *contents = NULL;
975
976 if (setup_type == FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY)
977 contents = g_strdup_printf ("this is some test content in %s", basename);
978 else
979 contents = g_strdup ("");
980
981 g_file_set_contents (g_file_peek_path (test_file), contents, -1, &local_error);
982 g_assert_no_error (local_error);
983
984 g_file_set_attribute_uint32 (test_file, G_FILE_ATTRIBUTE_UNIX_MODE,
985 setup_mode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
986 NULL, &local_error);
987 g_assert_no_error (local_error);
988
989 g_free (contents);
990 break;
991 }
992 case FILE_TEST_SETUP_TYPE_DIRECTORY:
993 g_assert (setup_mode == 0);
994
995 g_file_make_directory (test_file, NULL, &local_error);
996 g_assert_no_error (local_error);
997 break;
998 case FILE_TEST_SETUP_TYPE_SOCKET:
999 g_assert_no_errno (mknod (g_file_peek_path (test_file), S_IFSOCK | setup_mode, 0));
1000 break;
1001 case FILE_TEST_SETUP_TYPE_SYMLINK_VALID:
1002 g_file_set_contents (g_file_peek_path (target_file), "target file", -1, &local_error);
1003 g_assert_no_error (local_error);
1004 G_GNUC_FALLTHROUGH;
1005 case FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING:
1006 /* Permissions on a symlink are not used by the kernel, so are only
1007 * applicable if the symlink is valid (and are applied to the target) */
1008 g_assert (setup_type != FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING || setup_mode == 0);
1009
1010 g_file_make_symbolic_link (test_file, target_basename, NULL, &local_error);
1011 g_assert_no_error (local_error);
1012
1013 if (setup_type == FILE_TEST_SETUP_TYPE_SYMLINK_VALID)
1014 {
1015 g_file_set_attribute_uint32 (test_file, G_FILE_ATTRIBUTE_UNIX_MODE,
1016 setup_mode, G_FILE_QUERY_INFO_NONE,
1017 NULL, &local_error);
1018 g_assert_no_error (local_error);
1019 }
1020
1021 if (setup_type == FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING)
1022 {
1023 /* Ensure that the target doesn’t exist */
1024 g_assert_false (g_file_query_exists (target_file, NULL));
1025 }
1026 break;
1027 default:
1028 g_assert_not_reached ();
1029 }
1030
1031 g_clear_object (&target_file);
1032 g_free (target_basename);
1033
1034 return g_steal_pointer (&test_file);
1035 }
1036
1037 /* Check that @test_file is of the @expected_type, has the @expected_mode, and
1038 * (if it’s a regular file) has the @expected_contents or (if it’s a symlink)
1039 * has the symlink target given by @expected_contents.
1040 *
1041 * @test_file must point to the file `tmpdir/basename`.
1042 *
1043 * Aborts on any errors or mismatches against the expectations.
1044 */
1045 static void
check_test_file(GFile * test_file,GFile * tmpdir,const gchar * basename,FileTestSetupType expected_type,guint expected_mode,const gchar * expected_contents)1046 check_test_file (GFile *test_file,
1047 GFile *tmpdir,
1048 const gchar *basename,
1049 FileTestSetupType expected_type,
1050 guint expected_mode,
1051 const gchar *expected_contents)
1052 {
1053 gchar *target_basename = g_strdup_printf ("%s-target", basename); /* for symlinks */
1054 GFile *target_file = g_file_get_child (tmpdir, target_basename);
1055 GFileType test_file_type = g_file_query_file_type (test_file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL);
1056 GFileInfo *info = NULL;
1057 GError *local_error = NULL;
1058
1059 switch (expected_type)
1060 {
1061 case FILE_TEST_SETUP_TYPE_NONEXISTENT:
1062 g_assert (expected_mode == 0);
1063 g_assert (expected_contents == NULL);
1064
1065 g_assert_false (g_file_query_exists (test_file, NULL));
1066 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_UNKNOWN);
1067 break;
1068 case FILE_TEST_SETUP_TYPE_REGULAR_EMPTY:
1069 case FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY:
1070 g_assert (expected_type != FILE_TEST_SETUP_TYPE_REGULAR_EMPTY || expected_contents == NULL);
1071 g_assert (expected_type != FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY || expected_contents != NULL);
1072
1073 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_REGULAR);
1074
1075 info = g_file_query_info (test_file,
1076 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1077 G_FILE_ATTRIBUTE_UNIX_MODE,
1078 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
1079 g_assert_no_error (local_error);
1080
1081 if (expected_type == FILE_TEST_SETUP_TYPE_REGULAR_EMPTY)
1082 g_assert_cmpint (g_file_info_get_size (info), ==, 0);
1083 else
1084 g_assert_cmpint (g_file_info_get_size (info), >, 0);
1085
1086 if (expected_contents != NULL)
1087 {
1088 gchar *contents = NULL;
1089 gsize length = 0;
1090
1091 g_file_get_contents (g_file_peek_path (test_file), &contents, &length, &local_error);
1092 g_assert_no_error (local_error);
1093
1094 g_assert_cmpstr (contents, ==, expected_contents);
1095 g_free (contents);
1096 }
1097
1098 g_assert_cmpuint (g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE) & 0777, ==, expected_mode);
1099
1100 break;
1101 case FILE_TEST_SETUP_TYPE_DIRECTORY:
1102 g_assert (expected_mode == 0);
1103 g_assert (expected_contents == NULL);
1104
1105 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_DIRECTORY);
1106 break;
1107 case FILE_TEST_SETUP_TYPE_SOCKET:
1108 g_assert (expected_contents == NULL);
1109
1110 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_SPECIAL);
1111
1112 info = g_file_query_info (test_file,
1113 G_FILE_ATTRIBUTE_UNIX_MODE,
1114 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
1115 g_assert_no_error (local_error);
1116
1117 g_assert_cmpuint (g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE) & 0777, ==, expected_mode);
1118 break;
1119 case FILE_TEST_SETUP_TYPE_SYMLINK_VALID:
1120 case FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING:
1121 {
1122 GFile *symlink_target_file = NULL;
1123
1124 /* Permissions on a symlink are not used by the kernel, so are only
1125 * applicable if the symlink is valid (and are applied to the target) */
1126 g_assert (expected_type != FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING || expected_mode == 0);
1127 g_assert (expected_contents != NULL);
1128
1129 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_SYMBOLIC_LINK);
1130
1131 info = g_file_query_info (test_file,
1132 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
1133 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
1134 g_assert_no_error (local_error);
1135
1136 g_assert_cmpstr (g_file_info_get_symlink_target (info), ==, expected_contents);
1137
1138 symlink_target_file = g_file_get_child (tmpdir, g_file_info_get_symlink_target (info));
1139 if (expected_type == FILE_TEST_SETUP_TYPE_SYMLINK_VALID)
1140 g_assert_true (g_file_query_exists (symlink_target_file, NULL));
1141 else
1142 g_assert_false (g_file_query_exists (symlink_target_file, NULL));
1143
1144 if (expected_type == FILE_TEST_SETUP_TYPE_SYMLINK_VALID)
1145 {
1146 GFileInfo *target_info = NULL;
1147
1148 /* Need to re-query the info so we follow symlinks */
1149 target_info = g_file_query_info (test_file,
1150 G_FILE_ATTRIBUTE_UNIX_MODE,
1151 G_FILE_QUERY_INFO_NONE, NULL, &local_error);
1152 g_assert_no_error (local_error);
1153
1154 g_assert_cmpuint (g_file_info_get_attribute_uint32 (target_info, G_FILE_ATTRIBUTE_UNIX_MODE) & 0777, ==, expected_mode);
1155
1156 g_clear_object (&target_info);
1157 }
1158
1159 g_clear_object (&symlink_target_file);
1160 break;
1161 }
1162 default:
1163 g_assert_not_reached ();
1164 }
1165
1166 g_clear_object (&info);
1167 g_clear_object (&target_file);
1168 g_free (target_basename);
1169 }
1170
1171 #endif /* __linux__ */
1172
1173 /* A big test for g_file_replace() and g_file_replace_readwrite(). The
1174 * @test_data is a boolean: %TRUE to test g_file_replace_readwrite(), %FALSE to
1175 * test g_file_replace(). The test setup and checks are identical for both
1176 * functions; in the case of testing g_file_replace_readwrite(), only the output
1177 * stream side of the returned #GIOStream is tested. i.e. We test the write
1178 * behaviour of both functions is identical.
1179 *
1180 * This is intended to test all static behaviour of the function: for each test
1181 * scenario, a temporary directory is set up with a source file (and maybe some
1182 * other files) in a set configuration, g_file_replace{,_readwrite}() is called,
1183 * and the final state of the directory is checked.
1184 *
1185 * This test does not check dynamic behaviour or race conditions. For example,
1186 * it does not test what happens if the source file is deleted from another
1187 * process half-way through a call to g_file_replace().
1188 */
1189 static void
test_replace(gconstpointer test_data)1190 test_replace (gconstpointer test_data)
1191 {
1192 #ifdef __linux__
1193 gboolean read_write = GPOINTER_TO_UINT (test_data);
1194 const gchar *new_contents = "this is a new test message which should be written to source";
1195 const gchar *original_source_contents = "this is some test content in source";
1196 const gchar *original_backup_contents = "this is some test content in source~";
1197 mode_t current_umask = umask (0);
1198 guint32 default_public_mode = 0666 & ~current_umask;
1199 guint32 default_private_mode = 0600;
1200
1201 const struct
1202 {
1203 /* Arguments to pass to g_file_replace(). */
1204 gboolean replace_make_backup;
1205 GFileCreateFlags replace_flags;
1206 const gchar *replace_etag; /* (nullable) */
1207
1208 /* File system setup. */
1209 FileTestSetupType setup_source_type;
1210 guint setup_source_mode;
1211 FileTestSetupType setup_backup_type;
1212 guint setup_backup_mode;
1213
1214 /* Expected results. */
1215 gboolean expected_success;
1216 GQuark expected_error_domain;
1217 gint expected_error_code;
1218
1219 /* Expected final file system state. */
1220 guint expected_n_files;
1221 FileTestSetupType expected_source_type;
1222 guint expected_source_mode;
1223 const gchar *expected_source_contents; /* content for a regular file, or target for a symlink; NULL otherwise */
1224 FileTestSetupType expected_backup_type;
1225 guint expected_backup_mode;
1226 const gchar *expected_backup_contents; /* content for a regular file, or target for a symlink; NULL otherwise */
1227 }
1228 tests[] =
1229 {
1230 /* replace_make_backup == FALSE, replace_flags == NONE, replace_etag == NULL,
1231 * all the different values of setup_source_type, mostly with a backup
1232 * file created to check it’s not modified */
1233 {
1234 FALSE, G_FILE_CREATE_NONE, NULL,
1235 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1236 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1237 TRUE, 0, 0,
1238 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1239 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1240 },
1241 {
1242 FALSE, G_FILE_CREATE_NONE, NULL,
1243 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1244 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1245 TRUE, 0, 0,
1246 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1247 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1248 },
1249 {
1250 FALSE, G_FILE_CREATE_NONE, NULL,
1251 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1252 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1253 TRUE, 0, 0,
1254 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1255 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1256 },
1257 {
1258 FALSE, G_FILE_CREATE_NONE, NULL,
1259 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1260 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1261 FALSE, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1262 2, FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1263 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1264 },
1265 {
1266 FALSE, G_FILE_CREATE_NONE, NULL,
1267 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1268 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1269 FALSE, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
1270 2, FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode, NULL,
1271 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1272 },
1273 {
1274 FALSE, G_FILE_CREATE_NONE, NULL,
1275 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1276 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1277 TRUE, 0, 0,
1278 3, FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1279 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1280 },
1281 {
1282 FALSE, G_FILE_CREATE_NONE, NULL,
1283 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1284 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1285 TRUE, 0, 0,
1286 3, FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1287 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1288 },
1289
1290 /* replace_etag set to an invalid value, with setup_source_type as a
1291 * regular non-empty file; replacement should fail */
1292 {
1293 FALSE, G_FILE_CREATE_NONE, "incorrect etag",
1294 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1295 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1296 FALSE, G_IO_ERROR, G_IO_ERROR_WRONG_ETAG,
1297 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1298 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1299 },
1300
1301 /* replace_make_backup == TRUE, replace_flags == NONE, replace_etag == NULL,
1302 * all the different values of setup_source_type, with a backup
1303 * file created to check it’s either replaced or the operation fails */
1304 {
1305 TRUE, G_FILE_CREATE_NONE, NULL,
1306 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1307 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1308 TRUE, 0, 0,
1309 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1310 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1311 },
1312 {
1313 TRUE, G_FILE_CREATE_NONE, NULL,
1314 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1315 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1316 TRUE, 0, 0,
1317 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1318 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode, NULL,
1319 },
1320 {
1321 TRUE, G_FILE_CREATE_NONE, NULL,
1322 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1323 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1324 TRUE, 0, 0,
1325 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1326 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1327 },
1328 {
1329 TRUE, G_FILE_CREATE_NONE, NULL,
1330 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1331 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1332 FALSE, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1333 2, FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1334 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1335 },
1336 {
1337 TRUE, G_FILE_CREATE_NONE, NULL,
1338 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1339 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1340 FALSE, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
1341 2, FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode, NULL,
1342 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1343 },
1344 {
1345 TRUE, G_FILE_CREATE_NONE, NULL,
1346 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1347 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1348 TRUE, 0, 0,
1349 /* The final situation here is a bit odd; the backup file is a bit
1350 * pointless as the original source file was a dangling symlink.
1351 * Theoretically the backup file should be that symlink, pointing to
1352 * `source-target`, and hence no longer dangling, as that file has now
1353 * been created as the new source content, since REPLACE_DESTINATION was
1354 * not specified. However, the code instead creates an empty regular
1355 * file as the backup. FIXME: This seems acceptable for now, but not
1356 * entirely ideal and would be good to fix at some point. */
1357 3, FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1358 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, 0777 & ~current_umask, NULL,
1359 },
1360 {
1361 TRUE, G_FILE_CREATE_NONE, NULL,
1362 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1363 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1364 TRUE, 0, 0,
1365 /* FIXME: The permissions for the backup file are just the default umask,
1366 * but should probably be the same as the permissions for the source
1367 * file (`default_public_mode`). This probably arises from the fact that
1368 * symlinks don’t have permissions. */
1369 3, FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1370 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, 0777 & ~current_umask, "target file",
1371 },
1372
1373 /* replace_make_backup == TRUE, replace_flags == NONE, replace_etag == NULL,
1374 * setup_source_type is a regular file, with a backup file of every type
1375 * created to check it’s either replaced or the operation fails */
1376 {
1377 TRUE, G_FILE_CREATE_NONE, NULL,
1378 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1379 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1380 TRUE, 0, 0,
1381 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1382 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1383 },
1384 {
1385 TRUE, G_FILE_CREATE_NONE, NULL,
1386 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1387 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1388 TRUE, 0, 0,
1389 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1390 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1391 },
1392 {
1393 TRUE, G_FILE_CREATE_NONE, NULL,
1394 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1395 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1396 TRUE, 0, 0,
1397 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1398 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1399 },
1400 {
1401 TRUE, G_FILE_CREATE_NONE, NULL,
1402 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1403 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1404 FALSE, G_IO_ERROR, G_IO_ERROR_CANT_CREATE_BACKUP,
1405 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1406 FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1407 },
1408 {
1409 TRUE, G_FILE_CREATE_NONE, NULL,
1410 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1411 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1412 TRUE, 0, 0,
1413 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1414 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1415 },
1416 {
1417 TRUE, G_FILE_CREATE_NONE, NULL,
1418 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1419 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1420 TRUE, 0, 0,
1421 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1422 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1423 },
1424 {
1425 TRUE, G_FILE_CREATE_NONE, NULL,
1426 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1427 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1428 TRUE, 0, 0,
1429 /* the third file is `source~-target`, the original target of the old
1430 * backup symlink */
1431 3, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1432 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1433 },
1434
1435 /* replace_make_backup == FALSE, replace_flags == REPLACE_DESTINATION,
1436 * replace_etag == NULL, all the different values of setup_source_type,
1437 * mostly with a backup file created to check it’s not modified */
1438 {
1439 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1440 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1441 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1442 TRUE, 0, 0,
1443 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1444 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1445 },
1446 {
1447 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1448 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1449 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1450 TRUE, 0, 0,
1451 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1452 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1453 },
1454 {
1455 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1456 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1457 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1458 TRUE, 0, 0,
1459 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1460 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1461 },
1462 {
1463 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1464 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1465 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1466 FALSE, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1467 2, FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1468 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1469 },
1470 {
1471 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1472 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1473 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1474 FALSE, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
1475 2, FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode, NULL,
1476 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1477 },
1478 {
1479 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1480 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1481 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1482 TRUE, 0, 0,
1483 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1484 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1485 },
1486 {
1487 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1488 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1489 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1490 TRUE, 0, 0,
1491 /* the third file is `source-target`, the original target of the old
1492 * source file */
1493 3, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1494 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1495 },
1496
1497 /* replace_flags == REPLACE_DESTINATION, replace_etag set to an invalid
1498 * value, with setup_source_type as a regular non-empty file; replacement
1499 * should fail */
1500 {
1501 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, "incorrect etag",
1502 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1503 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1504 FALSE, G_IO_ERROR, G_IO_ERROR_WRONG_ETAG,
1505 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1506 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1507 },
1508
1509 /* replace_make_backup == TRUE, replace_flags == REPLACE_DESTINATION,
1510 * replace_etag == NULL, all the different values of setup_source_type,
1511 * with a backup file created to check it’s either replaced or the
1512 * operation fails */
1513 {
1514 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1515 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1516 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1517 TRUE, 0, 0,
1518 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1519 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1520 },
1521 {
1522 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1523 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1524 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1525 TRUE, 0, 0,
1526 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1527 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode, NULL,
1528 },
1529 {
1530 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1531 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1532 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1533 TRUE, 0, 0,
1534 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1535 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1536 },
1537 {
1538 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1539 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1540 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1541 FALSE, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1542 2, FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1543 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1544 },
1545 {
1546 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1547 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1548 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1549 FALSE, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
1550 2, FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode, NULL,
1551 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1552 },
1553 {
1554 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1555 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1556 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1557 TRUE, 0, 0,
1558 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1559 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0, "source-target",
1560 },
1561 {
1562 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1563 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1564 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1565 TRUE, 0, 0,
1566 /* the third file is `source-target`, the original target of the old
1567 * source file */
1568 3, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1569 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1570 },
1571
1572 /* replace_make_backup == TRUE, replace_flags == REPLACE_DESTINATION,
1573 * replace_etag == NULL, setup_source_type is a regular file, with a
1574 * backup file of every type created to check it’s either replaced or the
1575 * operation fails */
1576 {
1577 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1578 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1579 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1580 TRUE, 0, 0,
1581 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1582 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1583 },
1584 {
1585 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1586 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1587 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1588 TRUE, 0, 0,
1589 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1590 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1591 },
1592 {
1593 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1594 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1595 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1596 TRUE, 0, 0,
1597 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1598 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1599 },
1600 {
1601 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1602 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1603 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1604 FALSE, G_IO_ERROR, G_IO_ERROR_CANT_CREATE_BACKUP,
1605 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1606 FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1607 },
1608 {
1609 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1610 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1611 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1612 TRUE, 0, 0,
1613 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1614 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1615 },
1616 {
1617 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1618 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1619 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1620 TRUE, 0, 0,
1621 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1622 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1623 },
1624 {
1625 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1626 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1627 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1628 TRUE, 0, 0,
1629 /* the third file is `source~-target`, the original target of the old
1630 * backup symlink */
1631 3, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1632 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1633 },
1634
1635 /* several different setups with replace_flags == PRIVATE */
1636 {
1637 FALSE, G_FILE_CREATE_PRIVATE, NULL,
1638 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1639 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1640 TRUE, 0, 0,
1641 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_private_mode, new_contents,
1642 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1643 },
1644 {
1645 FALSE, G_FILE_CREATE_PRIVATE, NULL,
1646 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1647 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1648 TRUE, 0, 0,
1649 /* the file isn’t being replaced, so it should keep its existing permissions */
1650 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1651 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1652 },
1653 {
1654 FALSE, G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1655 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1656 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1657 TRUE, 0, 0,
1658 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_private_mode, new_contents,
1659 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1660 },
1661 {
1662 FALSE, G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1663 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1664 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1665 TRUE, 0, 0,
1666 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_private_mode, new_contents,
1667 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1668 },
1669
1670 /* make the initial source file unreadable, so the replace operation
1671 * should fail */
1672 {
1673 FALSE, G_FILE_CREATE_NONE, NULL,
1674 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, 0 /* most restrictive permissions */,
1675 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1676 FALSE, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
1677 1, FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, 0, NULL,
1678 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1679 },
1680 };
1681 gsize i;
1682
1683 g_test_summary ("Test various situations for g_file_replace()");
1684
1685 /* Reset the umask after querying it above. There’s no way to query it without
1686 * changing it. */
1687 umask (current_umask);
1688 g_test_message ("Current umask: %u", current_umask);
1689
1690 for (i = 0; i < G_N_ELEMENTS (tests); i++)
1691 {
1692 gchar *tmpdir_path = NULL;
1693 GFile *tmpdir = NULL, *source_file = NULL, *backup_file = NULL;
1694 GFileOutputStream *output_stream = NULL;
1695 GFileIOStream *io_stream = NULL;
1696 GFileEnumerator *enumerator = NULL;
1697 GFileInfo *info = NULL;
1698 guint n_files;
1699 GError *local_error = NULL;
1700
1701 /* Create a fresh, empty working directory. */
1702 tmpdir_path = g_dir_make_tmp ("g_file_replace_XXXXXX", &local_error);
1703 g_assert_no_error (local_error);
1704 tmpdir = g_file_new_for_path (tmpdir_path);
1705
1706 g_test_message ("Test %" G_GSIZE_FORMAT ", using temporary directory %s", i, tmpdir_path);
1707 g_free (tmpdir_path);
1708
1709 /* Set up the test directory. */
1710 source_file = create_test_file (tmpdir, "source", tests[i].setup_source_type, tests[i].setup_source_mode);
1711 backup_file = create_test_file (tmpdir, "source~", tests[i].setup_backup_type, tests[i].setup_backup_mode);
1712
1713 /* Replace the source file. Check the error state only after finishing
1714 * writing, as the replace operation is split across g_file_replace() and
1715 * g_output_stream_close(). */
1716 if (read_write)
1717 io_stream = g_file_replace_readwrite (source_file,
1718 tests[i].replace_etag,
1719 tests[i].replace_make_backup,
1720 tests[i].replace_flags,
1721 NULL,
1722 &local_error);
1723 else
1724 output_stream = g_file_replace (source_file,
1725 tests[i].replace_etag,
1726 tests[i].replace_make_backup,
1727 tests[i].replace_flags,
1728 NULL,
1729 &local_error);
1730
1731 if (tests[i].expected_success)
1732 {
1733 g_assert_no_error (local_error);
1734 if (read_write)
1735 g_assert_nonnull (io_stream);
1736 else
1737 g_assert_nonnull (output_stream);
1738 }
1739
1740 /* Write new content to it. */
1741 if (io_stream != NULL)
1742 {
1743 GOutputStream *io_output_stream = g_io_stream_get_output_stream (G_IO_STREAM (io_stream));
1744 gsize n_written;
1745
1746 g_output_stream_write_all (G_OUTPUT_STREAM (io_output_stream), new_contents, strlen (new_contents),
1747 &n_written, NULL, &local_error);
1748
1749 if (tests[i].expected_success)
1750 {
1751 g_assert_no_error (local_error);
1752 g_assert_cmpint (n_written, ==, strlen (new_contents));
1753 }
1754
1755 g_io_stream_close (G_IO_STREAM (io_stream), NULL, (local_error == NULL) ? &local_error : NULL);
1756
1757 if (tests[i].expected_success)
1758 g_assert_no_error (local_error);
1759 }
1760 else if (output_stream != NULL)
1761 {
1762 gsize n_written;
1763
1764 g_output_stream_write_all (G_OUTPUT_STREAM (output_stream), new_contents, strlen (new_contents),
1765 &n_written, NULL, &local_error);
1766
1767 if (tests[i].expected_success)
1768 {
1769 g_assert_no_error (local_error);
1770 g_assert_cmpint (n_written, ==, strlen (new_contents));
1771 }
1772
1773 g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, (local_error == NULL) ? &local_error : NULL);
1774
1775 if (tests[i].expected_success)
1776 g_assert_no_error (local_error);
1777 }
1778
1779 if (tests[i].expected_success)
1780 g_assert_no_error (local_error);
1781 else
1782 g_assert_error (local_error, tests[i].expected_error_domain, tests[i].expected_error_code);
1783
1784 g_clear_error (&local_error);
1785 g_clear_object (&io_stream);
1786 g_clear_object (&output_stream);
1787
1788 /* Verify the final state of the directory. */
1789 enumerator = g_file_enumerate_children (tmpdir,
1790 G_FILE_ATTRIBUTE_STANDARD_NAME,
1791 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
1792 g_assert_no_error (local_error);
1793
1794 n_files = 0;
1795 do
1796 {
1797 g_file_enumerator_iterate (enumerator, &info, NULL, NULL, &local_error);
1798 g_assert_no_error (local_error);
1799
1800 if (info != NULL)
1801 n_files++;
1802 }
1803 while (info != NULL);
1804
1805 g_clear_object (&enumerator);
1806
1807 g_assert_cmpuint (n_files, ==, tests[i].expected_n_files);
1808
1809 check_test_file (source_file, tmpdir, "source", tests[i].expected_source_type, tests[i].expected_source_mode, tests[i].expected_source_contents);
1810 check_test_file (backup_file, tmpdir, "source~", tests[i].expected_backup_type, tests[i].expected_backup_mode, tests[i].expected_backup_contents);
1811
1812 /* Tidy up. Ignore failure apart from when deleting the directory, which
1813 * should be empty. */
1814 g_file_delete (source_file, NULL, NULL);
1815 g_file_delete (backup_file, NULL, NULL);
1816
1817 /* Other files which are occasionally generated by the tests. */
1818 {
1819 GFile *backup_target_file = g_file_get_child (tmpdir, "source~-target");
1820 g_file_delete (backup_target_file, NULL, NULL);
1821 g_clear_object (&backup_target_file);
1822 }
1823 {
1824 GFile *backup_target_file = g_file_get_child (tmpdir, "source-target");
1825 g_file_delete (backup_target_file, NULL, NULL);
1826 g_clear_object (&backup_target_file);
1827 }
1828
1829 g_file_delete (tmpdir, NULL, &local_error);
1830 g_assert_no_error (local_error);
1831
1832 g_clear_object (&backup_file);
1833 g_clear_object (&source_file);
1834 g_clear_object (&tmpdir);
1835 }
1836 #else /* if !__linux__ */
1837 g_test_skip ("File replacement tests can only be run on Linux");
1838 #endif
1839 }
1840
1841 static void
on_file_deleted(GObject * object,GAsyncResult * result,gpointer user_data)1842 on_file_deleted (GObject *object,
1843 GAsyncResult *result,
1844 gpointer user_data)
1845 {
1846 GError *local_error = NULL;
1847 GMainLoop *loop = user_data;
1848
1849 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
1850 g_assert_no_error (local_error);
1851
1852 g_main_loop_quit (loop);
1853 }
1854
1855 static void
test_async_delete(void)1856 test_async_delete (void)
1857 {
1858 GFile *file;
1859 GFileIOStream *iostream;
1860 GError *local_error = NULL;
1861 GError **error = &local_error;
1862 GMainLoop *loop;
1863
1864 file = g_file_new_tmp ("g_file_delete_XXXXXX",
1865 &iostream, error);
1866 g_assert_no_error (local_error);
1867 g_object_unref (iostream);
1868
1869 g_assert_true (g_file_query_exists (file, NULL));
1870
1871 loop = g_main_loop_new (NULL, TRUE);
1872
1873 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
1874
1875 g_main_loop_run (loop);
1876
1877 g_assert_false (g_file_query_exists (file, NULL));
1878
1879 g_main_loop_unref (loop);
1880 g_object_unref (file);
1881 }
1882
1883 static void
test_copy_preserve_mode(void)1884 test_copy_preserve_mode (void)
1885 {
1886 #ifdef G_OS_UNIX
1887 mode_t current_umask = umask (0);
1888 const struct
1889 {
1890 guint32 source_mode;
1891 guint32 expected_destination_mode;
1892 gboolean create_destination_before_copy;
1893 GFileCopyFlags copy_flags;
1894 }
1895 vectors[] =
1896 {
1897 /* Overwriting the destination file should copy the permissions from the
1898 * source file, even if %G_FILE_COPY_ALL_METADATA is set: */
1899 { 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
1900 { 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
1901 /* The same behaviour should hold if the destination file is not being
1902 * overwritten because it doesn’t already exist: */
1903 { 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
1904 { 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS },
1905 /* Anything with %G_FILE_COPY_TARGET_DEFAULT_PERMS should use the current
1906 * umask for the destination file: */
1907 { 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
1908 { 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
1909 { 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
1910 { 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS },
1911 };
1912 gsize i;
1913
1914 /* Reset the umask after querying it above. There’s no way to query it without
1915 * changing it. */
1916 umask (current_umask);
1917 g_test_message ("Current umask: %u", current_umask);
1918
1919 for (i = 0; i < G_N_ELEMENTS (vectors); i++)
1920 {
1921 GFile *tmpfile;
1922 GFile *dest_tmpfile;
1923 GFileInfo *dest_info;
1924 GFileIOStream *iostream;
1925 GError *local_error = NULL;
1926 guint32 romode = vectors[i].source_mode;
1927 guint32 dest_mode;
1928
1929 g_test_message ("Vector %" G_GSIZE_FORMAT, i);
1930
1931 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
1932 &iostream, &local_error);
1933 g_assert_no_error (local_error);
1934 g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
1935 g_assert_no_error (local_error);
1936 g_clear_object (&iostream);
1937
1938 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
1939 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1940 NULL, &local_error);
1941 g_assert_no_error (local_error);
1942
1943 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
1944 &iostream, &local_error);
1945 g_assert_no_error (local_error);
1946 g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
1947 g_assert_no_error (local_error);
1948 g_clear_object (&iostream);
1949
1950 if (!vectors[i].create_destination_before_copy)
1951 {
1952 g_file_delete (dest_tmpfile, NULL, &local_error);
1953 g_assert_no_error (local_error);
1954 }
1955
1956 g_file_copy (tmpfile, dest_tmpfile, vectors[i].copy_flags,
1957 NULL, NULL, NULL, &local_error);
1958 g_assert_no_error (local_error);
1959
1960 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1961 NULL, &local_error);
1962 g_assert_no_error (local_error);
1963
1964 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
1965
1966 g_assert_cmpint (dest_mode & ~S_IFMT, ==, vectors[i].expected_destination_mode);
1967 g_assert_cmpint (dest_mode & S_IFMT, ==, S_IFREG);
1968
1969 (void) g_file_delete (tmpfile, NULL, NULL);
1970 (void) g_file_delete (dest_tmpfile, NULL, NULL);
1971
1972 g_clear_object (&tmpfile);
1973 g_clear_object (&dest_tmpfile);
1974 g_clear_object (&dest_info);
1975 }
1976 #else /* if !G_OS_UNIX */
1977 g_test_skip ("File permissions tests can only be run on Unix")
1978 #endif
1979 }
1980
1981 static gchar *
splice_to_string(GInputStream * stream,GError ** error)1982 splice_to_string (GInputStream *stream,
1983 GError **error)
1984 {
1985 GMemoryOutputStream *buffer = NULL;
1986 char *ret = NULL;
1987
1988 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
1989 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
1990 goto out;
1991
1992 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
1993 goto out;
1994
1995 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
1996 goto out;
1997
1998 ret = g_memory_output_stream_steal_data (buffer);
1999 out:
2000 g_clear_object (&buffer);
2001 return ret;
2002 }
2003
2004 static gboolean
get_size_from_du(const gchar * path,guint64 * size)2005 get_size_from_du (const gchar *path, guint64 *size)
2006 {
2007 GSubprocess *du;
2008 gboolean ok;
2009 gchar *result;
2010 gchar *endptr;
2011 GError *error = NULL;
2012 gchar *du_path = NULL;
2013
2014 /* If we can’t find du, don’t try and run the test. */
2015 du_path = g_find_program_in_path ("du");
2016 if (du_path == NULL)
2017 return FALSE;
2018 g_free (du_path);
2019
2020 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
2021 &error,
2022 "du", "--bytes", "-s", path, NULL);
2023 g_assert_no_error (error);
2024
2025 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
2026 g_assert_no_error (error);
2027
2028 *size = g_ascii_strtoll (result, &endptr, 10);
2029
2030 g_subprocess_wait (du, NULL, &error);
2031 g_assert_no_error (error);
2032
2033 ok = g_subprocess_get_successful (du);
2034
2035 g_object_unref (du);
2036 g_free (result);
2037
2038 return ok;
2039 }
2040
2041 static void
test_measure(void)2042 test_measure (void)
2043 {
2044 GFile *file;
2045 guint64 size;
2046 guint64 num_bytes;
2047 guint64 num_dirs;
2048 guint64 num_files;
2049 GError *error = NULL;
2050 gboolean ok;
2051 gchar *path;
2052
2053 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
2054 file = g_file_new_for_path (path);
2055
2056 if (!get_size_from_du (path, &size))
2057 {
2058 g_test_message ("du not found or fail to run, skipping byte measurement");
2059 size = 0;
2060 }
2061
2062 ok = g_file_measure_disk_usage (file,
2063 G_FILE_MEASURE_APPARENT_SIZE,
2064 NULL,
2065 NULL,
2066 NULL,
2067 &num_bytes,
2068 &num_dirs,
2069 &num_files,
2070 &error);
2071 g_assert_true (ok);
2072 g_assert_no_error (error);
2073
2074 if (size > 0)
2075 g_assert_cmpuint (num_bytes, ==, size);
2076 g_assert_cmpuint (num_dirs, ==, 6);
2077 g_assert_cmpuint (num_files, ==, 32);
2078
2079 g_object_unref (file);
2080 g_free (path);
2081 }
2082
2083 typedef struct {
2084 guint64 expected_bytes;
2085 guint64 expected_dirs;
2086 guint64 expected_files;
2087 gint progress_count;
2088 guint64 progress_bytes;
2089 guint64 progress_dirs;
2090 guint64 progress_files;
2091 } MeasureData;
2092
2093 static void
measure_progress(gboolean reporting,guint64 current_size,guint64 num_dirs,guint64 num_files,gpointer user_data)2094 measure_progress (gboolean reporting,
2095 guint64 current_size,
2096 guint64 num_dirs,
2097 guint64 num_files,
2098 gpointer user_data)
2099 {
2100 MeasureData *data = user_data;
2101
2102 data->progress_count += 1;
2103
2104 g_assert_cmpuint (current_size, >=, data->progress_bytes);
2105 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
2106 g_assert_cmpuint (num_files, >=, data->progress_files);
2107
2108 data->progress_bytes = current_size;
2109 data->progress_dirs = num_dirs;
2110 data->progress_files = num_files;
2111 }
2112
2113 static void
measure_done(GObject * source,GAsyncResult * res,gpointer user_data)2114 measure_done (GObject *source,
2115 GAsyncResult *res,
2116 gpointer user_data)
2117 {
2118 MeasureData *data = user_data;
2119 guint64 num_bytes, num_dirs, num_files;
2120 GError *error = NULL;
2121 gboolean ok;
2122
2123 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
2124 g_assert_true (ok);
2125 g_assert_no_error (error);
2126
2127 if (data->expected_bytes > 0)
2128 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
2129 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
2130 g_assert_cmpuint (data->expected_files, ==, num_files);
2131
2132 g_assert_cmpuint (data->progress_count, >, 0);
2133 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
2134 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
2135 g_assert_cmpuint (num_files, >=, data->progress_files);
2136
2137 g_free (data);
2138 g_object_unref (source);
2139 }
2140
2141 static void
test_measure_async(void)2142 test_measure_async (void)
2143 {
2144 gchar *path;
2145 GFile *file;
2146 MeasureData *data;
2147
2148 data = g_new (MeasureData, 1);
2149
2150 data->progress_count = 0;
2151 data->progress_bytes = 0;
2152 data->progress_files = 0;
2153 data->progress_dirs = 0;
2154
2155 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
2156 file = g_file_new_for_path (path);
2157
2158 if (!get_size_from_du (path, &data->expected_bytes))
2159 {
2160 g_test_message ("du not found or fail to run, skipping byte measurement");
2161 data->expected_bytes = 0;
2162 }
2163
2164 g_free (path);
2165
2166 data->expected_dirs = 6;
2167 data->expected_files = 32;
2168
2169 g_file_measure_disk_usage_async (file,
2170 G_FILE_MEASURE_APPARENT_SIZE,
2171 0, NULL,
2172 measure_progress, data,
2173 measure_done, data);
2174 }
2175
2176 static void
test_load_bytes(void)2177 test_load_bytes (void)
2178 {
2179 gchar filename[] = "g_file_load_bytes_XXXXXX";
2180 GError *error = NULL;
2181 GBytes *bytes;
2182 GFile *file;
2183 int len;
2184 int fd;
2185 int ret;
2186
2187 fd = g_mkstemp (filename);
2188 g_assert_cmpint (fd, !=, -1);
2189 len = strlen ("test_load_bytes");
2190 ret = write (fd, "test_load_bytes", len);
2191 g_assert_cmpint (ret, ==, len);
2192 close (fd);
2193
2194 file = g_file_new_for_path (filename);
2195 bytes = g_file_load_bytes (file, NULL, NULL, &error);
2196 g_assert_no_error (error);
2197 g_assert_nonnull (bytes);
2198 g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
2199 g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
2200
2201 g_file_delete (file, NULL, NULL);
2202
2203 g_bytes_unref (bytes);
2204 g_object_unref (file);
2205 }
2206
2207 typedef struct
2208 {
2209 GMainLoop *main_loop;
2210 GFile *file;
2211 GBytes *bytes;
2212 } LoadBytesAsyncData;
2213
2214 static void
test_load_bytes_cb(GObject * object,GAsyncResult * result,gpointer user_data)2215 test_load_bytes_cb (GObject *object,
2216 GAsyncResult *result,
2217 gpointer user_data)
2218 {
2219 GFile *file = G_FILE (object);
2220 LoadBytesAsyncData *data = user_data;
2221 GError *error = NULL;
2222
2223 data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
2224 g_assert_no_error (error);
2225 g_assert_nonnull (data->bytes);
2226
2227 g_main_loop_quit (data->main_loop);
2228 }
2229
2230 static void
test_load_bytes_async(void)2231 test_load_bytes_async (void)
2232 {
2233 LoadBytesAsyncData data = { 0 };
2234 gchar filename[] = "g_file_load_bytes_XXXXXX";
2235 int len;
2236 int fd;
2237 int ret;
2238
2239 fd = g_mkstemp (filename);
2240 g_assert_cmpint (fd, !=, -1);
2241 len = strlen ("test_load_bytes_async");
2242 ret = write (fd, "test_load_bytes_async", len);
2243 g_assert_cmpint (ret, ==, len);
2244 close (fd);
2245
2246 data.main_loop = g_main_loop_new (NULL, FALSE);
2247 data.file = g_file_new_for_path (filename);
2248
2249 g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
2250 g_main_loop_run (data.main_loop);
2251
2252 g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
2253 g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
2254
2255 g_file_delete (data.file, NULL, NULL);
2256 g_object_unref (data.file);
2257 g_bytes_unref (data.bytes);
2258 g_main_loop_unref (data.main_loop);
2259 }
2260
2261 static void
test_writev_helper(GOutputVector * vectors,gsize n_vectors,gboolean use_bytes_written,const guint8 * expected_contents,gsize expected_length)2262 test_writev_helper (GOutputVector *vectors,
2263 gsize n_vectors,
2264 gboolean use_bytes_written,
2265 const guint8 *expected_contents,
2266 gsize expected_length)
2267 {
2268 GFile *file;
2269 GFileIOStream *iostream = NULL;
2270 GOutputStream *ostream;
2271 GError *error = NULL;
2272 gsize bytes_written = 0;
2273 gboolean res;
2274 guint8 *contents;
2275 gsize length;
2276
2277 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2278 &iostream, NULL);
2279 g_assert_nonnull (file);
2280 g_assert_nonnull (iostream);
2281
2282 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2283
2284 res = g_output_stream_writev_all (ostream, vectors, n_vectors, use_bytes_written ? &bytes_written : NULL, NULL, &error);
2285 g_assert_no_error (error);
2286 g_assert_true (res);
2287 if (use_bytes_written)
2288 g_assert_cmpuint (bytes_written, ==, expected_length);
2289
2290 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2291 g_assert_no_error (error);
2292 g_assert_true (res);
2293 g_object_unref (iostream);
2294
2295 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2296 g_assert_no_error (error);
2297 g_assert_true (res);
2298
2299 g_assert_cmpmem (contents, length, expected_contents, expected_length);
2300
2301 g_free (contents);
2302
2303 g_file_delete (file, NULL, NULL);
2304 g_object_unref (file);
2305 }
2306
2307 /* Test that writev() on local file output streams works on a non-empty vector */
2308 static void
test_writev(void)2309 test_writev (void)
2310 {
2311 GOutputVector vectors[3];
2312 const guint8 buffer[] = {1, 2, 3, 4, 5,
2313 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2314 1, 2, 3};
2315
2316 vectors[0].buffer = buffer;
2317 vectors[0].size = 5;
2318
2319 vectors[1].buffer = buffer + 5;
2320 vectors[1].size = 12;
2321
2322 vectors[2].buffer = buffer + 5 + 12;
2323 vectors[2].size = 3;
2324
2325 test_writev_helper (vectors, G_N_ELEMENTS (vectors), TRUE, buffer, sizeof buffer);
2326 }
2327
2328 /* Test that writev() on local file output streams works on a non-empty vector without returning bytes_written */
2329 static void
test_writev_no_bytes_written(void)2330 test_writev_no_bytes_written (void)
2331 {
2332 GOutputVector vectors[3];
2333 const guint8 buffer[] = {1, 2, 3, 4, 5,
2334 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2335 1, 2, 3};
2336
2337 vectors[0].buffer = buffer;
2338 vectors[0].size = 5;
2339
2340 vectors[1].buffer = buffer + 5;
2341 vectors[1].size = 12;
2342
2343 vectors[2].buffer = buffer + 5 + 12;
2344 vectors[2].size = 3;
2345
2346 test_writev_helper (vectors, G_N_ELEMENTS (vectors), FALSE, buffer, sizeof buffer);
2347 }
2348
2349 /* Test that writev() on local file output streams works on 0 vectors */
2350 static void
test_writev_no_vectors(void)2351 test_writev_no_vectors (void)
2352 {
2353 test_writev_helper (NULL, 0, TRUE, NULL, 0);
2354 }
2355
2356 /* Test that writev() on local file output streams works on empty vectors */
2357 static void
test_writev_empty_vectors(void)2358 test_writev_empty_vectors (void)
2359 {
2360 GOutputVector vectors[3];
2361
2362 vectors[0].buffer = NULL;
2363 vectors[0].size = 0;
2364 vectors[1].buffer = NULL;
2365 vectors[1].size = 0;
2366 vectors[2].buffer = NULL;
2367 vectors[2].size = 0;
2368
2369 test_writev_helper (vectors, G_N_ELEMENTS (vectors), TRUE, NULL, 0);
2370 }
2371
2372 /* Test that writev() fails if the sum of sizes in the vector is too big */
2373 static void
test_writev_too_big_vectors(void)2374 test_writev_too_big_vectors (void)
2375 {
2376 GFile *file;
2377 GFileIOStream *iostream = NULL;
2378 GOutputStream *ostream;
2379 GError *error = NULL;
2380 gsize bytes_written = 0;
2381 gboolean res;
2382 guint8 *contents;
2383 gsize length;
2384 GOutputVector vectors[3];
2385
2386 vectors[0].buffer = (void*) 1;
2387 vectors[0].size = G_MAXSIZE / 2;
2388
2389 vectors[1].buffer = (void*) 1;
2390 vectors[1].size = G_MAXSIZE / 2;
2391
2392 vectors[2].buffer = (void*) 1;
2393 vectors[2].size = G_MAXSIZE / 2;
2394
2395 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2396 &iostream, NULL);
2397 g_assert_nonnull (file);
2398 g_assert_nonnull (iostream);
2399
2400 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2401
2402 res = g_output_stream_writev_all (ostream, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
2403 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
2404 g_assert_cmpuint (bytes_written, ==, 0);
2405 g_assert_false (res);
2406 g_clear_error (&error);
2407
2408 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2409 g_assert_no_error (error);
2410 g_assert_true (res);
2411 g_object_unref (iostream);
2412
2413 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2414 g_assert_no_error (error);
2415 g_assert_true (res);
2416
2417 g_assert_cmpmem (contents, length, NULL, 0);
2418
2419 g_free (contents);
2420
2421 g_file_delete (file, NULL, NULL);
2422 g_object_unref (file);
2423 }
2424
2425 typedef struct
2426 {
2427 gsize bytes_written;
2428 GOutputVector *vectors;
2429 gsize n_vectors;
2430 GError *error;
2431 gboolean done;
2432 } WritevAsyncData;
2433
2434 static void
test_writev_async_cb(GObject * object,GAsyncResult * result,gpointer user_data)2435 test_writev_async_cb (GObject *object,
2436 GAsyncResult *result,
2437 gpointer user_data)
2438 {
2439 GOutputStream *ostream = G_OUTPUT_STREAM (object);
2440 WritevAsyncData *data = user_data;
2441 GError *error = NULL;
2442 gsize bytes_written;
2443 gboolean res;
2444
2445 res = g_output_stream_writev_finish (ostream, result, &bytes_written, &error);
2446 g_assert_true (res);
2447 g_assert_no_error (error);
2448 data->bytes_written += bytes_written;
2449
2450 /* skip vectors that have been written in full */
2451 while (data->n_vectors > 0 && bytes_written >= data->vectors[0].size)
2452 {
2453 bytes_written -= data->vectors[0].size;
2454 ++data->vectors;
2455 --data->n_vectors;
2456 }
2457 /* skip partially written vector data */
2458 if (bytes_written > 0 && data->n_vectors > 0)
2459 {
2460 data->vectors[0].size -= bytes_written;
2461 data->vectors[0].buffer = ((guint8 *) data->vectors[0].buffer) + bytes_written;
2462 }
2463
2464 if (data->n_vectors > 0)
2465 g_output_stream_writev_async (ostream, data->vectors, data->n_vectors, 0, NULL, test_writev_async_cb, &data);
2466 }
2467
2468 /* Test that writev_async() on local file output streams works on a non-empty vector */
2469 static void
test_writev_async(void)2470 test_writev_async (void)
2471 {
2472 WritevAsyncData data = { 0 };
2473 GFile *file;
2474 GFileIOStream *iostream = NULL;
2475 GOutputVector vectors[3];
2476 const guint8 buffer[] = {1, 2, 3, 4, 5,
2477 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2478 1, 2, 3};
2479 GOutputStream *ostream;
2480 GError *error = NULL;
2481 gboolean res;
2482 guint8 *contents;
2483 gsize length;
2484
2485 vectors[0].buffer = buffer;
2486 vectors[0].size = 5;
2487
2488 vectors[1].buffer = buffer + 5;
2489 vectors[1].size = 12;
2490
2491 vectors[2].buffer = buffer + 5 + 12;
2492 vectors[2].size = 3;
2493
2494 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2495 &iostream, NULL);
2496 g_assert_nonnull (file);
2497 g_assert_nonnull (iostream);
2498
2499 data.vectors = vectors;
2500 data.n_vectors = G_N_ELEMENTS (vectors);
2501
2502 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2503
2504 g_output_stream_writev_async (ostream, data.vectors, data.n_vectors, 0, NULL, test_writev_async_cb, &data);
2505
2506 while (data.n_vectors > 0)
2507 g_main_context_iteration (NULL, TRUE);
2508
2509 g_assert_cmpuint (data.bytes_written, ==, sizeof buffer);
2510
2511 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2512 g_assert_no_error (error);
2513 g_assert_true (res);
2514 g_object_unref (iostream);
2515
2516 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2517 g_assert_no_error (error);
2518 g_assert_true (res);
2519
2520 g_assert_cmpmem (contents, length, buffer, sizeof buffer);
2521
2522 g_free (contents);
2523
2524 g_file_delete (file, NULL, NULL);
2525 g_object_unref (file);
2526 }
2527
2528 static void
test_writev_all_cb(GObject * object,GAsyncResult * result,gpointer user_data)2529 test_writev_all_cb (GObject *object,
2530 GAsyncResult *result,
2531 gpointer user_data)
2532 {
2533 GOutputStream *ostream = G_OUTPUT_STREAM (object);
2534 WritevAsyncData *data = user_data;
2535
2536 g_output_stream_writev_all_finish (ostream, result, &data->bytes_written, &data->error);
2537 data->done = TRUE;
2538 }
2539
2540 /* Test that writev_async_all() on local file output streams works on a non-empty vector */
2541 static void
test_writev_async_all(void)2542 test_writev_async_all (void)
2543 {
2544 WritevAsyncData data = { 0 };
2545 GFile *file;
2546 GFileIOStream *iostream = NULL;
2547 GOutputStream *ostream;
2548 GOutputVector vectors[3];
2549 const guint8 buffer[] = {1, 2, 3, 4, 5,
2550 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2551 1, 2, 3};
2552 GError *error = NULL;
2553 gboolean res;
2554 guint8 *contents;
2555 gsize length;
2556
2557 vectors[0].buffer = buffer;
2558 vectors[0].size = 5;
2559
2560 vectors[1].buffer = buffer + 5;
2561 vectors[1].size = 12;
2562
2563 vectors[2].buffer = buffer + 5 + 12;
2564 vectors[2].size = 3;
2565
2566 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2567 &iostream, NULL);
2568 g_assert_nonnull (file);
2569 g_assert_nonnull (iostream);
2570
2571 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2572
2573 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
2574
2575 while (!data.done)
2576 g_main_context_iteration (NULL, TRUE);
2577
2578 g_assert_cmpuint (data.bytes_written, ==, sizeof buffer);
2579 g_assert_no_error (data.error);
2580
2581 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2582 g_assert_no_error (error);
2583 g_assert_true (res);
2584 g_object_unref (iostream);
2585
2586 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2587 g_assert_no_error (error);
2588 g_assert_true (res);
2589
2590 g_assert_cmpmem (contents, length, buffer, sizeof buffer);
2591
2592 g_free (contents);
2593
2594 g_file_delete (file, NULL, NULL);
2595 g_object_unref (file);
2596 }
2597
2598 /* Test that writev_async_all() on local file output streams handles cancellation correctly */
2599 static void
test_writev_async_all_cancellation(void)2600 test_writev_async_all_cancellation (void)
2601 {
2602 WritevAsyncData data = { 0 };
2603 GFile *file;
2604 GFileIOStream *iostream = NULL;
2605 GOutputVector vectors[3];
2606 const guint8 buffer[] = {1, 2, 3, 4, 5,
2607 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2608 1, 2, 3};
2609 GOutputStream *ostream;
2610 GError *error = NULL;
2611 gboolean res;
2612 guint8 *contents;
2613 gsize length;
2614 GCancellable *cancellable;
2615
2616 vectors[0].buffer = buffer;
2617 vectors[0].size = 5;
2618
2619 vectors[1].buffer = buffer + 5;
2620 vectors[1].size = 12;
2621
2622 vectors[2].buffer = buffer + 5 + 12;
2623 vectors[2].size = 3;
2624
2625 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2626 &iostream, NULL);
2627 g_assert_nonnull (file);
2628 g_assert_nonnull (iostream);
2629
2630 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2631
2632 cancellable = g_cancellable_new ();
2633 g_cancellable_cancel (cancellable);
2634
2635 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, cancellable, test_writev_all_cb, &data);
2636
2637 while (!data.done)
2638 g_main_context_iteration (NULL, TRUE);
2639
2640 g_assert_cmpuint (data.bytes_written, ==, 0);
2641 g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
2642 g_clear_error (&data.error);
2643
2644 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2645 g_assert_no_error (error);
2646 g_assert_true (res);
2647 g_object_unref (iostream);
2648
2649 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2650 g_assert_no_error (error);
2651 g_assert_true (res);
2652 g_assert_cmpuint (length, ==, 0);
2653
2654 g_free (contents);
2655
2656 g_file_delete (file, NULL, NULL);
2657 g_object_unref (file);
2658 g_object_unref (cancellable);
2659 }
2660
2661 /* Test that writev_async_all() with empty vectors is handled correctly */
2662 static void
test_writev_async_all_empty_vectors(void)2663 test_writev_async_all_empty_vectors (void)
2664 {
2665 WritevAsyncData data = { 0 };
2666 GFile *file;
2667 GFileIOStream *iostream = NULL;
2668 GOutputVector vectors[3];
2669 GOutputStream *ostream;
2670 GError *error = NULL;
2671 gboolean res;
2672 guint8 *contents;
2673 gsize length;
2674
2675 vectors[0].buffer = NULL;
2676 vectors[0].size = 0;
2677
2678 vectors[1].buffer = NULL;
2679 vectors[1].size = 0;
2680
2681 vectors[2].buffer = NULL;
2682 vectors[2].size = 0;
2683
2684 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2685 &iostream, NULL);
2686 g_assert_nonnull (file);
2687 g_assert_nonnull (iostream);
2688
2689 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2690
2691 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
2692
2693 while (!data.done)
2694 g_main_context_iteration (NULL, TRUE);
2695
2696 g_assert_cmpuint (data.bytes_written, ==, 0);
2697 g_assert_no_error (data.error);
2698 g_clear_error (&data.error);
2699
2700 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2701 g_assert_no_error (error);
2702 g_assert_true (res);
2703 g_object_unref (iostream);
2704
2705 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2706 g_assert_no_error (error);
2707 g_assert_true (res);
2708 g_assert_cmpuint (length, ==, 0);
2709
2710 g_free (contents);
2711
2712 g_file_delete (file, NULL, NULL);
2713 g_object_unref (file);
2714 }
2715
2716 /* Test that writev_async_all() with no vectors is handled correctly */
2717 static void
test_writev_async_all_no_vectors(void)2718 test_writev_async_all_no_vectors (void)
2719 {
2720 WritevAsyncData data = { 0 };
2721 GFile *file;
2722 GFileIOStream *iostream = NULL;
2723 GOutputStream *ostream;
2724 GError *error = NULL;
2725 gboolean res;
2726 guint8 *contents;
2727 gsize length;
2728
2729 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2730 &iostream, NULL);
2731 g_assert_nonnull (file);
2732 g_assert_nonnull (iostream);
2733
2734 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2735
2736 g_output_stream_writev_all_async (ostream, NULL, 0, 0, NULL, test_writev_all_cb, &data);
2737
2738 while (!data.done)
2739 g_main_context_iteration (NULL, TRUE);
2740
2741 g_assert_cmpuint (data.bytes_written, ==, 0);
2742 g_assert_no_error (data.error);
2743 g_clear_error (&data.error);
2744
2745 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2746 g_assert_no_error (error);
2747 g_assert_true (res);
2748 g_object_unref (iostream);
2749
2750 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2751 g_assert_no_error (error);
2752 g_assert_true (res);
2753 g_assert_cmpuint (length, ==, 0);
2754
2755 g_free (contents);
2756
2757 g_file_delete (file, NULL, NULL);
2758 g_object_unref (file);
2759 }
2760
2761 /* Test that writev_async_all() with too big vectors is handled correctly */
2762 static void
test_writev_async_all_too_big_vectors(void)2763 test_writev_async_all_too_big_vectors (void)
2764 {
2765 WritevAsyncData data = { 0 };
2766 GFile *file;
2767 GFileIOStream *iostream = NULL;
2768 GOutputVector vectors[3];
2769 GOutputStream *ostream;
2770 GError *error = NULL;
2771 gboolean res;
2772 guint8 *contents;
2773 gsize length;
2774
2775 vectors[0].buffer = (void*) 1;
2776 vectors[0].size = G_MAXSIZE / 2;
2777
2778 vectors[1].buffer = (void*) 1;
2779 vectors[1].size = G_MAXSIZE / 2;
2780
2781 vectors[2].buffer = (void*) 1;
2782 vectors[2].size = G_MAXSIZE / 2;
2783
2784 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2785 &iostream, NULL);
2786 g_assert_nonnull (file);
2787 g_assert_nonnull (iostream);
2788
2789 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2790
2791 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
2792
2793 while (!data.done)
2794 g_main_context_iteration (NULL, TRUE);
2795
2796 g_assert_cmpuint (data.bytes_written, ==, 0);
2797 g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
2798 g_clear_error (&data.error);
2799
2800 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2801 g_assert_no_error (error);
2802 g_assert_true (res);
2803 g_object_unref (iostream);
2804
2805 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2806 g_assert_no_error (error);
2807 g_assert_true (res);
2808 g_assert_cmpuint (length, ==, 0);
2809
2810 g_free (contents);
2811
2812 g_file_delete (file, NULL, NULL);
2813 g_object_unref (file);
2814 }
2815
2816 static void
test_build_attribute_list_for_copy(void)2817 test_build_attribute_list_for_copy (void)
2818 {
2819 GFile *tmpfile;
2820 GFileIOStream *iostream;
2821 GError *error = NULL;
2822 const GFileCopyFlags test_flags[] =
2823 {
2824 G_FILE_COPY_NONE,
2825 G_FILE_COPY_TARGET_DEFAULT_PERMS,
2826 G_FILE_COPY_ALL_METADATA,
2827 G_FILE_COPY_ALL_METADATA | G_FILE_COPY_TARGET_DEFAULT_PERMS,
2828 };
2829 gsize i;
2830 char *attrs;
2831 gchar *attrs_with_commas;
2832
2833 tmpfile = g_file_new_tmp ("tmp-build-attribute-list-for-copyXXXXXX",
2834 &iostream, &error);
2835 g_assert_no_error (error);
2836 g_io_stream_close ((GIOStream*)iostream, NULL, &error);
2837 g_assert_no_error (error);
2838 g_clear_object (&iostream);
2839
2840 for (i = 0; i < G_N_ELEMENTS (test_flags); i++)
2841 {
2842 GFileCopyFlags flags = test_flags[i];
2843
2844 attrs = g_file_build_attribute_list_for_copy (tmpfile, flags, NULL, &error);
2845 g_test_message ("Attributes for copy: %s", attrs);
2846 g_assert_no_error (error);
2847 g_assert_nonnull (attrs);
2848 attrs_with_commas = g_strconcat (",", attrs, ",", NULL);
2849 g_free (attrs);
2850
2851 /* See g_local_file_class_init for reference. */
2852 if (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS)
2853 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_MODE ","));
2854 else
2855 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_MODE ","));
2856 #ifdef G_OS_UNIX
2857 if (flags & G_FILE_COPY_ALL_METADATA)
2858 {
2859 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_UID ","));
2860 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_GID ","));
2861 }
2862 else
2863 {
2864 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_UID ","));
2865 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_GID ","));
2866 }
2867 #endif
2868 #ifdef HAVE_UTIMES
2869 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_MODIFIED ","));
2870 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC ","));
2871 if (flags & G_FILE_COPY_ALL_METADATA)
2872 {
2873 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_ACCESS ","));
2874 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_ACCESS_USEC ","));
2875 }
2876 else
2877 {
2878 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_ACCESS ","));
2879 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_ACCESS_USEC ","));
2880 }
2881 #endif
2882 g_free (attrs_with_commas);
2883 }
2884
2885 (void) g_file_delete (tmpfile, NULL, NULL);
2886 g_clear_object (&tmpfile);
2887 }
2888
2889 int
main(int argc,char * argv[])2890 main (int argc, char *argv[])
2891 {
2892 g_test_init (&argc, &argv, NULL);
2893
2894 g_test_add_func ("/file/basic", test_basic);
2895 g_test_add_func ("/file/build-filename", test_build_filename);
2896 g_test_add_func ("/file/parent", test_parent);
2897 g_test_add_func ("/file/child", test_child);
2898 g_test_add_func ("/file/empty-path", test_empty_path);
2899 g_test_add_func ("/file/type", test_type);
2900 g_test_add_func ("/file/parse-name", test_parse_name);
2901 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
2902 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
2903 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
2904 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
2905 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
2906 g_test_add_func ("/file/replace-load", test_replace_load);
2907 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
2908 g_test_add_func ("/file/replace-symlink", test_replace_symlink);
2909 g_test_add_data_func ("/file/replace/write-only", GUINT_TO_POINTER (FALSE), test_replace);
2910 g_test_add_data_func ("/file/replace/read-write", GUINT_TO_POINTER (TRUE), test_replace);
2911 g_test_add_func ("/file/async-delete", test_async_delete);
2912 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
2913 g_test_add_func ("/file/measure", test_measure);
2914 g_test_add_func ("/file/measure-async", test_measure_async);
2915 g_test_add_func ("/file/load-bytes", test_load_bytes);
2916 g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
2917 g_test_add_func ("/file/writev", test_writev);
2918 g_test_add_func ("/file/writev/no-bytes-written", test_writev_no_bytes_written);
2919 g_test_add_func ("/file/writev/no-vectors", test_writev_no_vectors);
2920 g_test_add_func ("/file/writev/empty-vectors", test_writev_empty_vectors);
2921 g_test_add_func ("/file/writev/too-big-vectors", test_writev_too_big_vectors);
2922 g_test_add_func ("/file/writev/async", test_writev_async);
2923 g_test_add_func ("/file/writev/async_all", test_writev_async_all);
2924 g_test_add_func ("/file/writev/async_all-empty-vectors", test_writev_async_all_empty_vectors);
2925 g_test_add_func ("/file/writev/async_all-no-vectors", test_writev_async_all_no_vectors);
2926 g_test_add_func ("/file/writev/async_all-to-big-vectors", test_writev_async_all_too_big_vectors);
2927 g_test_add_func ("/file/writev/async_all-cancellation", test_writev_async_all_cancellation);
2928 g_test_add_func ("/file/build-attribute-list-for-copy", test_build_attribute_list_for_copy);
2929
2930 return g_test_run ();
2931 }
2932