示例#1
0
/*
 * Convert a system path into a VFS path.
 */
const char *fs_resolve(const char *system)
{
    static char path[MAXSTR];

    const char *p;

    /*
     * PhysicsFS will claim a file doesn't exist if its path uses a
     * directory separator other than a forward slash, even if that
     * separator is valid for the system. We'll oblige.
     */

    SAFECPY(path, system);

    path_normalize(path);

    if (fs_exists(path))
        return path;

    /* Chop off directories until we have a match. */

    p = path;

    while ((p = path_next_sep(p)))
    {
        /* Skip separator. */

        p += 1;

        if (fs_exists(p))
            return p;
    }

    return NULL;
}
示例#2
0
/* NOTE: despite what you would expect, 'file_name' is actually a path.
 * With windoze style backlashes, ofc.
 */
static struct efi_file_handle *file_open(struct file_system *fs,
		struct file_handle *parent, s16 *file_name, u64 mode)
{
	struct file_handle *fh;
	char f0[MAX_UTF8_PER_UTF16] = {0};
	int plen = 0;
	int flen = 0;

	if (file_name) {
		utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
		flen = utf16_strlen((u16 *)file_name);
	}

	/* we could have a parent, but also an absolute path: */
	if (f0[0] == '\\') {
		plen = 0;
	} else if (parent) {
		plen = strlen(parent->path) + 1;
	}

	/* +2 is for null and '/' */
	fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);

	fh->base = efi_file_handle_protocol;
	fh->fs = fs;

	if (parent) {
		char *p = fh->path;

		if (plen > 0) {
			strcpy(p, parent->path);
			p += plen - 1;
			*p++ = '/';
		}

		utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);

		if (sanitize_path(fh->path))
			goto error;

		/* check if file exists: */
		if (set_blk_dev(fh))
			goto error;

		if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path)))
			goto error;

		/* figure out if file is a directory: */
		fh->isdir = is_dir(fh);
	} else {
		fh->isdir = 1;
		strcpy(fh->path, "");
	}

	return &fh->base;

