Getting colder with our second freeze... it's 3.31.91 release day and string freeze, upload a tarball and lock those strings 🏂

Commit 9edba4e4 authored by Matthias Clasen's avatar Matthias Clasen

Add a new gio commandline tool

This command collects the various commandline utilities that
are currently shipped in gvfs, and unifies them under a single,
command-style binary.

The tools just use GIO APIs, so it makes sense for them to live here.
parent 669a0f72
......@@ -833,11 +833,41 @@ gresource_LDADD = libgio-2.0.la \
# ------------------------------------------------------------------------
# ------ MSVC Project File Generation ------
# ------------------------------------------------------------------------
#
MSVCPROJS = gio glib-compile-resources glib-compile-schemas
gio_FILES = $(libgio_2_0_la_SOURCES) $(win32_actual_sources) $(win32_more_sources_for_vcproj)
gio_EXCLUDES = gunix*.c|gdesktopappinfo.c|gnetworkmonitornetlink.c|gcontenttype.c|gnetworkmonitornm.c
# gio tool
bin_PROGRAMS += gio
gio_SOURCES = \
gio-tool.c \
gio-tool.h \
gio-tool-cat.c \
gio-tool-copy.c \
gio-tool-info.c \
gio-tool-list.c \
gio-tool-mime.c \
gio-tool-mkdir.c \
gio-tool-monitor.c \
gio-tool-mount.c \
gio-tool-move.c \
gio-tool-open.c \
gio-tool-rename.c \
gio-tool-remove.c \
gio-tool-save.c \
gio-tool-set.c \
gio-tool-trash.c \
gio-tool-tree.c \
$(NULL)
gio_LDADD = libgio-2.0.la \
$(top_builddir)/gobject/libgobject-2.0.la \
$(top_builddir)/glib/libglib-2.0.la \
$(NULL)
gio_HEADERS_DIR = $(includedir)/glib-2.0/gio
gio_HEADERS_INST = $(gioinclude_HEADERS) $(nodist_gioinclude_HEADERS)
gio_HEADERS_EXCLUDES = dummy
......
/*
* 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
* version 2 of the licence, or (at your option) any later version.
*
* 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 <errno.h>
#include "gio-tool.h"
static const GOptionEntry entries[] = {
{ NULL }
};
static gboolean
cat (GFile *file)
{
GInputStream *in;
char buffer[1024 * 8 + 1];
char *p;
gssize res;
gboolean close_res;
GError *error;
gboolean success;
error = NULL;
in = (GInputStream *) g_file_read (file, NULL, &error);
if (in == NULL)
{
print_file_error (file, error->message);
g_error_free (error);
return FALSE;
}
success = TRUE;
while (1)
{
res = g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
if (res > 0)
{
ssize_t written;
p = buffer;
while (res > 0)
{
written = write (STDOUT_FILENO, p, res);
if (written == -1 && errno != EINTR)
{
print_file_error (file, "error writing to stdout");
success = FALSE;
goto out;
}
res -= written;
p += written;
}
}
else if (res < 0)
{
print_file_error (file, error->message);
g_error_free (error);
error = NULL;
success = FALSE;
break;
}
else if (res == 0)
break;
}
out:
close_res = g_input_stream_close (in, NULL, &error);
if (!close_res)
{
print_file_error (file, error->message);
g_error_free (error);
success = FALSE;
}
return success;
}
int
handle_cat (int argc, char *argv[], gboolean do_help)
{
GOptionContext *context;
gchar *param;
GError *error = NULL;
int i;
gboolean res;
GFile *file;
g_set_prgname ("gio cat");
/* Translators: commandline placeholder */
param = g_strdup_printf ("%s...", _("LOCATION"));
context = g_option_context_new (param);
g_free (param);
g_option_context_set_help_enabled (context, FALSE);
g_option_context_set_summary (context,
_("Concatenate files and print to standard output."));
g_option_context_set_description (context,
_("gio cat works just like the traditional cat utility, but using GIO\n"
"locations instead of local files: for example, you can use something\n"
"like smb://server/resource/file.txt as location."));
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
if (do_help)
{
show_help (context, NULL);
return 0;
}
if (!g_option_context_parse (context, &argc, &argv, &error))
{
show_help (context, error->message);
g_error_free (error);
return 1;
}
if (argc < 2)
{
show_help (context, _("No files given"));
return 1;
}
g_option_context_free (context);
res = TRUE;
for (i = 1; i < argc; i++)
{
file = g_file_new_for_commandline_arg (argv[i]);
res &= cat (file);
g_object_unref (file);
}
return res ? 0 : 2;
}
/*
* 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
* version 2 of the licence, or (at your option) any later version.
*
* 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 <stdio.h>
#if 0
#include <locale.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#endif
#include "gio-tool.h"
static gboolean no_target_directory = FALSE;
static gboolean progress = FALSE;
static gboolean interactive = FALSE;
static gboolean preserve = FALSE;
static gboolean backup = FALSE;
static gboolean no_dereference = FALSE;
static const GOptionEntry entries[] = {
{ "no-target-directory", 'T', 0, G_OPTION_ARG_NONE, &no_target_directory, N_("No target directory"), NULL },
{ "progress", 'p', 0, G_OPTION_ARG_NONE, &progress, N_("Show progress"), NULL },
{ "interactive", 'i', 0, G_OPTION_ARG_NONE, &interactive, N_("Prompt before overwrite"), NULL },
{ "preserve", 'p', 0, G_OPTION_ARG_NONE, &preserve, N_("Preserve all attributes"), NULL },
{ "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
{ "no-dereference", 'P', 0, G_OPTION_ARG_NONE, &no_dereference, N_("Never follow symbolic links"), NULL },
{ NULL }
};
static gint64 start_time;
static gint64 previous_time;
static void
show_progress (goffset current_num_bytes,
goffset total_num_bytes,
gpointer user_data)
{
gint64 tv;
char *current_size, *total_size, *rate;
tv = g_get_monotonic_time ();
if (tv - previous_time < (G_USEC_PER_SEC / 5) &&
current_num_bytes != total_num_bytes)
return;
current_size = g_format_size (current_num_bytes);
total_size = g_format_size (total_num_bytes);
rate = g_format_size (current_num_bytes /
MAX ((tv - start_time) / G_USEC_PER_SEC, 1));
g_print ("\r\033[K");
g_print (_("Transferred %s out of %s (%s/s)"), current_size, total_size, rate);
previous_time = tv;
g_free (current_size);
g_free (total_size);
g_free (rate);
}
int
handle_copy (int argc, char *argv[], gboolean do_help)
{
GOptionContext *context;
GError *error = NULL;
char *param;
GFile *source, *dest, *target;
gboolean dest_is_dir;
char *basename;
char *uri;
int i;
GFileCopyFlags flags;
int retval = 0;
g_set_prgname ("gio copy");
/* Translators: commandline placeholder */
param = g_strdup_printf ("%s... %s", _("SOURCE"), _("DESTINATION"));
context = g_option_context_new (param);
g_free (param);
g_option_context_set_help_enabled (context, FALSE);
g_option_context_set_summary (context,
_("Copy one or more files from SOURCE to DESTINATION."));
g_option_context_set_description (context,
_("gio copy is similar to the traditional cp utility, but using GIO\n"
"locations instead of local files: for example, you can use something\n"
"like smb://server/resource/file.txt as location."));
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
if (do_help)
{
show_help (context, NULL);
return 0;
}
if (!g_option_context_parse (context, &argc, &argv, &error))
{
show_help (context, error->message);
g_error_free (error);
return 1;
}
if (argc < 3)
{
show_help (context, NULL);
return 1;
}
dest = g_file_new_for_commandline_arg (argv[argc - 1]);
if (no_target_directory && argc > 3)
{
show_help (context, NULL);
g_object_unref (dest);
return 1;
}
dest_is_dir = file_is_dir (dest);
if (!dest_is_dir && argc > 3)
{
char *message;
message = g_strdup_printf (_("Destination %s is not a directory"), argv[argc - 1]);
show_help (context, message);
g_free (message);
g_object_unref (dest);
return 1;
}
g_option_context_free (context);
for (i = 1; i < argc - 1; i++)
{
source = g_file_new_for_commandline_arg (argv[i]);
if (dest_is_dir && !no_target_directory)
{
basename = g_file_get_basename (source);
target = g_file_get_child (dest, basename);
g_free (basename);
}
else
target = g_object_ref (dest);
flags = 0;
if (backup)
flags |= G_FILE_COPY_BACKUP;
if (!interactive)
flags |= G_FILE_COPY_OVERWRITE;
if (no_dereference)
flags |= G_FILE_COPY_NOFOLLOW_SYMLINKS;
if (preserve)
flags |= G_FILE_COPY_ALL_METADATA;
error = NULL;
start_time = g_get_monotonic_time ();
if (!g_file_copy (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
{
if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
{
char line[16];
g_error_free (error);
error = NULL;
uri = g_file_get_uri (target);
g_print (_("%s: overwrite '%s' ? "), argv[0], uri);
g_free (uri);
if (fgets (line, sizeof (line), stdin) &&
(line[0] == 'y' || line[0] == 'Y'))
{
flags |= G_FILE_COPY_OVERWRITE;
start_time = g_get_monotonic_time ();
if (!g_file_copy (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
goto copy_failed;
}
}
else
{
copy_failed:
print_file_error (source, error->message);
g_error_free (error);
retval = 1;
}
}
if (progress && retval == 0)
g_print ("\n");
g_object_unref (source);
g_object_unref (target);
}
g_object_unref (dest);
return retval;
}
/*
* 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
* version 2 of the licence, or (at your option) any later version.
*
* 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 "gio-tool.h"
static gboolean writable = FALSE;
static gboolean filesystem = FALSE;
static char *attributes = NULL;
static gboolean nofollow_symlinks = FALSE;
static const GOptionEntry entries[] = {
{ "query-writable", 'w', 0, G_OPTION_ARG_NONE, &writable, N_("List writable attributes"), NULL },
{ "filesystem", 'f', 0, G_OPTION_ARG_NONE, &filesystem, N_("Get file system info"), NULL },
{ "attributes", 'a', 0, G_OPTION_ARG_STRING, &attributes, N_("The attributes to get"), N_("ATTRIBUTES") },
{ "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE, &nofollow_symlinks, N_("Don't follow symbolic links"), NULL },
{ NULL }
};
static char *
escape_string (const char *in)
{
GString *str;
static char *hex_digits = "0123456789abcdef";
unsigned 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 >> 4) & 0xf]);
g_string_append_c (str, hex_digits[c & 0xf]);
}
}
return g_string_free (str, FALSE);
}
static void
show_attributes (GFileInfo *info)
{
char **attributes;
char *s;
int i;
attributes = g_file_info_list_attributes (info, NULL);
g_print (_("attributes:\n"));
for (i = 0; attributes[i] != NULL; i++)
{
/* list the icons in order rather than displaying "GThemedIcon:0x8df7200" */
if (strcmp (attributes[i], "standard::icon") == 0 ||
strcmp (attributes[i], "standard::symbolic-icon") == 0)
{
GIcon *icon;
int j;
const char * const *names = NULL;
if (strcmp (attributes[i], "standard::symbolic-icon") == 0)
icon = g_file_info_get_symbolic_icon (info);
else
icon = g_file_info_get_icon (info);
/* only look up names if GThemedIcon */
if (G_IS_THEMED_ICON(icon))
{
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
g_print (" %s: ", attributes[i]);
for (j = 0; names[j] != NULL; j++)
g_print ("%s%s", names[j], (names[j+1] == NULL)?"":", ");
g_print ("\n");
}
else
{
s = g_file_info_get_attribute_as_string (info, attributes[i]);
g_print (" %s: %s\n", attributes[i], s);
g_free (s);
}
}
else
{
s = g_file_info_get_attribute_as_string (info, attributes[i]);
g_print (" %s: %s\n", attributes[i], s);
g_free (s);
}
}
g_strfreev (attributes);
}
static void
show_info (GFile *file, GFileInfo *info)
{
const char *name, *type;
char *escaped, *uri;
goffset size;
name = g_file_info_get_display_name (info);
if (name)
/* Translators: This is a noun and represents and attribute of a file */
g_print (_("display name: %s\n"), name);
name = g_file_info_get_edit_name (info);
if (name)
/* Translators: This is a noun and represents and attribute of a file */
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);
}
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
{
type = file_type_to_string (g_file_info_get_file_type (info));
g_print (_("type: %s\n"), type);
}
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
{
size = g_file_info_get_size (info);
g_print (_("size: "));
g_print (" %"G_GUINT64_FORMAT"\n", (guint64)size);
}
if (g_file_info_get_is_hidden (info))
g_print (_("hidden\n"));
uri = g_file_get_uri (file);
g_print (_("uri: %s\n"), uri);
g_free (uri);
show_attributes (info);
}
static gboolean
query_info (GFile *file)
{
GFileQueryInfoFlags flags;
GFileInfo *info;
GError *error;
if (file == NULL)
return FALSE;
if (attributes == NULL)
attributes = "*";
flags = 0;
if (nofollow_symlinks)
flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
error = NULL;
if (filesystem)
info = g_file_query_filesystem_info (file, attributes, NULL, &error);
else
info = g_file_query_info (file, attributes, flags, NULL, &error);
if (info == NULL)
{
g_printerr ("Error getting info: %s\n", error->message);
g_error_free (error);
return FALSE;
}
if (filesystem)
show_attributes (info);
else
show_info (file, info);
g_object_unref (info);
return TRUE;
}
static gboolean
get_writable_info (GFile *file)
{
GFileAttributeInfoList *list;
GError *error;
int i;
char *flags;
if (file == NULL)
return FALSE;
error = NULL;
list = g_file_query_settable_attributes (file, NULL, &error);
if (list == NULL)
{
g_printerr (_("Error getting writable attributes: %s\n"), error->message);
g_error_free (error);
return FALSE;
}
g_print (_("Settable attributes:\n"));
for (i = 0; i < list->n_infos; i++)
{
flags = attribute_flags_to_string (list->infos[i].flags);
g_print (" %s (%s%s%s)\n",
list->infos[i].name,
attribute_type_to_string (list->infos[i].type),
(*flags != 0)?", ":"", flags);
g_free (flags);
}
g_file_attribute_info_list_unref (list);
list = g_file_query_writable_namespaces (file, NULL, &error);
if (list == NULL)
{
g_printerr ("Error getting writable namespaces: %s\n", error->message);
g_error_free (error);
return FALSE;
}
if (list->n_infos > 0)
{
g_print (_("Writable attribute namespaces:\n"));
for (i = 0; i < list->n_infos; i++)
{
flags = attribute_flags_to_string (list->infos[i].flags);
g_print (" %s (%s%s%s)\n",
list->infos[i].name,
attribute_type_to_string (list->infos[i].type),
(*flags != 0)?", ":"", flags);
}
}
g_file_attribute_info_list_unref (list);
return TRUE;
}
int
handle_info (int argc, char *argv[], gboolean do_help)
{
GOptionContext *context;
gchar *param;
GError *error = NULL;
gboolean res;
gint i;
GFile *file;
g_set_prgname ("gio info");
/* Translators: commandline placeholder */
param = g_strdup_printf ("%s...", _("LOCATION"));
context = g_option_context_new (param);
g_free (param);
g_option_context_set_help_enabled (context, FALSE);
g_option_context_set_summary (context,
_("Show information about locations."));
g_option_context_set_description (context,
_("gio info is similar to the traditional ls utility, but using GIO\n"
"locations instead of local files: for example, you can use something\n"
"like smb://server/resource/file.txt as location. File attributes can\n"
"be specified with their GIO name, e.g. standard::icon, or just by\n"
"namespace, e.g. unix, or by '*', which matches all attributes"));
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
if (do_help)
{
show_help (context, NULL);
return 0;
}
if (!g_option_context_parse (context, &argc, &argv, &error))
{
show_help (context, error->message);
g_error_free (error);
return 1;
}
if (argc < 2)
{
show_help (context, _("No locations given"));
return 1;
}
g_option_context_free (context);
res = TRUE;
for (i = 1; i < argc; i++)
{
file = g_file_new_for_commandline_arg (argv[i]);
if (writable)
res &= get_writable_info (file);
else
res &= query_info (file);