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