Beispiel #1
0
void			check_error(char **av)
{
	t_stat		*buf;
	int			j;

	init(&j, &buf);
	while (av[j])
	{
		if ((is_lnk(av[j]) && (lstat(av[j], buf)) == -1)
				|| (!is_lnk(av[j]) && (stat(av[j], buf)) == -1))
		{
			ft_putstr("ls: ");
			if (ft_strcmp(av[j], "") == 0)
				ft_putstr("fts_open");
			else
				ft_putstr(av[j]);
			ft_putstr(": ");
			perror("");
		}
		if (ft_strcmp(av[j], "") == 0)
			exit(0);
		++j;
	}
	ft_tstatdel(&buf);
}
Beispiel #2
0
void		display_l(char *file, t_max *max, char *option)
{
	t_stat		*buf;

	if (!(buf = (t_stat*)malloc(sizeof(t_stat))))
		perror_malloc();
	if (is_lnk(file))
		if (lstat(file, buf))
			perror_lstat();
	if (!(is_lnk(file)))
		if (stat(file, buf))
			perror_stat();
	disp_all(file, buf, max, option);
	ft_tstatdel(&buf);
}
Beispiel #3
0
static void	init_buf(char *tab_j, t_stat **buf)
{
	if (is_lnk(tab_j))
		(lstat(tab_j, (*buf))) ? perror_lstat() : 0;
	else
		(stat(tab_j, (*buf))) ? perror_stat() : 0;
}
Beispiel #4
0
t_files			*dispatch(t_opm_params *opm)
{
	t_files		*to_list;
	char		*path;
	int			i;

	to_list = (t_files *)ft_memalloc(sizeof(t_files) * 1);
	to_list->files = ft_vectnew();
	to_list->dirs = ft_vectnew();
	i = 0;
	while (i < opm->params->total)
	{
		path = arm_getparam_name(opm->params, i);
		if (is_reg(path))
			ft_vectadd(to_list->files, file_initdata(path, NULL));
		else if (is_dir(path))
			ft_vectadd(to_list->dirs, file_initdata(path, NULL));
		else if (is_lnk(path))
			case_link(path, opm, to_list);
		else
			perror(ft_strjoin("ls: ", path));
		i++;
	}
	return (to_list);
}
Beispiel #5
0
// Return 1 if the backup is now finalising.
int check_for_rubble(struct async *as,
	struct sdirs *sdirs, const char *incexc,
	int *resume, struct conf **cconfs)
{
	struct asfd *asfd=as->asfd;

	switch(is_lnk(sdirs->finishing))
	{
		case 1:
			if(recover_finishing(as, sdirs, cconfs))
				return -1;
			return 1;
		case 0:
			log_and_send(asfd,
				"Finishing directory is not a symlink.\n");
			return -1;
	}

	switch(is_lnk(sdirs->working))
	{
		case 1:
			return recover_working(as,
				sdirs, incexc, resume, cconfs);
		case 0:
			log_and_send(asfd,
				"Working directory is not a symlink.\n");
			return -1;
	}

	switch(is_lnk(sdirs->currenttmp))
	{
		case 1:
			return recover_currenttmp(sdirs);
		case 0:
			log_and_send(asfd,
				"Currenttmp directory is not a symlink.\n");
			return -1;
	}

	return 0;
}
Beispiel #6
0
static void	disp_all(char *file, t_stat *buf, t_max *max, char *option)
{
	disp_permit(buf->st_mode);
	disp_xattr(file);
	disp_link(buf, max->max_lnk);
	if (!(IS_G))
		disp_owner(owner_name(buf->st_uid), max->max_user);
	if (!(IS_O))
		disp_group(group_name(buf->st_gid), max->max_group);
	if (IS_O && IS_G)
		write(1, "  ", 2);
	disp_size(buf, max->max_size, max->is_b_c_file);
	ft_putstr(s_time(buf));
	write(1, " ", 1);
	(is_lnk(file)) ? disp_lnk_name(file) : 0;
	if (!(is_lnk(file)))
		ft_putstr_name(file);
	if (IS_P && is_file_or_dir(file) == 2)
		write(1, "/", 1);
	if (!(is_lnk(file)))
		write(1, "\n", 1);
}
Beispiel #7
0
int				is_lnk_fold(char *file)
{
	int		is_fold;
	int		i;

	is_fold = 0;
	i = 0;
	if (is_lnk(file))
	{
		while (file[i])
			++i;
		--i;
		if (file[i] == '/')
			is_fold = 1;
	}
	return (is_fold);
}
Beispiel #8
0
static int recover_currenttmp(struct sdirs *sdirs)
{
	logp("Found currenttmp symlink\n");
	switch(is_lnk_valid(sdirs->currenttmp))
	{
		case 0:
			logp("But currenttmp is not pointing at something valid.\n");
			logp("Deleting it.\n");
			return unlink_w(sdirs->currenttmp, __func__);
		case -1:
			return -1;
	}

	switch(is_lnk(sdirs->current))
	{
		case 0:
			logp("But current already exists and is not a symlink!\n");
			logp("Giving up.\n");
			return -1;
		case 1:
			logp("But current symlink already exists!\n");
			switch(is_lnk_valid(sdirs->current))
			{
				case 0:
					logp("But current symlink is not pointing at something valid.\n");
					logp("Replacing current with currenttmp.\n");
					return do_rename(sdirs->currenttmp,
						sdirs->current);
				case 1:
					logp("And current symlink points at something valid.\n");
					logp("Deleting currenttmp.\n");
					return unlink_w(sdirs->currenttmp, __func__);
				default:
					return -1;
			}
		default:
			logp("Renaming currenttmp to current\n");
			return do_rename(sdirs->currenttmp, sdirs->current);
	}
}
Beispiel #9
0
int			find_b_c_file_list(t_content *file)
{
	int			ret;
	t_stat		*buf;
	int			j;

	j = 0;
	ret = 0;
	if (!(buf = (t_stat*)malloc(sizeof(*buf))))
		perror_malloc();
	while (file)
	{
		if (is_lnk(file->name))
			(lstat(file->name, buf)) ? perror_lstat() : 0;
		else
			(stat(file->name, buf)) ? perror_stat() : 0;
		if (S_ISCHR(buf->st_mode) || S_ISBLK(buf->st_mode))
			ret = 1;
		file = file->next;
	}
	(buf) ? ft_tstatdel(&buf) : 0;
	return (ret);
}
Beispiel #10
0
int			find_b_c_file(char **tab)
{
	int			ret;
	t_stat		*buf;
	int			j;

	j = 0;
	ret = 0;
	if (!(buf = (t_stat*)malloc(sizeof(*buf))))
		perror_malloc();
	while (tab[j])
	{
		if (is_lnk(tab[j]))
			(lstat(tab[j], buf)) ? perror_lstat() : 0;
		else
			(stat(tab[j], buf)) ? perror_stat() : 0;
		if (S_ISCHR(buf->st_mode) || S_ISBLK(buf->st_mode))
			ret = 1;
		++j;
	}
	(buf) ? ft_tstatdel(&buf) : 0;
	return (ret);
}
Beispiel #11
0
/* This is the function which is called to display all files and
 * directories, and it's where the magic happens.  We are called with
 * full stat and extended attributes for each file, so there is no
 * penalty for displaying anything in those structures.  However if we
 * need other things (eg. checksum) we may have to go back to the
 * appliance and then there can be a very large penalty.
 */
