Blame gio/gio-tool-monitor.c

Packit ae235b
/*
Packit ae235b
 * Copyright 2015 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <gi18n.h>
Packit ae235b
Packit ae235b
#include "gio-tool.h"
Packit ae235b
Packit ae235b
static gchar **watch_dirs;
Packit ae235b
static gchar **watch_files;
Packit ae235b
static gchar **watch_direct;
Packit ae235b
static gchar **watch_silent;
Packit ae235b
static gchar **watch_default;
Packit ae235b
static gboolean no_moves;
Packit ae235b
static gboolean mounts;
Packit ae235b
Packit ae235b
static const GOptionEntry entries[] = {
Packit ae235b
  { "dir", 'd', 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_dirs,
Packit ae235b
      N_("Monitor a directory (default: depends on type)"), N_("LOCATION") },
Packit ae235b
  { "file", 'f', 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_files,
Packit ae235b
      N_("Monitor a file (default: depends on type)"), N_("LOCATION") },
Packit ae235b
  { "direct", 'D', 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_direct,
Packit ae235b
      N_("Monitor a file directly (notices changes made via hardlinks)"), N_("LOCATION") },
Packit ae235b
  { "silent", 's', 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_silent,
Packit ae235b
      N_("Monitors a file directly, but doesn’t report changes"), N_("LOCATION") },
Packit ae235b
  { "no-moves", 'n', 0, G_OPTION_ARG_NONE, &no_moves,
Packit ae235b
      N_("Report moves and renames as simple deleted/created events"), NULL },
Packit ae235b
  { "mounts", 'm', 0, G_OPTION_ARG_NONE, &mounts,
Packit ae235b
      N_("Watch for mount events"), NULL },
Packit ae235b
  { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_default },
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
watch_callback (GFileMonitor      *monitor,
Packit ae235b
                GFile             *child,
Packit ae235b
                GFile             *other,
Packit ae235b
                GFileMonitorEvent  event_type,
Packit ae235b
                gpointer           user_data)
Packit ae235b
{
Packit ae235b
  gchar *child_str;
Packit ae235b
  gchar *other_str;
Packit ae235b
Packit ae235b
  g_assert (child);
Packit ae235b
Packit ae235b
  if (g_file_is_native (child))
Packit ae235b
    child_str = g_file_get_path (child);
Packit ae235b
  else
Packit ae235b
    child_str = g_file_get_uri (child);
Packit ae235b
Packit ae235b
  if (other)
Packit ae235b
    {
Packit ae235b
      if (g_file_is_native (other))
Packit ae235b
        other_str = g_file_get_path (other);
Packit ae235b
      else
Packit ae235b
        other_str = g_file_get_uri (other);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    other_str = g_strdup ("(none)");
Packit ae235b
Packit ae235b
  g_print ("%s: ", (gchar *) user_data);
Packit ae235b
  switch (event_type)
Packit ae235b
    {
Packit ae235b
    case G_FILE_MONITOR_EVENT_CHANGED:
Packit ae235b
      g_assert (!other);
Packit ae235b
      g_print ("%s: changed", child_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
Packit ae235b
      g_assert (!other);
Packit ae235b
      g_print ("%s: changes done", child_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_DELETED:
Packit ae235b
      g_assert (!other);
Packit ae235b
      g_print ("%s: deleted", child_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_CREATED:
Packit ae235b
      g_assert (!other);
Packit ae235b
      g_print ("%s: created", child_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
Packit ae235b
      g_assert (!other);
Packit ae235b
      g_print ("%s: attributes changed", child_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
Packit ae235b
      g_assert (!other);
Packit ae235b
      g_print ("%s: pre-unmount", child_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_UNMOUNTED:
Packit ae235b
      g_assert (!other);
Packit ae235b
      g_print ("%s: unmounted", child_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_MOVED_IN:
Packit ae235b
      g_print ("%s: moved in", child_str);
Packit ae235b
      if (other)
Packit ae235b
        g_print (" (from %s)", other_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_MOVED_OUT:
Packit ae235b
      g_print ("%s: moved out", child_str);
Packit ae235b
      if (other)
Packit ae235b
        g_print (" (to %s)", other_str);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_MONITOR_EVENT_RENAMED:
Packit ae235b
      g_assert (other);
Packit ae235b
      g_print ("%s: renamed to %s\n", child_str, other_str);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_FILE_MONITOR_EVENT_MOVED:
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (child_str);
Packit ae235b
  g_free (other_str);
Packit ae235b
  g_print ("\n");
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  WATCH_DIR,
Packit ae235b
  WATCH_FILE,
Packit ae235b
  WATCH_AUTO
Packit ae235b
} WatchType;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
add_watch (const gchar       *cmdline,
Packit ae235b
           WatchType          watch_type,
Packit ae235b
           GFileMonitorFlags  flags,
Packit ae235b
           gboolean           connect_handler)
Packit ae235b
{
Packit ae235b
  GFileMonitor *monitor = NULL;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFile *file;
Packit ae235b
Packit ae235b
  file = g_file_new_for_commandline_arg (cmdline);
Packit ae235b
Packit ae235b
  if (watch_type == WATCH_AUTO)
Packit ae235b
    {
Packit ae235b
      GFileInfo *info;
Packit ae235b
      guint32 type;
Packit ae235b
Packit ae235b
      info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_QUERY_INFO_NONE, NULL, &error);
Packit ae235b
      if (!info)
Packit ae235b
        goto err;
Packit ae235b
Packit ae235b
      type = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
Packit ae235b
      watch_type = (type == G_FILE_TYPE_DIRECTORY) ? WATCH_DIR : WATCH_FILE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (watch_type == WATCH_DIR)
Packit ae235b
    monitor = g_file_monitor_directory (file, flags, NULL, &error);
Packit ae235b
  else
Packit ae235b
    monitor = g_file_monitor (file, flags, NULL, &error);
Packit ae235b
Packit ae235b
  if (!monitor)
Packit ae235b
    goto err;
Packit ae235b
Packit ae235b
  if (connect_handler)
Packit ae235b
    g_signal_connect (monitor, "changed", G_CALLBACK (watch_callback), g_strdup (cmdline));
Packit ae235b
Packit ae235b
  monitor = NULL; /* leak */