error:
	free(fh);
	return NULL;
}
示例#3
0
static int
install_local_packages() {
  if (-1 == fs_exists("./package.json")) {
    logger_error("error", "Missing package.json");
    return 1;
  }

  char *json = fs_read("./package.json");
  if (NULL == json) return 1;

  clib_package_t *pkg = clib_package_new(json, opts.verbose);
  if (NULL == pkg) goto e1;

  int rc = clib_package_install_dependencies(pkg, opts.dir, opts.verbose);
  if (-1 == rc) goto e2;

  if (opts.dev) {
    rc = clib_package_install_development(pkg, opts.dir, opts.verbose);
    if (-1 == rc) goto e2;
  }

  free(json);
  clib_package_free(pkg);
  return 0;

e2:
  clib_package_free(pkg);
e1:
  free(json);
  return 1;
}
示例#4
0
int main() {
  response_t *res = http_get("http://google.com");
  assert(res);
  assert(res->data);
  assert(res->ok);
  assert(200 == res->status);

  assert(0 == http_get_file("http://google.com", "./google.html"));
  assert(0 == fs_exists("./google.html"));
  return 0;
}
示例#5
0
文件: main.c 项目: marcandrysco/Shim
bool test_fs_manip()
{
	char *str;

	printf("testing simple read/write... ");

	fs_writestr("testfile", "str-TEST");
	str = fs_readstr("testfile");
	if(!str_isequal(str, "str-TEST"))
		return printf("failed\n"), false;

	mem_free(str);

	printf("okay\n");

	printf("testing file copy... ");

	fs_copy("copyfile", "testfile");
	str = fs_readstr("copyfile");
	if(!str_isequal(str, "str-TEST"))
		return printf("failed\n"), false;

	mem_free(str);

	printf("okay\n");

	printf("testing file deletion... ");

	fs_rmfile("testfile");
	fs_rmfile("copyfile");
	if(fs_exists("testfile") || fs_exists("copyfile"))
		return printf("failed\n"), false;

	printf("okay\n");

	return true;
}
示例#6
0
文件: fs.c 项目: liwei606/OS_proj
int process_request(const char *line, FILE *fp, FileSystem *fs) {
    char command[4096];
    
#ifdef DEBUG
    fprintf(stderr, "process request `%s`\n", line);
#endif
    sscanf(line, "%s", command);
#ifdef DEBUG
    fprintf(stderr, "command is `%s`\n", command);
#endif
    if (0 == strcmp("f", command)) {
        fs_format(fs);
        return RESULT_DONE;
    } else if (0 == strcmp("mk", command)) {
        char f[4096];
        char parent_path[4096];
        
        sscanf(line + 2, "%s", f);
#ifdef DEBUG
        fprintf(stderr, "> mk `%s`\n", f);
#endif
        if (fs_exists(fs, f)) {
#ifdef DEBUG
            fprintf(stderr, "> `%s` already exists\n", f);
#endif
            return RESULT_NO;
        } else {
#ifdef DEBUG
            fprintf(stderr, "> going to create `%s`\n", f);
#endif
        }
        fs_split_path(f, parent_path, NULL);
        if (fs_isdir(fs, parent_path)) {
            if (fs_create(fs, f)) {
#ifdef DEBUG
                fprintf(stderr, "> failed to create `%s`\n", f);
#endif
                return RESULT_NO;
            }
            return RESULT_YES;
        }
#ifdef DEBUG
        fprintf(stderr, "> parent path `%s` is not a directory\n", parent_path);
#endif
        return RESULT_NO;
    } else if (0 == strcmp("mkdir", command)) {
        char d[4096];
        char parent_path[4096];
        
        sscanf(line + 5, "%s", d);
        if (fs_exists(fs, d)) {
            return RESULT_NO;
        }
        fs_split_path(d, parent_path, NULL);
        if (fs_isdir(fs, parent_path)) {
            if (fs_mkdir(fs, d)) {
                return RESULT_NO;
            }
            return RESULT_YES;
        }
        return RESULT_NO;
    } else if (0 == strcmp("rm", command)) {
        char f[4096];
        
        sscanf(line + 2, "%s", f);
        if (fs_isfile(fs, f)) {
            if (fs_unlink(fs, f)) {
                return RESULT_NO;
            }
            return RESULT_YES;
        }
        return RESULT_NO;
    } else if (0 == strcmp("cd", command)) {
        char path[4096];
        
        sscanf(line + 2, "%s", path);
        if (fs_isdir(fs, path)) {
            if (fs_chdir(fs, path)) {
                return RESULT_NO;
            }
            return RESULT_YES;
        }
#ifdef DEBUG
        fprintf(stderr, "`%s` is not a directory\n", path);
#endif
        return RESULT_NO;
    } else if (0 == strcmp("rmdir", command)) {
        char d[4096];
        
        sscanf(line + 5, "%s", d);
        if (fs_isdir(fs, d)) {
            if (fs_rmdir(fs, d)) {
                return RESULT_NO;
            }
            return RESULT_YES;
        }
        return RESULT_NO;
    } else if (0 == strcmp("ls", command)) {
        fs_ls(fs, fp);
        return RESULT_ELSE;
    } else if (0 == strcmp("cat", command)) {
        char f[4096];
        
        sscanf(line + 3, "%s", f);
        if (fs_isfile(fs, f)) {
            fs_cat(fs, f, fp);
            return RESULT_ELSE;
        }
        return RESULT_NO;
    } else if (0 == strcmp("w", command)) {
        char f[4096];
        int l;
        char data[4096];
        
        sscanf(line + 1, "%s %d %[^\n]", f, &l, data);
        if (fs_isfile(fs, f)) {
            if (fs_write(fs, f, l, data)) {
                return RESULT_NO;
            }
            return RESULT_YES;
        }
        return RESULT_NO;
    } else if (0 == strcmp("i", command)) {
        char f[4096];
        int pos;
        int l;
        char data[4096];
        
        sscanf(line + 1, "%s %d %d %[^\n]", f, &pos, &l, data);
        if (fs_isfile(fs, f)) {
            if (fs_insert(fs, f, pos, l, data)) {
                return RESULT_NO;
            }
            return RESULT_YES;
        }
        return RESULT_NO;
    } else if (0 == strcmp("d", command)) {
        char f[4096];
        int pos;
        int l;
        
        sscanf(line + 1, "%s %d %d", f, &pos, &l);
        if (fs_isfile(fs, f)) {
            if (fs_delete(fs, f, pos, l)) {
                return RESULT_NO;
            }
            return RESULT_YES;
        }
        return RESULT_NO;
    } else if (0 == strcmp("e", command)) {
        return RESULT_EXIT;
    }
    return RESULT_ELSE;
}
示例#7
0
文件: main.c 项目: qute/json
int
main (int argc, char **argv) {
  json_value_t *value = NULL;
  json_value_t *root = NULL;
  char *filename = NULL;
  char *key = NULL;
  char *src = NULL;
  int i = 0;

#define usage() printf("usage: %s <file> [key]\n", argv[0]);

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

  // parse opts
  for (i = 1; i < argc; ++i) {
    if (EQ(argv[i], "--each")) {
      each = 1;
    } else if (EQ(argv[i], "--stream")) {
      stream = 1;
    } else if (EQ(argv[i], "--help")) {
      usage();
      return 0;
    }
  }

  if (ferror(stdin)) {
    return 1;
  } else if (0 == isatty(0)) {
    filename = "<stdin>";
    tty = 1;
    if (argc > 1 && '-' != argv[1][0]) {
      key = argv[1];
    }
  } else {
    filename = argv[1];
    if (0 != fs_exists(filename)) {
      printf("E: not found - `%s'\n", filename);
      return 1;
    }

    src = fs_read(filename);
    if (NULL == src) {
      return 1;
    }

    if (argc > 2 && '-' !=  argv[2][0]) {
      key = argv[2];
    }
  }

  do {
    if (tty) {
      src = read_stdin();
    }

    if (NULL == src || 0 == strlen(src)) {
      break;
    }

    // proxy source if just streaming stdin
    // without a key lookup
    if (NULL == key && stream) {
      printf("%s", src);
      continue;
    }

    root = json_parse(filename, src);

    if (NULL == root) {
      return 1;
    } else if (root->errno) {
      json_perror(root);
      return 1;
    }

    if (NULL != key) {
      value = json_get(root, key);
      if (NULL == value) {
        return 1;
      }

      free(root);
      root = value;
    } else {
      value = root;
    }

    if (1 == each && JSON_ARRAY == value->type) {
      value = value->values[0];
      while (value) {
        printf("%s\n", json_stringify(value));
        value = value->next;
      }
    } else {
      printf("%s\n", json_stringify(value));
    }

    json_destroy(root);
    free(src);
    value = NULL;
    root = NULL;
    src = NULL;

  } while (tty);

  return 0;
}