xref: /freebsd/sbin/fsck_ffs/utilities.c (revision 32e86a82f54826f14ea381affa6674db3aa3b5ae)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes  * are met:
108fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes  *    without specific prior written permission.
188fae3551SRodney W. Grimes  *
198fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes  * SUCH DAMAGE.
308fae3551SRodney W. Grimes  */
318fae3551SRodney W. Grimes 
328fae3551SRodney W. Grimes #include <sys/param.h>
339ea6f4f0SAdrian Chadd #include <sys/types.h>
349ea6f4f0SAdrian Chadd #include <sys/stat.h>
35780a5c1eSPeter Wemm 
368fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
378fae3551SRodney W. Grimes #include <ufs/ufs/dir.h>
388fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
3951a5cf90SBruce Evans 
409ea6f4f0SAdrian Chadd #include <errno.h>
4151a5cf90SBruce Evans #include <string.h>
429ea6f4f0SAdrian Chadd #include <fstab.h>
4315fca934SKirk McKusick #include <paths.h>
449ea6f4f0SAdrian Chadd #include <stdio.h>
45780a5c1eSPeter Wemm 
468fae3551SRodney W. Grimes #include "fsck.h"
478fae3551SRodney W. Grimes 
489ea6f4f0SAdrian Chadd 
499ea6f4f0SAdrian Chadd char *
blockcheck(char * origname)50b70cd7eeSWarner Losh blockcheck(char *origname)
519ea6f4f0SAdrian Chadd {
52bf58d635SIan Dowse 	struct stat stblock;
53bf58d635SIan Dowse 	char *newname, *cp;
549ea6f4f0SAdrian Chadd 	struct fstab *fsinfo;
559ea6f4f0SAdrian Chadd 	int retried = 0, len;
5615fca934SKirk McKusick 	static char device[MAXPATHLEN];
579ea6f4f0SAdrian Chadd 
589ea6f4f0SAdrian Chadd 	newname = origname;
5915fca934SKirk McKusick 	if (stat(newname, &stblock) < 0) {
6015fca934SKirk McKusick 		cp = strrchr(newname, '/');
617d5e6562SPedro F. Giffuni 		if (cp == NULL) {
6215fca934SKirk McKusick 			(void)snprintf(device, sizeof(device), "%s%s",
6315fca934SKirk McKusick 				_PATH_DEV, newname);
6415fca934SKirk McKusick 			newname = device;
6515fca934SKirk McKusick 		}
6615fca934SKirk McKusick 	}
679ea6f4f0SAdrian Chadd retry:
689ea6f4f0SAdrian Chadd 	if (stat(newname, &stblock) < 0) {
699ea6f4f0SAdrian Chadd 		printf("Can't stat %s: %s\n", newname, strerror(errno));
709ea6f4f0SAdrian Chadd 		return (origname);
719ea6f4f0SAdrian Chadd 	}
729ea6f4f0SAdrian Chadd 	switch(stblock.st_mode & S_IFMT) {
739ea6f4f0SAdrian Chadd 	case S_IFCHR:
749ea6f4f0SAdrian Chadd 	case S_IFBLK:
759ea6f4f0SAdrian Chadd 		return(newname);
769ea6f4f0SAdrian Chadd 	case S_IFDIR:
779ea6f4f0SAdrian Chadd 		if (retried)
789ea6f4f0SAdrian Chadd 			break;
799ea6f4f0SAdrian Chadd 
809ea6f4f0SAdrian Chadd 		len = strlen(origname) - 1;
819ea6f4f0SAdrian Chadd 		if (len > 0 && origname[len] == '/')
829ea6f4f0SAdrian Chadd 			/* remove trailing slash */
839ea6f4f0SAdrian Chadd 			origname[len] = '\0';
849ea6f4f0SAdrian Chadd 		if ((fsinfo = getfsfile(origname)) == NULL) {
8570d8bdefSPoul-Henning Kamp 			printf(
8670d8bdefSPoul-Henning Kamp 			    "Can't resolve %s to character special device.\n",
879ea6f4f0SAdrian Chadd 			    origname);
8815fca934SKirk McKusick 			return (origname);
899ea6f4f0SAdrian Chadd 		}
909ea6f4f0SAdrian Chadd 		newname = fsinfo->fs_spec;
919ea6f4f0SAdrian Chadd 		retried++;
929ea6f4f0SAdrian Chadd 		goto retry;
939ea6f4f0SAdrian Chadd 	}
949ea6f4f0SAdrian Chadd 	/*
959ea6f4f0SAdrian Chadd 	 * Not a block or character device, just return name and
969ea6f4f0SAdrian Chadd 	 * let the user decide whether to use it.
979ea6f4f0SAdrian Chadd 	 */
989ea6f4f0SAdrian Chadd 	return (origname);
999ea6f4f0SAdrian Chadd }
1006db798caSIan Dowse 
101