Packit ae235b
  g_object_unref (file);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
Packit ae235b
err:
Packit ae235b
  print_file_error (file, error->message);
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_object_unref (file);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_monitor (int argc, gchar *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  gchar *param;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFileMonitorFlags flags;
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio monitor");
Packit ae235b
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("[%s...]", _("LOCATION"));
Packit ae235b
  context = g_option_context_new (param);
Packit ae235b
  g_free (param);
Packit ae235b
  g_option_context_set_help_enabled (context, FALSE);
Packit ae235b
  g_option_context_set_summary (context,
Packit ae235b
    _("Monitor files or directories for changes."));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  if (do_help)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      show_help (context, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!watch_dirs && !watch_files && !watch_direct && !watch_silent && !watch_default)
Packit ae235b
    {
Packit ae235b
      show_help (context, _("No locations given"));
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  flags = (no_moves ? 0 : G_FILE_MONITOR_WATCH_MOVES) |
Packit ae235b
          (mounts ? G_FILE_MONITOR_WATCH_MOUNTS : 0);
Packit ae235b
Packit ae235b
  if (watch_dirs)
Packit ae235b
    {
Packit ae235b
      for (i = 0; watch_dirs[i]; i++)
Packit ae235b
        if (!add_watch (watch_dirs[i], WATCH_DIR, flags, TRUE))
Packit ae235b
          return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (watch_files)
Packit ae235b
    {
Packit ae235b
      for (i = 0; watch_files[i]; i++)
Packit ae235b
        if (!add_watch (watch_files[i], WATCH_FILE, flags, TRUE))
Packit ae235b
          return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (watch_direct)
Packit ae235b
    {
Packit ae235b
      for (i = 0; watch_direct[i]; i++)
Packit ae235b
        if (!add_watch (watch_direct[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, TRUE))
Packit ae235b
          return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (watch_silent)
Packit ae235b
    {
Packit ae235b
      for (i = 0; watch_silent[i]; i++)
Packit ae235b
        if (!add_watch (watch_silent[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, FALSE))
Packit ae235b
          return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (watch_default)
Packit ae235b
    {
Packit ae235b
      for (i = 0; watch_default[i]; i++)
Packit ae235b
        if (!add_watch (watch_default[i], WATCH_AUTO, flags, TRUE))
Packit ae235b
          return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  while (TRUE)
Packit ae235b
    g_main_context_iteration (NULL, TRUE);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}