示例#1
0
文件: cmd-xsync.c 项目: xboot/xboot
static uint8_t xsync_handle_start(struct xsync_ctx_t * ctx)
{
	char fpath[VFS_MAX_PATH];
	char path[PACKET_DATA_MAX];
	char buf[PACKET_DATA_MAX];
	uint32_t crc1, crc2 = 0;
	u64_t n;

	crc1  = (ctx->packet.data[0] << 24) & 0xff000000;
	crc1 |= (ctx->packet.data[1] << 16) & 0x00ff0000;
	crc1 |= (ctx->packet.data[2] <<  8) & 0x0000ff00;
	crc1 |= (ctx->packet.data[3] <<  0) & 0x000000ff;
	memset(path, 0, sizeof(path));
	memcpy(path, &ctx->packet.data[4], packet_dsize(&ctx->packet) - 4);

	shell_realpath(path, fpath);
	ctx->fd = vfs_open(fpath, O_RDONLY, 0);
	if(ctx->fd > 0)
	{
		while((n = vfs_read(ctx->fd, buf, sizeof(buf))) > 0)
		{
			crc2 = crc32_sum(crc2, (const uint8_t *)buf, n);
		}
		vfs_close(ctx->fd);
		ctx->fd = -1;

		if((crc1 == crc2) && (crc2 != 0))
			return 1;
	}

	ctx->fd = vfs_open(fpath, O_WRONLY | O_CREAT | O_TRUNC, 0755);
	if(ctx->fd < 0)
		return 0;
	return 2;
}
示例#2
0
文件: cmd-rmdir.c 项目: xboot/xboot
static int do_rmdir(int argc, char ** argv)
{
	char fpath[VFS_MAX_PATH];
	int ret = 0;
	int i;

	if(argc < 2)
	{
		usage();
		return -1;
	}

	for(i = 1; i < argc; i++)
	{
		if(shell_realpath(argv[i], fpath) < 0)
			continue;
		if(vfs_rmdir(fpath) != 0)
		{
			ret = -1;
			printf("mkdir: failed to remove directory %s\r\n", fpath);
		}
	}
	return ret;
}
示例#3
0
文件: builtin_cd.c 项目: rsenn/shish
/* change working directory
 * ----------------------------------------------------------------------- */
int builtin_cd(int argc, char **argv) {
  int c;
  int ok = 0;
  int symbolic = 1;
  const char *arg;
  unsigned long len;
  unsigned long n;
  stralloc newcwd;
  
  /* check options, -L for symlink, -P for physical path */
  while((c = shell_getopt(argc, argv, "LP")) > 0) {
    switch(c) {
      case 'L': symbolic = 1; break;
      case 'P': symbolic = 0; break;
      default: builtin_invopt(argv); return 1;
    }
  }
  
  arg = argv[shell_optind];
  stralloc_init(&newcwd);

  /* empty argument means chdir(HOME) */
  if(arg == NULL) {
    arg = var_value("HOME", &len);

    if(arg[0] == '\0') {
      sh_msg("HOME variable not set!");
      return 1;
    }
  }

  len = str_len(arg);
  
  /* when it isn't an absolute path we have to check CDPATH */
  if(arg[0] != '/') {
    char path[PATH_MAX + 1];
    const char *cdpath;

    /* loop through colon-separated CDPATH variable */
    cdpath = var_value("CDPATH", NULL);

    do {
      /* too much, too much :) */
      if((n = str_chr(cdpath, ':')) + len + 1 > PATH_MAX) {
        /* set error code and print the longer string in the error msg */
        errno = ENAMETOOLONG;
        return builtin_errmsgn(argv, (n > len ? cdpath : arg),
                               (n > len ? n : len), strerror(errno));
      }
      
      /* copy path prefix from cdpath if present */
      if(n) {
        byte_copy(path, n, cdpath);
        cdpath += n;
        path[n++] = '/';
      }
      
      /* copy the argument and canonicalize */
      str_copy(&path[n], arg);

      ok = shell_realpath(path, &newcwd, symbolic, &sh->cwd);

      /* skip the colon */
      if(*cdpath == ':')
        cdpath++;
    } while(*cdpath && !ok);

  }
  /* absolute path */
  else {
    /* last cdpath length set to 0, because we're not using cdpath here */
    n = 0;
    ok = shell_canonicalize(arg, &newcwd, symbolic);
  }
  
  stralloc_nul(&newcwd);

  /* try to chdir() if everything's ok */
  if(ok && chdir(newcwd.s) == 0) {
    /* print path if prefix was taken from cdpath */
    if(n) {
      buffer_putsa(fd_out->w, &newcwd);
      buffer_putnlflush(fd_out->w);
    }
    
    /* set the path */
    stralloc_move(&sh->cwd, &newcwd);

    /* if the path has symlinks then set sh->cwdsym */
    sh->cwdsym = (ok > 1);

    return 0;
  }
  
  /* we failed */
  builtin_error(argv, newcwd.s);
  stralloc_free(&newcwd);
  return 1;
}