Public feed #721743

Someone

public text v1 · immutable
Raw Edit
·published 2007-10-01 14:50 UTC
rendered paste body
#include <config.h>

#include <stdio.h>
#include <unistd.h>
#include <locale.h>
#include <errno.h>
#include <string.h>

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

static gboolean no_create = FALSE;

static GOptionEntry entries[] = 
{
  { "no-create", 'c', 0, G_OPTION_ARG_NONE, &no_create, "do not create file if it doesn't exist'", NULL },
  { NULL }
};

static void
touch (GFile *file)
{
  GFileInfo *info;
  GError *error = NULL;
  GTimeVal now;
  gboolean res;

  info = g_file_query_info (file, "time:*", 0, NULL, &error);

  if (info == NULL)
    {
      if (error->domain == G_FILE_ERROR &&
          error->code == G_FILE_ERROR_NOENT &&
          !no_create)
        {
          g_clear_error (&error);
          g_file_create (file, 0, NULL, &error);
          if (error)
            {
              g_printerr ("Error creating file: %s\n", error->message);
              g_error_free (error);
              return;
            }

          /* TODO: get info */
        }
      else
        {
          g_printerr ("Error getting info: %s\n", error->message);
          g_error_free (error);
          return;
        }
    }

  g_get_current_time (&now);

  g_file_info_set_modification_time (info, &now);

  res = g_file_set_attributes_from_info (file, info, 0, NULL, &error);

  if (!res)
    {
      g_printerr ("Error setting info: %s\n", error->message);
      g_error_free (error);
      return;
    }
}

int
main (int argc, char *argv[])
{
  GError *error;
  GOptionContext *context;
  int i;

  setlocale (LC_ALL, "");

  g_type_init ();
  
  error = NULL;
  context = g_option_context_new ("- touch files at <location>");
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
  g_option_context_parse (context, &argc, &argv, &error);

  if (argc <= 1)
    {
      g_printerr ("Missing operand\n");
      return 1;
    }

  for (i = 1; i < argc; i++)
    {
      GFile *file;

      file = g_file_new_for_commandline_arg (argv[i]);
      touch (file);
      g_object_unref (file);
    }

  return 0;
}