static int
show_file (const char *dir, const char *name,
           const struct guestfs_stat *stat,
           const struct guestfs_xattr_list *xattrs,
           void *unused)
{
  const char *filetype;
  CLEANUP_FREE char *path = NULL, *csum = NULL, *link = NULL;

  /* Display the basic fields. */
  output_start_line ();

  if (is_reg (stat->mode))
    filetype = "-";
  else if (is_dir (stat->mode))
    filetype = "d";
  else if (is_chr (stat->mode))
    filetype = "c";
  else if (is_blk (stat->mode))
    filetype = "b";
  else if (is_fifo (stat->mode))
    filetype = "p";
  else if (is_lnk (stat->mode))
    filetype = "l";
  else if (is_sock (stat->mode))
    filetype = "s";
  else
    filetype = "u";
  output_string (filetype);
  output_int64_perms (stat->mode & 07777);

  output_int64_size (stat->size);

  /* Display extra fields when enabled. */
  if (enable_uids) {
    output_int64_uid (stat->uid);
    output_int64_uid (stat->gid);
  }

  if (enable_times) {
    output_int64_time (stat->atime);
    output_int64_time (stat->mtime);
    output_int64_time (stat->ctime);
  }

  if (enable_extra_stats) {
    output_int64_dev (stat->dev);
    output_int64 (stat->ino);
    output_int64 (stat->nlink);
    output_int64_dev (stat->rdev);
    output_int64 (stat->blocks);
  }

  /* Disabled for now -- user would definitely want these to be interpreted.
  if (enable_xattrs)
    output_xattrs (xattrs);
  */

  path = full_path (dir, name);

  if (checksum && is_reg (stat->mode)) {
    csum = guestfs_checksum (g, checksum, path);
    if (!csum)
      exit (EXIT_FAILURE);

    output_string (csum);
  }

  output_string (path);

  if (is_lnk (stat->mode))
    /* XXX Fix this for NTFS. */
    link = guestfs_readlink (g, path);
  if (link)
    output_string_link (link);

  output_end_line ();

  return 0;
}
Beispiel #12
0
static void
output_file (guestfs_h *g, struct file *file)
{
    const char *filetype;
    size_t i;
    CLEANUP_FREE char *link = NULL;

    if (is_reg (file->stat->st_mode))
        filetype = "-";
    else if (is_dir (file->stat->st_mode))
        filetype = "d";
    else if (is_chr (file->stat->st_mode))
        filetype = "c";
    else if (is_blk (file->stat->st_mode))
        filetype = "b";
    else if (is_fifo (file->stat->st_mode))
        filetype = "p";
    else if (is_lnk (file->stat->st_mode))
        filetype = "l";
    else if (is_sock (file->stat->st_mode))
        filetype = "s";
    else
        filetype = "u";

    output_string (filetype);
    output_int64_perms (file->stat->st_mode & 07777);

    output_int64_size (file->stat->st_size);

    /* Display extra fields when enabled. */
    if (enable_uids) {
        output_int64_uid (file->stat->st_uid);
        output_int64_uid (file->stat->st_gid);
    }

    if (enable_times) {
        if (atime)
            output_int64_time (file->stat->st_atime_sec, file->stat->st_atime_nsec);
        output_int64_time (file->stat->st_mtime_sec, file->stat->st_mtime_nsec);
        output_int64_time (file->stat->st_ctime_sec, file->stat->st_ctime_nsec);
    }

    if (enable_extra_stats) {
        output_int64_dev (file->stat->st_dev);
        output_int64 (file->stat->st_ino);
        output_int64 (file->stat->st_nlink);
        output_int64_dev (file->stat->st_rdev);
        output_int64 (file->stat->st_blocks);
    }

    if (file->csum)
        output_string (file->csum);

    output_string (file->path);

    if (is_lnk (file->stat->st_mode)) {
        /* XXX Fix this for NTFS. */
        link = guestfs_readlink (g, file->path);
        if (link)
            output_string_link (link);
    }

    if (enable_xattrs) {
        for (i = 0; i < file->xattrs->len; ++i) {
            output_string (file->xattrs->val[i].attrname);
            output_binary (file->xattrs->val[i].attrval,
                           file->xattrs->val[i].attrval_len);
        }
    }
}
Beispiel #13
0
int transfer_begin(struct job *j)
{
    int res;
    char *pread = NULL, *pwrite = NULL;
    size_t p_len;

    pthread_mutex_lock(&m_transfer);

    if (t_state.active)
    {
        DEBUG("called transfer_begin while a transfer is active!");
        pthread_mutex_unlock(&m_transfer);
        return TRANSFER_FAIL;
    }
    pthread_mutex_unlock(&m_transfer);

    p_len = strlen(j->path);

    if (j->op == JOB_PUSH)
    {
        pread = cache_path2(j->path, p_len);
        pwrite = remote_path2(j->path, p_len);
    }
    else if (j->op == JOB_PULL)
    {
        pread = remote_path2(j->path, p_len);
        pwrite = cache_path2(j->path, p_len);
    }

    if (!pread || !pwrite)
    {
        free(pread);
        free(pwrite);
        errno = ENOMEM;
        return -1;
    }

    if (is_reg(pread))
    {
        if (!is_reg(pwrite) && !is_nonexist(pwrite))
        {
            DEBUG("write target is non-regular file: %s", pwrite);
            free(pread);
            free(pwrite);
            return TRANSFER_FAIL;
        }

        pthread_mutex_lock(&m_transfer);
        t_state.active = true;
        t_state.job = j;
        t_state.offset = 0;
        pthread_mutex_unlock(&m_transfer);

        res = transfer(pread, pwrite);
        free(pread);
        free(pwrite);
        return res;
    }
    /* if symlink, copy instantly */
    else if (is_lnk(pread))
    {
        DEBUG("push/pull on symlink");
        copy_symlink(pread, pwrite);
        copy_attrs(pread, pwrite);
        free(pread);
        free(pwrite);
        return TRANSFER_FINISH;
    }
    else if (is_dir(pread))
    {
        DEBUG("push/pull on DIR");
        clone_dir(pread, pwrite);
        copy_attrs(pread, pwrite);
        free(pread);
        free(pwrite);
        return TRANSFER_FINISH;
    }
    else
    {
        ERROR("cannot read file %s", pread);
        free(pread);
        free(pwrite);
        return TRANSFER_FAIL;
    }

    DEBUG("wtf");
    return TRANSFER_FAIL;
}