⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 live-g-file.c

📁 this is a glib for c language
💻 C
📖 第 1 页 / 共 3 页
字号:
  /*  test the structure  */  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))	  || (item.handle_special))	continue;      log ("    Testing file '%s'...\n", item.filename);      child = file_exists (root, item.filename, &res);      g_assert (child != NULL);      g_assert_cmpint (res, ==, TRUE);      error = NULL;      info =	g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,			   NULL, &error);      g_assert (error == NULL);      g_assert (info != NULL);      test_attributes (item, info);      g_object_unref (child);    }  /*  read and test the pattern file  */  log ("    Testing pattern file...\n");  child = file_exists (root, "pattern_file", &res);  g_assert (child != NULL);  g_assert_cmpint (res, ==, TRUE);  error = NULL;  info =    g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,		       &error);  g_assert (error == NULL);  g_assert (info != NULL);  size = g_file_info_get_size (info);  g_assert_cmpint (size, ==, PATTERN_FILE_SIZE);  error = NULL;  ins = g_file_read (child, NULL, &error);  g_assert (ins != NULL);  g_assert (error == NULL);  buffer = g_malloc (PATTERN_FILE_SIZE);  total_read = 0;  while (total_read < PATTERN_FILE_SIZE)    {      error = NULL;      read =	g_input_stream_read (G_INPUT_STREAM (ins), buffer + total_read,			     PATTERN_FILE_SIZE, NULL, &error);      g_assert (error == NULL);      total_read += read;      log ("      read %d bytes, total = %d of %d.\n", read, total_read,	   PATTERN_FILE_SIZE);    }  g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);  error = NULL;  res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);  g_assert (error == NULL);  g_assert_cmpint (res, ==, TRUE);  for (i = 0; i < PATTERN_FILE_SIZE; i++)    g_assert_cmpint (*(buffer + i), ==, i % 256);  g_object_unref (ins);  g_object_unref (child);  g_free (buffer);  g_object_unref (root);}static voidtraverse_recurse_dirs (GFile * parent, GFile * root){  gboolean res;  GError *error;  GFileEnumerator *enumerator;  GFileInfo *info;  GFile *descend;  char *relative_path;  int i;  gboolean found;  g_assert (root != NULL);  error = NULL;  enumerator =    g_file_enumerate_children (parent, "*",			       G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,			       &error);  g_assert (enumerator != NULL);  g_assert (error == NULL);  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);      found = FALSE;      for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)	{	  if (strcmp (sample_struct[i].filename, relative_path) == 0)	    {	      /*  test the attributes again  */	      test_attributes (sample_struct[i], info);	      found = TRUE;	      break;	    }	}      g_assert_cmpint (found, ==, TRUE);      log ("  Found file %s, relative to root: %s\n",	   g_file_info_get_display_name (info), relative_path);      if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)	traverse_recurse_dirs (descend, root);      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 voidtest_traverse_structure (gconstpointer test_data){  GFile *root;  gboolean res;  g_assert (test_data != NULL);  log ("\n  Traversing through the sample structure in '%s'...\n",       (char *) test_data);  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);  traverse_recurse_dirs (root, root);  g_object_unref (root);}static voidtest_enumerate (gconstpointer test_data){  GFile *root, *child;  gboolean res;  GError *error;  GFileEnumerator *enumerator;  GFileInfo *info;  int i;  struct StructureItem item;  g_assert (test_data != NULL);  log ("\n  Test enumerate '%s'...\n", (char *) test_data);  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_NOT_EXISTS) == TEST_NOT_EXISTS) ||	  (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)	   && posix_compat)	  || ((item.extra_flags & TEST_ENUMERATE_FILE) ==	      TEST_ENUMERATE_FILE))	{	  log ("    Testing file '%s'\n", item.filename);	  child = g_file_get_child (root, item.filename);	  g_assert (child != NULL);	  error = NULL;	  enumerator =	    g_file_enumerate_children (child, "*",				       G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,				       NULL, &error);	  if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)	    {	      g_assert (enumerator == NULL);	      g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);	    }	  if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)	    {	      g_assert (enumerator == NULL);	      g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_DIRECTORY);	    }	  if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)	    {	      g_assert (enumerator != NULL);	      error = NULL;	      info = g_file_enumerator_next_file (enumerator, NULL, &error);	      g_assert (info == NULL);	      g_assert (error == NULL);	      /*  no items should be found, no error should be logged  */	    }	  if (error)	    g_error_free (error);	  if (enumerator)	    {	      error = NULL;	      res = g_file_enumerator_close (enumerator, NULL, &error);	      g_assert_cmpint (res, ==, TRUE);	      g_assert (error == NULL);	    }	  g_object_unref (child);	}    }  g_object_unref (root);}static voiddo_copy_move (GFile * root, struct StructureItem item, const char *target_dir,	      enum StructureExtraFlags extra_flags){  GFile *dst_dir, *src_file, *dst_file;  gboolean res;  GError *error;  log ("    do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);  dst_dir = g_file_get_child (root, target_dir);  g_assert (dst_dir != NULL);  src_file = g_file_get_child (root, item.filename);  g_assert (src_file != NULL);  dst_file = g_file_get_child (dst_dir, item.filename);  g_assert (dst_file != NULL);  error = NULL;  if ((item.extra_flags & TEST_COPY) == TEST_COPY)    res =      g_file_copy (src_file, dst_file,		   G_FILE_COPY_NOFOLLOW_SYMLINKS |		   ((extra_flags ==		     TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :		    G_FILE_COPY_NONE), NULL, NULL, NULL, &error);  else    res =      g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,		   NULL, NULL, &error);  if (error)    log ("       res = %d, error code %d = %s\n", res, error->code,	 error->message);  /*  copying file/directory to itself (".")  */  if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&      (extra_flags == TEST_ALREADY_EXISTS))    {      g_assert_cmpint (res, ==, FALSE);      g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);    }  /*  target file is a file, overwrite is not set  */  else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&	   (extra_flags == TEST_TARGET_IS_FILE))    {      g_assert_cmpint (res, ==, FALSE);      if (item.file_type == G_FILE_TYPE_DIRECTORY)	g_assert_cmpint (error->code, ==, G_IO_ERROR_WOULD_RECURSE);      else	g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_DIRECTORY);    }  /*  source file is directory  */  else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==	   TEST_COPY_ERROR_RECURSE)    {      g_assert_cmpint (res, ==, FALSE);      g_assert_cmpint (error->code, ==, G_IO_ERROR_WOULD_RECURSE);    }  /*  source or target path doesn't exist  */  else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||	   (extra_flags == TEST_NOT_EXISTS))    {      g_assert_cmpint (res, ==, FALSE);      g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);    }  /*  source or target path permission denied  */  else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||	   (extra_flags == TEST_NO_ACCESS))    {      g_assert_cmpint (res, ==, FALSE);      g_assert_cmpint (error->code, ==, G_IO_ERROR_PERMISSION_DENIED);    }  /*  no error should be found, all exceptions defined above  */  else    {      g_assert_cmpint (res, ==, TRUE);      g_assert (error == NULL);    }  if (error)    g_error_free (error);  g_object_unref (dst_dir);  g_object_unref (src_file);  g_object_unref (dst_file);}static voidtest_copy_move (gconstpointer test_data){  GFile *root;  gboolean res;  int i;  struct StructureItem item;  log ("\n");  g_assert (test_data != NULL);  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_COPY) == TEST_COPY) ||	  ((item.extra_flags & TEST_MOVE) == TEST_MOVE))	{	  /*  test copy/move to a directory, expecting no errors if source files exist  */	  do_copy_move (root, item, TEST_DIR_TARGET, 0);	  /*  some files have been already moved so we can't count with them in the tests  */	  if ((item.extra_flags & TEST_COPY) == TEST_COPY)	    {	      /*  test overwrite for flagged files  */	      if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)		{		  do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);		}	      /*  source = target, should return G_IO_ERROR_EXISTS  */	      do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);	      /*  target is file  */	      do_copy_move (root, item, TEST_TARGET_FILE,			    TEST_TARGET_IS_FILE);	      /*  target path is invalid  */	      do_copy_move (root, item, TEST_NAME_NOT_EXISTS,			    TEST_NOT_EXISTS);	      /*  tests on POSIX-compatible filesystems  */	      if (posix_compat)		{		  /*  target directory is not accessible (no execute flag)  */		  do_copy_move (root, item, TEST_DIR_NO_ACCESS,				TEST_NO_ACCESS);		  /*  target directory is readonly  */		  do_copy_move (root, item, TEST_DIR_NO_WRITE,				TEST_NO_ACCESS);		}	    }	}    }  g_object_unref (root);}static voidtest_create (gconstpointer test_data){  GFile *root, *child;  gboolean res;  GError *error;  int i;  struct StructureItem item;  GFileOutputStream *os;  g_assert (test_data != NULL);  log ("\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -