Skip to content

Instantly share code, notes, and snippets.

@andreldm
Last active May 14, 2019 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreldm/007a07c2fd361e938cac42e1d5c04bd9 to your computer and use it in GitHub Desktop.
Save andreldm/007a07c2fd361e938cac42e1d5c04bd9 to your computer and use it in GitHub Desktop.
GIO file read/write info
/*
* Build:
* gcc $(pkg-config --cflags gtk+-3.0) fs_sample.c -o fs_sample $(pkg-config --libs gtk+-3.0)
*/
#include <gtk/gtk.h>
void check(const char *path)
{
GFile *file = g_file_new_for_path (path);
GFileInfo *filesystem_info = g_file_query_filesystem_info (file, "filesystem::*", NULL, NULL);
GFileInfo *file_info = g_file_query_info (file, "access::*", G_FILE_QUERY_INFO_NONE, NULL, NULL);
switch (g_file_query_file_type (file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL))
{
case G_FILE_TYPE_REGULAR:
g_print ("%s is a regular file\n", path);
break;
case G_FILE_TYPE_DIRECTORY:
g_print ("%s is a directory\n", path);
break;
case G_FILE_TYPE_SYMBOLIC_LINK:
g_print ("%s is a symlink\n", path);
break;
case G_FILE_TYPE_SPECIAL:
g_print ("%s is a special file\n", path);
break;
case G_FILE_TYPE_MOUNTABLE:
g_print ("%s is a mountable location\n", path);
break;
case G_FILE_TYPE_UNKNOWN:
default:
g_print ("Unable to detect file type of %s\n", path);
break;
}
gboolean isReadOnly = g_file_info_get_attribute_boolean (filesystem_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY);
g_print ("is %s on read only filesystem? %s\n", path, isReadOnly ? "TRUE" : "FALSE");
gboolean canWrite = g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
g_print ("can %s be written? %s\n", path, canWrite ? "TRUE" : "FALSE");
g_object_unref (filesystem_info);
g_object_unref (file_info);
g_object_unref (file);
}
int
main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
if (argc <= 1) {
g_print ("Please pass the path to the file to be checked\n");
return 1;
}
check (argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment