summaryrefslogtreecommitdiff
path: root/programs/gvfs-info.c
blob: 3c1578c3d912a1d68c75a0fb72cccc7050d10237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <config.h>

#include <glib.h>
#include <locale.h>
#include <gio/gfile.h>

static char *attributes = NULL;
static gboolean follow_symlinks = FALSE;

static GOptionEntry entries[] = 
{
	{ "attributes", 'a', 0, G_OPTION_ARG_STRING, &attributes, "The attributes to get", NULL },
	{ "follow-symlinks", 'f', 0, G_OPTION_ARG_NONE, &follow_symlinks, "Follow symlinks", NULL },
	{ NULL }
};

static const char *
type_to_string (GFileType type)
{
  switch (type)
    {
    default:
      return "invalid type";
      
    case G_FILE_TYPE_UNKNOWN:
      return "unknown";
      
    case G_FILE_TYPE_REGULAR:
      return "regular";
      
    case G_FILE_TYPE_DIRECTORY:
      return "directory";
      
    case G_FILE_TYPE_SYMBOLIC_LINK:
      return "symlink";

    case G_FILE_TYPE_SPECIAL:
      return "special";

    case G_FILE_TYPE_SHORTCUT:
      return "shortcut";

    case G_FILE_TYPE_MOUNTABLE:
      return "mountable";
    }
}

static char *
escape_string (const char *in)
{
  GString *str;
  static char *hex_digits = "0123456789abcdef";
  char c;
  

  str = g_string_new ("");

  while ((c = *in++) != 0)
    {
      if (c >= 32 && c <= 126 && c != '\\')
	g_string_append_c (str, c);
      else
	{
	  g_string_append (str, "\\x");
	  g_string_append_c (str, hex_digits[(c >> 8) & 0xf]);
	  g_string_append_c (str, hex_digits[c & 0xf]);
	}
    }

  return g_string_free (str, FALSE);
}


static void
show_info (GFileInfo *info)
{
  const char *name, *type;
  char *escaped;
  goffset size;
  GFileAttribute *attributes;
  int n_attributes, i;

  name = g_file_info_get_display_name (info);
  if (name)
    g_print ("display name: %s\n", name);

  name = g_file_info_get_edit_name (info);
  if (name)
    g_print ("edit name: %s\n", name);
  
  name = g_file_info_get_name (info);
  if (name)
    {
      escaped = escape_string (name);
      g_print ("name: %s\n", escaped);
      g_free (escaped);
    }
  
  type = type_to_string (g_file_info_get_file_type (info));
  g_print ("type: %s\n", type);

  size = g_file_info_get_size (info);
  g_print ("size: %"G_GUINT64_FORMAT"\n", (guint64)size);

  if (g_file_info_get_is_hidden (info))
    g_print ("hidden\n");

  attributes = g_file_info_get_all_attributes (info, &n_attributes);

  if (attributes != NULL)
    {
      g_print ("attributes:\n");
      for (i = 0; i < n_attributes; i++)
	g_print ("  %s: %s\n", attributes[i].attribute, attributes[i].value);
      g_free (attributes);
    }
}

static void
get_info (GFile *file)
{
  GFileInfoRequestFlags request;
  GFileInfo *info;
  GError *error;

  if (file == NULL)
    return;

  request =
    G_FILE_INFO_FILE_TYPE | G_FILE_INFO_NAME |
    G_FILE_INFO_SIZE | G_FILE_INFO_IS_HIDDEN |
    G_FILE_INFO_DISPLAY_NAME | G_FILE_INFO_EDIT_NAME;
  
  error = NULL;
  info = g_file_get_info (file, request, attributes, follow_symlinks, NULL, &error);

  if (info == NULL)
    {
      g_printerr ("Error getting info: %s\n", error->message);
      g_error_free (error);
      return;
    }

  show_info (info);

  g_object_unref (info);
}

int
main (int argc, char *argv[])
{
  GError *error;
  GOptionContext *context;
  GFile *file;
  
  setlocale (LC_ALL, "");

  g_type_init ();
  
  error = NULL;
  context = g_option_context_new ("- show info for <location>");
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
  g_option_context_parse (context, &argc, &argv, &error);
  
  if (argc > 1)
    {
      int i;
      
      for (i = 1; i < argc; i++) {
	file = g_file_get_for_commandline_arg (argv[i]);
	get_info (file);
	g_object_unref (file);
      }
    }

  return 0;
}