示例#1
0
文件: main.c 项目: andyvand/fusehfs
/*
 * NAME:	main()
 * DESCRIPTION:	program entry
 */
int main(int argc, char *argv[])
{
  char *path;
  int nparts, pnum, result;
  hfsvol vol;

  suid_init();

  if (argc == 2)
    {
      if (strcmp(argv[1], "--version") == 0)
	{
	  printf("%s - %s\n", hfsutils_version, hfsutils_copyright);
	  printf("`%s --license' for licensing information.\n", argv[0]);
	  return 0;
	}
      else if (strcmp(argv[1], "--license") == 0)
	{
	  printf("\n%s", hfsutils_license);
	  return 0;
	}
    }

  options = HFSCK_REPAIR;

  while (1)
    {
      int opt;

      opt = getopt(argc, argv, "vna");
      if (opt == EOF)
	break;

      switch (opt)
	{
	case '?':
	  return usage(argv);

	case 'v':
	  options |= HFSCK_VERBOSE;
	  break;

	case 'n':
	  options &= ~HFSCK_REPAIR;
	  break;

	case 'a':
	  options |= HFSCK_YES;
	  break;
	}
    }

  if (argc - optind < 1 ||
      argc - optind > 2)
    return usage(argv);

  path = argv[optind];

  suid_enable();
  nparts = hfs_nparts(path);
  suid_disable();

  if (nparts == 0)
    {
      fprintf(stderr, "%s: partitioned medium contains no HFS partitions\n",
	      argv[0]);
      return 1;
    }

  if (argc - optind == 2)
    {
      pnum = atoi(argv[optind + 1]);

      if (pnum < 0)
	{
	  fprintf(stderr, "%s: invalid partition number\n", argv[0]);
	  return 1;
	}

      if (nparts == -1 && pnum > 0)
	{
	  fprintf(stderr, "%s: warning: ignoring partition number for"
		  " non-partitioned medium\n", argv[0]);
	  pnum = 0;
	}
      else if (nparts > 0 && pnum == 0)
	{
	  fprintf(stderr, "%s: cannot specify whole medium"
		  " (has %d partition%s)\n", argv[0], nparts,
		  nparts == 1 ? "" : "s");
	  return 1;
	}
      else if (nparts > 0 && pnum > nparts)
	{
	  fprintf(stderr, "%s: invalid partition number (only %d available)\n",
		  argv[0], nparts);
	  return 1;
	}
    }
  else
    {
      if (nparts > 1)
	{
	  fprintf(stderr, "%s: must specify partition number (%d available)\n",
		  argv[0], nparts);
	  return 1;
	}
      else if (nparts == -1)
	pnum = 0;
      else
	pnum = 1;
    }

  v_init(&vol, HFS_OPT_NOCACHE);

  if (REPAIR)
    {
      suid_enable();
      result = v_open(&vol, path, HFS_MODE_RDWR);
      suid_disable();

      if (result == -1)
	{
	  vol.flags |= HFS_VOL_READONLY;

	  suid_enable();
	  result = v_open(&vol, path, HFS_MODE_RDONLY);
	  suid_disable();
	}
    }

  if (result == -1)
    {
      perror(path);
      return 1;
    }

  if (REPAIR && (vol.flags & HFS_VOL_READONLY))
    {
      fprintf(stderr, "%s: warning: %s not writable; cannot repair\n",
	      argv[0], path);

      options &= ~HFSCK_REPAIR;
    }

  if (v_geometry(&vol, pnum) == -1 ||
      l_getmdb(&vol, &vol.mdb, 0) == -1)
    {
      perror(path);
      v_close(&vol);
      return 1;
    }

  result = hfsck(&vol);

  vol.flags |= HFS_VOL_MOUNTED;

  if (v_close(&vol) == -1)
    {
      perror("closing volume");
      return 1;
    }

  return result;
}
示例#2
0
/*
 * NAME:	hformat->main()
 * DESCRIPTION:	implement hformat command
 */
int hformat_main(int argc, char *argv[])
{
    const char *vname, *path;
    hfsvol *vol;
    hfsvolent ent;
    int nparts, partno, options = 0, result = 0;

    vname = "Untitled";

    while (1)
    {
        int opt;

        opt = getopt(argc, argv, "fl:");
        if (opt == EOF)
            break;

        switch (opt)
        {
        case '?':
            return usage();

        case 'f':
            options |= O_FORCE;
            break;

        case 'l':
            vname = optarg;
            break;
        }
    }

    if (argc - optind < 1 || argc - optind > 2)
        return usage();

    path = argv[optind];

    nparts = hfs_nparts(path);

    if (argc - optind == 2)
    {
        partno = atoi(argv[optind + 1]);

        if (nparts != -1 && partno == 0)
        {
            if (options & O_FORCE)
            {
                fprintf(stderr,
                        "%s: warning: erasing partition information\n", argv0);
            }
            else
            {
                fprintf(stderr, "%s: medium is partitioned; "
                        "select partition > 0 or use -f\n", argv0);
                return 1;
            }
        }
    }
    else
    {
        if (nparts > 1)
        {
            fprintf(stderr,
                    "%s: must specify partition number (%d available)\n",
                    argv0, nparts);
            return 1;
        }
        else if (nparts == -1)
            partno = 0;
        else
            partno = 1;
    }

    vol = do_format(path, partno, vname);
    if (vol == 0)
    {
        hfsutil_perror(path);
        return 1;
    }

    hfs_vstat(vol, &ent);
    hfsutil_pinfo(&ent);

    if (hcwd_mounted(ent.name, ent.crdate, path, partno) == -1)
    {
        perror("Failed to record mount");
        result = 1;
    }

    hfsutil_unmount(vol, &result);

    return result;
}