Пример #1
0
static void
mount_done_cb (GObject *object,
               GAsyncResult *res,
               gpointer user_data)
{
  gboolean succeeded;
  GError *error = NULL;
  GMountOperation *op = user_data;

  succeeded = g_file_mount_enclosing_volume_finish (G_FILE (object), res, &error);

  if (!succeeded)
    {
      success = FALSE;
      if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
        print_file_error (G_FILE (object), _("Anonymous access denied"));
      else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
        print_file_error (G_FILE (object), error->message);

      g_error_free (error);
    }

  outstanding_mounts--;

  if (outstanding_mounts == 0)
    g_main_loop_quit (main_loop);
}
Пример #2
0
static void
eject (GFile *file)
{
  GMount *mount;
  GError *error = NULL;
  GMountOperation *mount_op;
  GMountUnmountFlags flags;

  if (file == NULL)
    return;

  mount = g_file_find_enclosing_mount (file, NULL, &error);
  if (mount == NULL)
    {
      print_file_error (file, error->message);
      success = FALSE;
      g_error_free (error);
      return;
    }

  mount_op = new_mount_op ();
  flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
  g_mount_eject_with_operation (mount, flags, mount_op, NULL, eject_done_cb, g_object_ref (file));
  g_object_unref (mount_op);

  outstanding_mounts++;
}
Пример #3
0
static void
eject_done_cb (GObject *object,
               GAsyncResult *res,
               gpointer user_data)
{
  gboolean succeeded;
  GError *error = NULL;
  GFile *file = G_FILE (user_data);

  succeeded = g_mount_eject_with_operation_finish (G_MOUNT (object), res, &error);

  g_object_unref (G_MOUNT (object));

  if (!succeeded)
    {
      print_file_error (file, error->message);
      success = FALSE;
      g_error_free (error);
    }

  g_object_unref (file);

  outstanding_mounts--;

  if (outstanding_mounts == 0)
    g_main_loop_quit (main_loop);
}
Пример #4
0
Файл: main.c Проект: mk12/eva
// Processes the command line arguments. Returns true on success.
static bool process_args(int argc, char **argv, struct Environment *env) {
	if (argc == 2 && is_opt(argv[1], 'h', "help")) {
		fputs(usage_message, stdout);
		return true;
	}

	bool tty = isatty(0);
	bool prelude = true;
	for (int i = 1; i < argc; i++) {
		if (is_opt(argv[i], 'n', "no-prelude")) {
			prelude = false;
			argv[i] = NULL;
			break;
		}
	}
	if (prelude) {
		execute(PRELUDE_FILENAME, prelude_source, env, false);
	}

	if (argc == 1 || (argc == 2 && !prelude)) {
		repl(env, tty);
		return true;
	}
	for (int i = 1; i < argc; i++) {
		if (argv[i] == NULL) {
			continue;
		}

		if (strcmp(argv[i], "-") == 0) {
			repl(env, tty);
		} else if (is_opt(argv[i], 'e', "expression")) {
			// Execute the next argument.
			if (i == argc - 1) {
				print_error(argv[i], err_opt_argument);
				return false;
			}
			if (!execute(argv_filename, argv[++i], env, true)) {
				return false;
			}
		} else {
			// Assume the argument is a filename.
			char *text = read_file(argv[i]);
			if (!text) {
				print_file_error(argv[i]);
				return false;
			}
			bool success = execute(argv[i], text, env, false);
			free(text);
			if (!success) {
				return false;
			}
		}
	}
	return true;
}
Пример #5
0
/* ------------------------------------------------------------------
 * Opens and iterates through a given directory, operating on allowed
 * files found within it.  . and .. are skipped, as are files beginning
 * with . unless it's been enabled in the flags.  All other entries,
 * both files and directories, are thrown to process_entry to determine
 * whether they're a file/directory/link, and to read/explore them if
 * appropriate.  If threads are being used, they wait for their children
 * threads to terminate after they themselves are done with their work.
 *
 * flags:	The usual I/O option flags.
 * list_head:	A list of already visited directories.  Always will have
 *		the working directory low was called from as the tail,
 *		and will always have the current directory being explored
 *		as the head (so the src = list_head->next->path assignment
 *		is always safe).
 * fullpath:	The realpath of the current directory being explored.
 * dir_depth:	Passed to process_entry.
 * ------------------------------------------------------------------
 */
