📄 live-g-file.c
字号:
root = g_file_new_for_commandline_arg ((char *) test_data); g_assert (root != NULL); res = g_file_query_exists (root, NULL); g_assert_cmpint (res, ==, TRUE); for (i = 0; i < G_N_ELEMENTS (sample_struct); i++) { item = sample_struct[i]; if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) || ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) || ((item.extra_flags & TEST_APPEND) == TEST_APPEND)) { log (" test_create: '%s'\n", item.filename); child = g_file_get_child (root, item.filename); g_assert (child != NULL); error = NULL; os = NULL; if ((item.extra_flags & TEST_CREATE) == TEST_CREATE) os = g_file_create (child, item.create_flags, NULL, &error); else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) os = g_file_replace (child, NULL, TRUE, item.create_flags, NULL, &error); else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND) os = g_file_append_to (child, item.create_flags, NULL, &error); if (error) log (" error code %d = %s\n", error->code, error->message); if (((item.extra_flags & TEST_NOT_EXISTS) == 0) && ((item.extra_flags & TEST_CREATE) == TEST_CREATE)) { g_assert (os == NULL); g_assert (error != NULL); g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS); } else if (item.file_type == G_FILE_TYPE_DIRECTORY) { g_assert (os == NULL); g_assert (error != NULL); if ((item.extra_flags & TEST_CREATE) == TEST_CREATE) g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS); else g_assert_cmpint (error->code, ==, G_IO_ERROR_IS_DIRECTORY); } else { g_assert (os != NULL); g_assert (error == NULL); } if (error) g_error_free (error); if (os) { error = NULL; res = g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error); if (error) log (" g_output_stream_close: error %d = %s\n", error->code, error->message); g_assert_cmpint (res, ==, TRUE); g_assert (error == NULL); } g_object_unref (child); } } g_object_unref (root);}static voidtest_open (gconstpointer test_data){ GFile *root, *child; gboolean res; GError *error; int i; struct StructureItem item; GFileInputStream *input_stream; g_assert (test_data != NULL); log ("\n"); root = g_file_new_for_commandline_arg ((char *) test_data); g_assert (root != NULL); res = g_file_query_exists (root, NULL); g_assert_cmpint (res, ==, TRUE); for (i = 0; i < G_N_ELEMENTS (sample_struct); i++) { item = sample_struct[i]; if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)) continue; if ((item.extra_flags & TEST_OPEN) == TEST_OPEN) { log (" test_open: '%s'\n", item.filename); child = g_file_get_child (root, item.filename); g_assert (child != NULL); error = NULL; input_stream = g_file_read (child, NULL, &error); if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) || ((item.extra_flags & TEST_INVALID_SYMLINK) == TEST_INVALID_SYMLINK)) { g_assert (input_stream == NULL); g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND); } else if (item.file_type == G_FILE_TYPE_DIRECTORY) { g_assert (input_stream == NULL); g_assert_cmpint (error->code, ==, G_IO_ERROR_IS_DIRECTORY); } else { g_assert (input_stream != NULL); g_assert (error == NULL); } if (error) g_error_free (error); if (input_stream) { error = NULL; res = g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, &error); g_assert_cmpint (res, ==, TRUE); g_assert (error == NULL); } g_object_unref (child); } } g_object_unref (root);}static voidtest_delete (gconstpointer test_data){ GFile *root; GFile *child; gboolean res; GError *error; int i; struct StructureItem item; g_assert (test_data != NULL); log ("\n"); root = g_file_new_for_commandline_arg ((char *) test_data); g_assert (root != NULL); res = g_file_query_exists (root, NULL); g_assert_cmpint (res, ==, TRUE); for (i = 0; i < G_N_ELEMENTS (sample_struct); i++) { item = sample_struct[i]; if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)) continue; if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) || ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH)) { child = file_exists (root, item.filename, &res); g_assert (child != NULL); /* we don't care about result here */ log (" Deleting %s, path = %s\n", item.filename, g_file_get_path (child)); error = NULL; if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) res = g_file_delete (child, NULL, &error); else res = g_file_trash (child, NULL, &error); if ((item.extra_flags & TEST_DELETE_NON_EMPTY) == TEST_DELETE_NON_EMPTY) { g_assert_cmpint (res, ==, FALSE); g_assert (error != NULL); g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_EMPTY); } if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE) { g_assert_cmpint (res, ==, FALSE); g_assert (error != NULL); g_assert_cmpint (error->code, !=, 0); } if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) { g_assert_cmpint (res, ==, FALSE); g_assert (error != NULL); g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND); } if (error) { log (" result = %d, error = %s\n", res, error->message); g_error_free (error); } g_object_unref (child); } } g_object_unref (root);}static voidcleanup_dir_recurse (GFile *parent, GFile *root){ gboolean res; GError *error; GFileEnumerator *enumerator; GFileInfo *info; GFile *descend; char *relative_path; g_assert (root != NULL); error = NULL; enumerator = g_file_enumerate_children (parent, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &error); if (! enumerator) return; error = NULL; info = g_file_enumerator_next_file (enumerator, NULL, &error); while ((info) && (!error)) { descend = g_file_get_child (parent, g_file_info_get_name (info)); g_assert (descend != NULL); relative_path = g_file_get_relative_path (root, descend); g_assert (relative_path != NULL); log (" deleting '%s'\n", g_file_info_get_display_name (info)); if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) cleanup_dir_recurse (descend, root); error = NULL; res = g_file_delete (descend, NULL, &error); g_assert_cmpint (res, ==, TRUE); g_object_unref (descend); error = NULL; info = g_file_enumerator_next_file (enumerator, NULL, &error); } g_assert (error == NULL); error = NULL; res = g_file_enumerator_close (enumerator, NULL, &error); g_assert_cmpint (res, ==, TRUE); g_assert (error == NULL);}static voidprep_clean_structure (gconstpointer test_data){ GFile *root; g_assert (test_data != NULL); log ("\n Cleaning target testing structure in '%s'...\n", (char *) test_data); root = g_file_new_for_commandline_arg ((char *) test_data); g_assert (root != NULL); cleanup_dir_recurse (root, root); g_file_delete (root, NULL, NULL); g_object_unref (root);}intmain (int argc, char *argv[]){ static gboolean only_create_struct; static char *target_path; GError *error; GOptionContext *context; static GOptionEntry cmd_entries[] = { {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test, "Perform write tests (incl. structure creation)", NULL}, {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct, "Only create testing structure (no tests)", NULL}, {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL}, {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat, "Test POSIX-specific features (unix permissions, symlinks)", NULL}, {NULL} }; verbose = FALSE; write_test = FALSE; only_create_struct = FALSE; target_path = NULL; posix_compat = FALSE; /* strip all gtester-specific args */ g_type_init (); g_test_init (&argc, &argv, NULL); /* no extra parameters specified, assume we're executed from glib test suite */ if (argc < 2) { verbose = TRUE; write_test = TRUE; only_create_struct = FALSE; target_path = DEFAULT_TEST_DIR;#ifdef G_PLATFORM_WIN32 posix_compat = FALSE;#else posix_compat = TRUE;#endif } /* add trailing args */ error = NULL; context = g_option_context_new ("target_path"); g_option_context_add_main_entries (context, cmd_entries, NULL); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_print ("option parsing failed: %s\n", error->message); return g_test_run (); } /* remaining arg should is the target path; we don't care of the extra args here */ if (argc >= 2) target_path = strdup (argv[1]); if (! target_path) { g_print ("error: target path was not specified\n"); g_print (g_option_context_get_help (context, TRUE, NULL)); return g_test_run (); } /* Write test - clean target directory first */ /* this can be also considered as a test - enumerate + delete */ if (write_test || only_create_struct) g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path, prep_clean_structure); /* Write test - create new testing structure */ if (write_test || only_create_struct) g_test_add_data_func ("/live-g-file/create_structure", target_path, test_create_structure); /* Read test - test the sample structure - expect defined attributes to be there */ if (!only_create_struct) g_test_add_data_func ("/live-g-file/test_initial_structure", target_path, test_initial_structure); /* Read test - test traverse the structure - no special file should appear */ if (!only_create_struct) g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path, test_traverse_structure); /* Read test - enumerate */ if (!only_create_struct) g_test_add_data_func ("/live-g-file/test_enumerate", target_path, test_enumerate); /* Read test - open (g_file_read()) */ if (!only_create_struct) g_test_add_data_func ("/live-g-file/test_open", target_path, test_open); /* Write test - create */ if (write_test && (!only_create_struct)) g_test_add_data_func ("/live-g-file/test_create", target_path, test_create); /* Write test - copy, move */ if (write_test && (!only_create_struct)) g_test_add_data_func ("/live-g-file/test_copy_move", target_path, test_copy_move); /* Write test - delete, trash */ if (write_test && (!only_create_struct)) g_test_add_data_func ("/live-g-file/test_delete", target_path, test_delete); if (write_test || only_create_struct) g_test_add_data_func ("/live-g-file/final_clean", target_path, prep_clean_structure); return g_test_run ();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -