示例#1
0
int
main(int argc, char *argv[])
{
	struct fstab *fs;
	int i, rval = 0;
	const char *vfstype = NULL;
	char globopt[3];
	const char *etc_fstab;

	globopt[0] = '-';
	globopt[2] = '\0';

	TAILQ_INIT(&selhead);
	TAILQ_INIT(&opthead);

	etc_fstab = NULL;
	while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1)
		switch (i) {
		case 'B':
			if (flags & CHECK_BACKGRD)
				errx(1, "Cannot specify -B and -F.");
			flags |= DO_BACKGRD;
			break;

		case 'd':
			flags |= CHECK_DEBUG;
			break;

		case 'v':
			flags |= CHECK_VERBOSE;
			break;

		case 'F':
			if (flags & DO_BACKGRD)
				errx(1, "Cannot specify -B and -F.");
			flags |= CHECK_BACKGRD;
			break;

		case 'p':
			flags |= CHECK_PREEN;
			/*FALLTHROUGH*/
		case 'C':
			flags |= CHECK_CLEAN;
			/*FALLTHROUGH*/
		case 'n':
		case 'y':
			globopt[1] = i;
			catopt(&options, globopt);
			break;

		case 'f':
			forceflag = 1;
			globopt[1] = i;
			catopt(&options, globopt);
			break;

		case 'l':
			warnx("Ignoring obsolete -l option\n");
			break;

		case 'T':
			if (*optarg)
				addoption(optarg);
			break;

		case 't':
			if (!TAILQ_EMPTY(&selhead))
				errx(1, "only one -t option may be specified.");

			maketypelist(optarg);
			vfstype = optarg;
			break;

		case 'c':
			etc_fstab = optarg;
			break;

		case '?':
		default:
			usage();
			/* NOTREACHED */
		}

	argc -= optind;
	argv += optind;

	if (etc_fstab != NULL)
		setfstab(etc_fstab);

	if (argc == 0)
		return checkfstab(flags, isok, checkfs);

#define	BADTYPE(type)							\
	(strcmp(type, FSTAB_RO) &&					\
	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))


	for (; argc--; argv++) {
		const char *spec, *mntpt, *type, *cp;
		char device[MAXPATHLEN];
		struct statfs *mntp;

		mntpt = NULL;
		spec = *argv;
		cp = strrchr(spec, '/');
		if (cp == 0) {
			(void)snprintf(device, sizeof(device), "%s%s",
				_PATH_DEV, spec);
			spec = device;
		}
		mntp = getmntpt(spec);
		if (mntp != NULL) {
			spec = mntp->f_mntfromname;
			mntpt = mntp->f_mntonname;
		}
		if ((fs = getfsfile(spec)) == NULL &&
		    (fs = getfsspec(spec)) == NULL) {
			if (vfstype == NULL)
				vfstype = getfslab(spec);
			if (vfstype == NULL)
				errx(1, "Could not determine filesystem type");
			type = vfstype;
			devcheck(spec);
		} else {
			spec = fs->fs_spec;
			type = fs->fs_vfstype;
			mntpt = fs->fs_file;
			if (BADTYPE(fs->fs_type))
				errx(1, "%s has unknown file system type.",
				    spec);
		}
		if ((flags & CHECK_BACKGRD) &&
		    checkfs(type, spec, mntpt, "-F", NULL) == 0) {
			printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
			continue;
		}
		if ((flags & DO_BACKGRD) && forceflag == 0 &&
		    checkfs(type, spec, mntpt, "-F", NULL) != 0)
			continue;

		rval |= checkfs(type, spec, mntpt, NULL, NULL);
	}

	return rval;
}
示例#2
0
文件: xfile.c 项目: npe9/harvey
Xfs *
getxfs(char *user, char *name)
{
	Xfs *xf, *fxf;
	Dir *dir;
	Qid dqid;
	char *p, *q;
	int32_t offset;
	int fd, omode;

	USED(user);
	if(name==nil || name[0]==0)
		name = deffile;
	if(name == nil){
		errno = Enofilsys;
		return 0;
	}

	/*
	 * If the name passed is of the form 'name:offset' then
	 * offset is used to prime xf->offset. This allows accessing
	 * a FAT-based filesystem anywhere within a partition.
	 * Typical use would be to mount a filesystem in the presence
	 * of a boot manager programme at the beginning of the disc.
	 */
	offset = 0;
	if(p = strrchr(name, ':')){
		*p++ = 0;
		offset = strtol(p, &q, 0);
		chat("name %s, offset %ld\n", p, offset);
		if(offset < 0 || p == q){
			errno = Enofilsys;
			return 0;
		}
		offset *= Sectorsize;
	}

	if(readonly)
		omode = OREAD;
	else
		omode = ORDWR;
	fd = open(name, omode);

	if(fd < 0 && omode==ORDWR){
		omode = OREAD;
		fd = open(name, omode);
	}

	if(fd < 0){
		chat("can't open %s: %r\n", name);
		errno = Eerrstr;
		return 0;
	}
	dir = dirfstat(fd);
	if(dir == nil){
		errno = Eio;
		close(fd);
		return 0;
	}

	dqid = dir->qid;
	free(dir);
	mlock(&xlock);
	for(fxf=0,xf=xhead; xf; xf=xf->next){
		if(xf->ref == 0){
			if(fxf == 0)
				fxf = xf;
			continue;
		}
		if(!eqqid(xf->qid, dqid))
			continue;
		if(strcmp(xf->name, name) != 0 || xf->dev < 0)
			continue;
		if(devcheck(xf) < 0) /* look for media change */
			continue;
		if(offset && xf->offset != offset)
			continue;
		chat("incref \"%s\", dev=%d...", xf->name, xf->dev);
		++xf->ref;
		unmlock(&xlock);
		close(fd);
		return xf;
	}
	if(fxf == nil){
		fxf = malloc(sizeof(Xfs));
		if(fxf == nil){
			unmlock(&xlock);
			close(fd);
			errno = Enomem;
			return nil;
		}
		fxf->next = xhead;
		xhead = fxf;
	}
	chat("alloc \"%s\", dev=%d...", name, fd);
	fxf->name = strdup(name);
	fxf->ref = 1;
	fxf->qid = dqid;
	fxf->dev = fd;
	fxf->fmt = 0;
	fxf->offset = offset;
	fxf->ptr = nil;
	fxf->isfat32 = 0;
	fxf->omode = omode;
	unmlock(&xlock);
	return fxf;
}