int explore_dir(params *P, visited_dir *list_head, char *nextpath, 
		int dir_depth)
{
	DIR *d;
	struct dirent *entry;
	struct dirent **done;
	int len;
	
	if ( (d = opendir(nextpath)) == 0)
	{
		return print_file_error(P, errno, nextpath);
	}
	
	if (((P->max_dir_depth) - dir_depth) > read_stat(P, t_ddepth))
		update_stat(P, t_ddepth, ((P->max_dir_depth) - dir_depth));
	
	len = offsetof(struct dirent, d_name) + 
		pathconf(nextpath, _PC_NAME_MAX) + 1;
	if ((entry = malloc(len)) == NULL || 
			(done = malloc(sizeof(struct dirent*))) == NULL)
	{
		fprintf(stderr, "Malloc failed in explore_dir.\n");
		return -1;
	}
	done = &entry;

	while ( (readdir_r(d, entry, done)) == 0 && (*done != NULL))
	{
		/* don't process '.' or '..' in a directory! */
		if ( (strcmp(entry->d_name, THIS_DIR) == 0) 
			|| (strcmp(entry->d_name, PARENT_DIR) == 0))
			continue;

		/* do all files but ones beginning with a dot,
		 * unless we've enabled it! */
		if (entry->d_name[0] == '.')
		{
			if (enabled(P, READ_DOT_FILES))
			{
				process_entry(P, list_head, entry->d_name,
					nextpath, dir_depth, 0);
			}
			else
				inc_stat(P, t_dotfiles, 1);
		}
		else
			process_entry(P, list_head, entry->d_name, nextpath,
				dir_depth, 0);
	}
	closedir(d);
	wait_for_children(list_head);
	free(nextpath);
	return 0;	
}
Пример #6
0
static int	nm(char *filename, t_arg_nm *options, t_bin *binary, size_t count)
{
	t_head	headers;

	get_binary_headers(binary->data, &headers);
	if (!headers.mach32 && !headers.mach64)
	{
		print_file_error(filename,
				"The file was not recognized as a valid object file.");
		return (1);
	}
	if ((!headers.mach32 && options->arch == A_X32) ||
		(!headers.mach64 && options->arch == A_X64))
		print_file_error(filename, "No architecture specified.");
	else if (((options->arch == A_ALL && (!headers.mach32 || !headers.mach64))
		|| options->arch == A_DEF ||
		(options->arch == A_X32 && headers.mach32 && !headers.mach64) ||
		(options->arch == A_X64 && headers.mach64 && !headers.mach32))
		&& count > 1
		&& !(options->arch == A_DEF && headers.mach32 && headers.mach64))
		print_filename_arch(filename, NULL);
	print_symbols(filename, options, &headers);
	return (0);
}
Пример #7
0
/* ------------------------------------------------------------------
 * Given a string to a file or path, first determines the full 'real' path.
 *
 * If the filename is a symlink, we resolve the link and call this function
 * again, with the from_link flag set.  This is used in deciding which
 * parameter (filename or fullpath) to use when calling lstat, and in
 * determining whether it is necessary to check for loops in the directory
 * hierarchy.
 *
 * If the filename is actually a directory, (and is not already open),
 * explore_dir is called to take care of its entries.  If the directory is
 * found within the visited_dir list, then we have found a loop, and the
 * directory is not explored.
 *
 * Otherwise, it's a regular ol' file and we just call read_file on it.
 *
 * list:	Linked list of visited directories.  Loops are detected
 *		by comparing absolute pathnames.
 * filename:	Name of entity being examined (directory or file).
 * fullpath:	Buffer to hold the full pathname of a file.
 * dir_d:	Maxmimum allowed depth of directory recursion to perform.
 * from_link:	Flag denoting whether this function has been called from
 *		a symbolic link or not.
 * ------------------------------------------------------------------
 */
