gio-tool.c 8.83 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright 2015 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
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
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Matthias Clasen <mclasen@redhat.com>
 */

#include "config.h"

#include <gio/gio.h>
#include <gi18n.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include "gio-tool.h"


void
34
print_error (const gchar *format, ...)
35
{
36 37 38 39 40 41 42 43 44 45 46 47
  gchar *message;
  va_list args;

  va_start (args, format);
  message = g_strdup_vprintf (format, args);
  va_end (args);

  g_printerr ("gio: %s\n", message);
  g_free (message);
}

void
48
print_file_error (GFile *file, const gchar *message)
49 50 51 52 53 54
{
  gchar *uri;

  uri = g_file_get_uri (file);
  print_error ("%s: %s", uri, message);
  g_free (uri);
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
}

void
show_help (GOptionContext *context, const char *message)
{
  char *help;

  if (message)
    g_printerr ("gio: %s\n\n", message);

  help = g_option_context_get_help (context, TRUE, NULL);
  g_printerr ("%s", help);
  g_free (help);
}

const char *
file_type_to_string (GFileType type)
{
  switch (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";
    default:
      return "invalid type";
    }
}

const char *
attribute_type_to_string (GFileAttributeType type)
{
  switch (type)
    {
    case G_FILE_ATTRIBUTE_TYPE_INVALID:
      return "invalid";
    case G_FILE_ATTRIBUTE_TYPE_STRING:
      return "string";
    case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
      return "bytestring";
    case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
      return "boolean";
    case G_FILE_ATTRIBUTE_TYPE_UINT32:
      return "uint32";
    case G_FILE_ATTRIBUTE_TYPE_INT32:
      return "int32";
    case G_FILE_ATTRIBUTE_TYPE_UINT64:
      return "uint64";
    case G_FILE_ATTRIBUTE_TYPE_INT64:
      return "int64";
    case G_FILE_ATTRIBUTE_TYPE_OBJECT:
      return "object";
    default:
118
      return "unknown type";
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    }
}

GFileAttributeType
attribute_type_from_string (const char *str)
{
  if (strcmp (str, "string") == 0)
    return G_FILE_ATTRIBUTE_TYPE_STRING;
  if (strcmp (str, "stringv") == 0)
    return G_FILE_ATTRIBUTE_TYPE_STRINGV;
  if (strcmp (str, "bytestring") == 0)
    return G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
  if (strcmp (str, "boolean") == 0)
    return G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
  if (strcmp (str, "uint32") == 0)
    return G_FILE_ATTRIBUTE_TYPE_UINT32;
  if (strcmp (str, "int32") == 0)
    return G_FILE_ATTRIBUTE_TYPE_INT32;
  if (strcmp (str, "uint64") == 0)
    return G_FILE_ATTRIBUTE_TYPE_UINT64;
  if (strcmp (str, "int64") == 0)
    return G_FILE_ATTRIBUTE_TYPE_INT64;
  if (strcmp (str, "object") == 0)
    return G_FILE_ATTRIBUTE_TYPE_OBJECT;
  if (strcmp (str, "unset") == 0)
    return G_FILE_ATTRIBUTE_TYPE_INVALID;
  return -1;
}

char *
attribute_flags_to_string (GFileAttributeInfoFlags flags)
{
  GString *s;
  int i;
  gboolean first;
  struct {
    guint32 mask;
    char *descr;
  } flag_descr[] = {
    {
      G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE,
      N_("Copy with file")
    },
    {
      G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED,
      N_("Keep with file when moved")
    }
  };

  first = TRUE;

  s = g_string_new ("");
  for (i = 0; i < G_N_ELEMENTS (flag_descr); i++)
    {
      if (flags & flag_descr[i].mask)
        {
          if (!first)
            g_string_append (s, ", ");
          g_string_append (s, gettext (flag_descr[i].descr));
          first = FALSE;
        }
    }

  return g_string_free (s, FALSE);
}

gboolean
file_is_dir (GFile *file)
{
  GFileInfo *info;
  gboolean res;

  info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, 0, NULL, NULL);
  res = info && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
  if (info)
    g_object_unref (info);
  return res;
}


