venus: Remove dedebug
[openafs.git] / src / vfsck / dirutils.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <afs/afsint.h>
16 #include <afs/ihandle.h>
17 #include <afs/partition.h>
18
19 /* ensure that we don't have a "/" instead of a "/dev/rxd0a" type of device.
20  * Overwrites abuffer with the corrected name.
21  */
22 int
23 EnsureDevice(char *abuffer)
24 {
25     struct dirent *dp;
26     char pbuffer[128];
27     struct stat tstat;
28     DIR *dirp;
29     char *dirName, dirn[100];
30     int code, i;
31     short dev;
32
33     code = stat(abuffer, &tstat);
34     if (code)
35         return code;
36     if (((tstat.st_mode & S_IFMT) == S_IFBLK)
37         || ((tstat.st_mode & S_IFMT) == S_IFCHR)) {
38         return 0;               /* already a block or char device */
39     }
40     /* otherwise, assume we've got a normal file, and we look up its device */
41     dev = tstat.st_dev;         /* remember device for this file */
42
43     /* now, look in /dev for the appropriate file */
44     dirp = opendir(dirName = AFS_DSKDEV);
45     while (dp = readdir(dirp)) {
46         strcpy(pbuffer, dirName);
47         strcat(pbuffer, "/");
48         strcat(pbuffer, dp->d_name);
49         if (stat(pbuffer, &tstat) != -1 && (tstat.st_mode & S_IFMT) == S_IFBLK
50             && (tstat.st_rdev == dev)) {
51             strcpy(abuffer, pbuffer);
52             closedir(dirp);
53             return 0;
54         }
55     }
56     closedir(dirp);
57
58     return 1;                   /* failed */
59 }