File: mapdevfs.c

package info (click to toggle)
autopartkit 1.07
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,060 kB
  • ctags: 198
  • sloc: ansic: 2,489; sh: 843; makefile: 103
file content (146 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 *  map devfs names to old-style names
 *
 */

#define _GNU_SOURCE /* Make asprintf available */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>

#include "autopartkit.h"

/* copied from libdebian-installer, there it is named di_mapdevfs() */
ssize_t
mapdevfs(const char *path, char *buf, size_t n)
{
  static struct entry
  {
    unsigned char major;
    unsigned char minor;
    char *name;
    enum { ENTRY_TYPE_ONE, ENTRY_TYPE_NUMBER, ENTRY_TYPE_DISC } type;
    unsigned char entry_first;
    unsigned char entry_disc_minor_shift;
  }
  entries[] =
  {
    /*
       major	minor	name		type           entry_first entry_disc_minor_shift */
    { 2,	0,	"fd",		ENTRY_TYPE_NUMBER,	0,	0 },
    { 3,	0,	"hd",		ENTRY_TYPE_DISC,	0,	6 },
    { 4,	64,	"ttyS",		ENTRY_TYPE_NUMBER,	0,	6 },
    { 4,	0,	"tty",		ENTRY_TYPE_NUMBER,	0,	6 },
    { 8,	0,	"sd",		ENTRY_TYPE_DISC,	0,	4 },
    { 9,	0,      "md",           ENTRY_TYPE_NUMBER,      0,      0 },
    { 22,	0,	"hd",		ENTRY_TYPE_DISC,	2,	6 },
    { 33,	0,	"hd",		ENTRY_TYPE_DISC,	4,	6 },
    { 34,	0,	"hd",		ENTRY_TYPE_DISC,	6,	6 },
    { 56,	0,	"hd",		ENTRY_TYPE_DISC,	8,	6 },
    { 57,	0,	"hd",		ENTRY_TYPE_DISC,	10,	6 },
    { 65,	0,	"sd",		ENTRY_TYPE_DISC,	16,	4 },
    { 66,	0,	"sd",		ENTRY_TYPE_DISC,	32,	4 },
    { 67,	0,	"sd",		ENTRY_TYPE_DISC,	48,	4 },
    { 68,	0,	"sd",		ENTRY_TYPE_DISC,	64,	4 },
    { 69,	0,	"sd",		ENTRY_TYPE_DISC,	80,	4 },
    { 70,	0,	"sd",		ENTRY_TYPE_DISC,	96,	4 },
    { 71,	0,	"sd",		ENTRY_TYPE_DISC,	112,	4 },
    { 88,	0,	"hd",		ENTRY_TYPE_DISC,	12,	6 },
    { 89,	0,	"hd",		ENTRY_TYPE_DISC,	14,	6 },
    { 90,	0,	"hd",		ENTRY_TYPE_DISC,	16,	6 },
    { 91,	0,	"hd",		ENTRY_TYPE_DISC,	18,	6 },
    { 94,	0,	"dasd",		ENTRY_TYPE_DISC,	0,	2 },
    { 0,	0,	NULL,		ENTRY_TYPE_ONE,		0,	0 },
  };

  struct stat s;
  struct entry *e;

  ssize_t ret = 0;

  unsigned int unit;
  unsigned int part;

  if (!strcmp("none", path))
    return snprintf(buf, n, "%s", path);

  if (stat(path,&s) == -1)
    return 0;

  e = entries;
  while (e->name != NULL) {
    if (major(s.st_rdev) == e->major && 
        ((e->type == ENTRY_TYPE_ONE && minor(s.st_rdev) == e->minor) ||
         (e->type != ENTRY_TYPE_ONE && minor(s.st_rdev) >= e->minor))) {
      break;
    }
    e++;
  }
  if ( ! e->name )
    /* Pass unknown devices on without changes.  This fixes LVM devices */
    return snprintf(buf, n, "%s", path);

  switch (e->type)
  {
    case ENTRY_TYPE_ONE:
      ret = snprintf(buf, n, "/dev/%s", e->name);
      break;

    case ENTRY_TYPE_NUMBER:
      unit = minor(s.st_rdev) - e->minor + e->entry_first;

      ret = snprintf(buf, n, "/dev/%s%d", e->name, unit);
      break;

    case ENTRY_TYPE_DISC:
      unit = (minor(s.st_rdev) >> e->entry_disc_minor_shift);
      part = (minor(s.st_rdev) & ((1 << e->entry_disc_minor_shift) -1 ));

      unit += e->entry_first;

      if (unit+'a' > 'z') {
        unit -= 26;
        ret = snprintf(buf, n, "/dev/%s%c%c", e->name, 'a' + unit / 26, 'a' + unit % 26);
      }
      else
        ret = snprintf(buf, n, "/dev/%s%c", e->name, 'a' + unit);

      if (part)
        ret = snprintf(buf + ret, n - ret, "%d", part);

      break;
  };

  return ret;
}

/* Returns the devfs path name normalized into a "normal" (hdaX, sdaX)
 * name.  The return value is malloced, which means that the caller
 * needs to free it.  This is kinda hairy, but I don't see any other
 * way of operating with non-devfs systems when we use devfs on the
 * boot medium.  The numbers are taken from devices.txt in the
 * Documentation subdirectory off the kernel sources.
 */

char *normalize_devfs(const char* path)
{
    char buf[1024];
    if (0 == mapdevfs(path, buf, sizeof(buf)))
      return NULL;
    return strdup(buf);
}

#if AS_PROGRAM
int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "Wrong number of args: mapdevfs <path>\n");
        return 1;
    }
    printf("%s\n", normalize_devfs(argv[1]));
    return 0;
}
#endif