static int
handle_version (int argc, char *argv[], gboolean do_help)
{
  if (do_help || argc > 1)
    {
      if (!do_help)
205
        g_printerr ("gio: %s\n\n", _("“version” takes no arguments"));
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

      g_printerr ("%s\n", _("Usage:"));
      g_printerr ("  gio version\n");
      g_printerr ("\n");
      g_printerr ("%s\n", _("Print version information and exit."));

      return do_help ? 0 : 2;
    }

  g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version, glib_micro_version);

  return 0;
}

static void
usage (void)
{
  g_printerr ("%s\n", _("Usage:"));
224
  g_printerr ("  gio %s %s\n", _("COMMAND"), _("[ARGS…]"));
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  g_printerr ("\n");
  g_printerr ("%s\n", _("Commands:"));
  g_printerr ("  help     %s\n", _("Print help"));
  g_printerr ("  version  %s\n", _("Print version"));
  g_printerr ("  cat      %s\n", _("Concatenate files to standard output"));
  g_printerr ("  copy     %s\n", _("Copy one or more files"));
  g_printerr ("  info     %s\n", _("Show information about locations"));
  g_printerr ("  list     %s\n", _("List the contents of locations"));
  g_printerr ("  mime     %s\n", _("Get or set the handler for a mimetype"));
  g_printerr ("  mkdir    %s\n", _("Create directories"));
  g_printerr ("  monitor  %s\n", _("Monitor files and directories for changes"));
  g_printerr ("  mount    %s\n", _("Mount or unmount the locations"));
  g_printerr ("  move     %s\n", _("Move one or more files"));
  g_printerr ("  open     %s\n", _("Open files with the default application"));
  g_printerr ("  rename   %s\n", _("Rename a file"));
  g_printerr ("  remove   %s\n", _("Delete one or more files"));
  g_printerr ("  save     %s\n", _("Read from standard input and save"));
  g_printerr ("  set      %s\n", _("Set a file attribute"));
  g_printerr ("  trash    %s\n", _("Move files or directories to the trash"));
  g_printerr ("  tree     %s\n", _("Lists the contents of locations in a tree"));
  g_printerr ("\n");
246
  g_printerr (_("Use %s to get detailed help.\n"), "“gio help COMMAND”");
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  exit (1);
}

int
main (int argc, char **argv)
{
  const char *command;
  gboolean do_help;

  setlocale (LC_ALL, "");
  textdomain (GETTEXT_PACKAGE);
  bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);

#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif

  if (argc < 2)
    {
      usage ();
      return 1;
    }

  command = argv[1];
  argc -= 1;
  argv += 1;

  do_help = FALSE;
  if (g_str_equal (command, "help"))
    {
      if (argc == 1)
        {
          usage ();
          return 0;
        }
      else
        {
          command = argv[1];
          do_help = TRUE;
        }
    }
  else if (g_str_equal (command, "--help"))
    {
      usage ();
      return 0;
    }
  else if (g_str_equal (command, "--version"))
    command = "version";

  if (g_str_equal (command, "version"))
    return handle_version (argc, argv, do_help);
  else if (g_str_equal (command, "cat"))
    return handle_cat (argc, argv, do_help);
  else if (g_str_equal (command, "copy"))
    return handle_copy (argc, argv, do_help);
  else if (g_str_equal (command, "info"))
    return handle_info (argc, argv, do_help);
  else if (g_str_equal (command, "list"))
    return handle_list (argc, argv, do_help);
  else if (g_str_equal (command, "mime"))
    return handle_mime (argc, argv, do_help);
  else if (g_str_equal (command, "mkdir"))
    return handle_mkdir (argc, argv, do_help);
  else if (g_str_equal (command, "monitor"))
    return handle_monitor (argc, argv, do_help);
  else if (g_str_equal (command, "mount"))
    return handle_mount (argc, argv, do_help);
  else if (g_str_equal (command, "move"))
    return handle_move (argc, argv, do_help);
  else if (g_str_equal (command, "open"))
    return handle_open (argc, argv, do_help);
  else if (g_str_equal (command, "rename"))
    return handle_rename (argc, argv, do_help);
  else if (g_str_equal (command, "remove"))
    return handle_remove (argc, argv, do_help);
  else if (g_str_equal (command, "save"))
    return handle_save (argc, argv, do_help);
  else if (g_str_equal (command, "set"))
    return handle_set (argc, argv, do_help);
  else if (g_str_equal (command, "trash"))
    return handle_trash (argc, argv, do_help);
  else if (g_str_equal (command, "tree"))
    return handle_tree (argc, argv, do_help);
  else
    usage ();

  return 1;
}