int process_entry(params *P, visited_dir *list, char *filename,
		  char *path, int dir_d, int from_link)
{
	struct stat statbuf;
	dev_t dev;
	ino_t ino;
	char *absolute_filename;
	int myerrno;
	if ((absolute_filename = malloc(path_size)) == NULL)
	{
		fprintf(stderr, "Malloc failed in process_entry.\n");
		return -1;
	}
	pthread_mutex_lock(&chdir_mutex);
	chdir(path);
	if (lstat(filename, &statbuf) < 0)
	{
		myerrno = errno;
		pthread_mutex_unlock(&chdir_mutex);
		if (filename[0] == '/')
		{
			snprintf(absolute_filename,path_size, "%s", filename);
		}
		else
		{
			snprintf(absolute_filename,path_size, "%s/%s", 
				path, filename);
		}
		if (!enabled(P, NO_SYMLINKS) || (myerrno != ELOOP))
			print_file_error(P, myerrno, absolute_filename);
		free(absolute_filename);
		return 0;
	}

	if (realpath(filename, absolute_filename) == 0)
	{
		myerrno = errno;
		pthread_mutex_unlock(&chdir_mutex);
		if (filename[0] == '/')
		{
			snprintf(absolute_filename,path_size, "%s", filename);
		}
		else
		{
			snprintf(absolute_filename,path_size, "%s/%s", 
				path, filename);
		}

		if (!enabled(P, NO_SYMLINKS) || (myerrno != ELOOP))
			print_file_error(P, myerrno, absolute_filename);
		free(absolute_filename);
		return 0;
	}
	if ( S_ISLNK(statbuf.st_mode))
	{ /* symbolic link: could be either file or dir, so resolve & process */
		if (!enabled(P, NO_SYMLINKS))
		{
			if (lstat(absolute_filename, &statbuf) < 0)
			{
				myerrno = errno;
				pthread_mutex_unlock(&chdir_mutex);
				/*snprintf(absolute_filename,path_size, "%s/%s", 
						path, filename);*/
				print_file_error(P, myerrno, absolute_filename);

				free(absolute_filename);
				return 0;
			}
		}
		else
		{
			free(absolute_filename);
			return 0;
		}
	}
	pthread_mutex_unlock(&chdir_mutex);
	dev = statbuf.st_dev;
	ino = statbuf.st_ino;
	myerrno = 0;
	if (S_ISREG(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
	{ /* regular, readable file */
		read_file(P, absolute_filename);
		inc_stat(P, t_infiles, 1);
		free(absolute_filename);
	}
	else if (!S_ISDIR(statbuf.st_mode))
	{ /* if not link, file, or dir, error (if appropriate) */
		print_file_error(P, 0, absolute_filename);
		free(absolute_filename);
		return 0;
	}
		
	else if (S_ISDIR(statbuf.st_mode) && dir_d != 0)
	{ /* directory, and dir_depth allows us to explore it */
		if(have_visited(list, dev, ino))
		{ /* if loop found, print it, but don't explore */
			inc_stat(P, t_dloops, 1);
			if (!enabled(P, QUIET_FILENAME))
			{
				print_loop(P, filename, path, 
					absolute_filename, list, dev, ino);
			}
			free(absolute_filename);
		}
		else
		{ /* add a list node, explore the dir, then free the node */
			handle_directory(P, list, absolute_filename, 
				dev, ino, path, (dir_d - 1));
		}
	}
	else if (S_ISDIR(statbuf.st_mode) && dir_d == 0)
	{
		inc_stat(P, t_dskips, 1);
		free(absolute_filename);
	}
	return 0;
}
Пример #8
0
int
handle_mkdir (int argc, char *argv[], gboolean do_help)
{
    GOptionContext *context;
    gchar *param;
    GError *error = NULL;
    GFile *file;
    int retval = 0;
    int i;

    g_set_prgname ("gio mkdir");

    /* 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, _("Create directories."));
    g_option_context_set_description (context,
                                      _("gio mkdir is similar to the traditional mkdir utility, but using GIO\n"
                                        "locations instead of local files: for example, you can use something\n"
                                        "like smb://server/resource/mydir 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 locations gives"));
        return 1;
    }

    g_option_context_free (context);

    for (i = 1; i < argc; i++)
    {
        file = g_file_new_for_commandline_arg (argv[i]);
        error = NULL;
        if (parent)
        {
            if (!g_file_make_directory_with_parents (file, NULL, &error))
            {
                print_file_error (file, error->message);
                g_error_free (error);
                retval = 1;
            }
        }
        else
        {
            if (!g_file_make_directory (file, NULL, &error))
            {
                print_file_error (file, error->message);
                g_error_free (error);
                retval = 1;
            }
            g_object_unref (file);
        }
    }

    return retval;

}
Пример #9
0
extern void
_DtCmsCollectOne(_DtCmsCalendar *cal)
{
	Rb_Status	status;
	CSA_return_code	 	stat;
	char *bak, *temp, *clog;

	if (cal == NULL)
		return;

	status	= rb_check_tree(APPT_TREE(cal));
	clog	= _DtCmsGetLogFN(cal->calendar);
	temp	= _DtCmsGetTmpFN(cal->calendar);
	bak	= _DtCmsGetBakFN(cal->calendar);

	if (status != rb_ok || clog==NULL || temp==NULL || bak==NULL) {
		fprintf(stderr, "%s: cannot acquire files to execute garbage collection.\n", pgname);
		fprintf(stderr, "possible causes: cannot find home directory.\n");
		fprintf(stderr, "\tNIS or your host server might be down.\n");
		fprintf(stderr, "damage: none\n\n");
		goto cleanup;
	}

	/* Make sure that the temp file does not exist before garbage collect */
	unlink (temp);
	if (cal->fversion == _DtCMS_VERSION1)
		stat = _DtCmsCreateLogV1(cal->owner, temp);
	else
		stat = _DtCmsCreateLogV2(cal->owner, temp);

	if (stat != CSA_SUCCESS) {
		if (stat == (CSA_X_DT_E_BACKING_STORE_PROBLEM))
			print_file_error(temp,
				"file error during garbage collection");
		else {
			fprintf(stderr, "%s: file error on %s during garbage collection\n",
				pgname, temp);
			fprintf(stderr, "Reason: getpwnam() failed. %s%s\n",
				"No passwd entry for owner of ",
				cal->calendar);

			fprintf(stderr, "damage: none\n\n");
		}
		goto cleanup;
	}

	if (cal->fversion == _DtCMS_VERSION1)
		stat = _DtCmsDumpDataV1(temp, cal);
	else
		stat = _DtCmsDumpDataV2(temp, cal);

	if (stat != CSA_SUCCESS) {
		print_file_error(temp,
			(stat == (CSA_X_DT_E_BACKING_STORE_PROBLEM) ?
			 "file error during garbage collection" :
			 "can't dump data structure to file during garbage collection"));
		goto cleanup;
	}

	/* mv -f .callog .calbak; mv -f temp .callog */
	if (rename (clog, bak) < 0) {
		perror ("rpc.cmsd: Can't backup callog to .calbak.\nreason:");
		goto cleanup;
	}

	if (rename (temp, clog) < 0) {
		perror("rpc.cmsd: Can't move .caltemp to callog.\nreason:");
		fprintf(stderr, "%s: you may recover %s from %s.\n", pgname,
			clog, bak);
	}

cleanup:
	if (bak != NULL) {
		free(bak);
		bak = NULL;
	}
	if (temp != NULL) {
		free(temp);
		temp = NULL;
	}
	if (clog != NULL) {
		free(clog);
		clog = NULL;
	}
}
Пример #10
0
int
handle_remove (int argc, char *argv[], gboolean do_help)
{
  GOptionContext *context;
  gchar *param;
  GError *error = NULL;
  GFile *file;
  int retval;
  int i;

  g_set_prgname ("gio remove");

  /* 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, _("Delete the given files."));
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);

  if (do_help)
    {
      show_help (context, NULL);
      g_option_context_free (context);
      return 0;
    }

  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      show_help (context, error->message);
      g_error_free (error);
      g_option_context_free (context);
      return 1;
    }

  if (argc == 1)
    {
      show_help (context, _("No locations given"));
      g_option_context_free (context);
      return 1;
    }

  g_option_context_free (context);

  retval = 0;
  for (i = 1; i < argc; i++)
    {
      file = g_file_new_for_commandline_arg (argv[i]);
      if (!g_file_delete (file, NULL, &error))
        {
          if (!force ||
              !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
            {
              print_file_error (file, error->message);
              retval = 1;
            }
          g_clear_error (&error);
        }
      g_object_unref (file);
    }

  return retval;
}