2 #include <sys/utsname.h>
4 #include "superblocks.h"
5 #include "linux_version.h"
8 #define DEBUG(fmt, ...) printf(fmt, __VA_ARGS__)
10 #define DEBUG(fmt, ...)
13 int blkid_debug_mask
= 0;
15 static unsigned char *probe_buffer
;
17 int get_linux_version (void)
31 n
= sscanf(uts
.release
, "%d.%d.%d", &major
, &minor
, &teeny
);
35 return kver
= KERNEL_VERSION(major
, minor
, teeny
);
38 int blkid_probe_is_tiny(blkid_probe pr
)
44 int blkid_probe_set_value(blkid_probe pr
, const char *name
,
45 unsigned char *data
, size_t len
)
51 int blkid_probe_set_version(blkid_probe pr
, const char *version
)
53 int len
= strlen(version
);
54 if (len
> (sizeof(pr
->version
) - 1)) {
55 fprintf(stderr
, "version buffer too small %d\n", len
);
59 strncpy(pr
->version
, version
, sizeof(pr
->version
));
64 int blkid_probe_sprintf_version(blkid_probe pr
, const char *fmt
, ...)
70 n
= vsnprintf(pr
->version
, sizeof(pr
->version
), fmt
, ap
);
73 if (n
>= sizeof(pr
->version
))
74 fprintf(stderr
, "version buffer too small %d\n", n
);
79 unsigned char *blkid_probe_get_buffer(blkid_probe pr
,
80 blkid_loff_t off
, blkid_loff_t len
)
85 fprintf(stderr
, "probe buffer too small %d\n", (int) len
);
90 probe_buffer
= malloc(4096);
91 memset(probe_buffer
, 0, 4096);
94 lseek(pr
->fd
, off
, SEEK_SET
);
95 ret
= read(pr
->fd
, probe_buffer
, len
);
98 fprintf(stderr
, "faile to read blkid\n");
103 int blkid_probe_set_label(blkid_probe pr
, unsigned char *label
, size_t len
)
105 if (len
> (sizeof(pr
->label
) - 1)) {
106 fprintf(stderr
, "label buffer too small %d > %d\n",
107 (int) len
, (int) sizeof(pr
->label
) - 1);
110 memcpy(pr
->label
, label
, len
+ 1);
115 int blkid_probe_set_uuid_as(blkid_probe pr
, unsigned char *uuid
, const char *name
)
117 short unsigned int*u
= (short unsigned int*) uuid
;
121 "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
122 be16_to_cpu(u
[0]), be16_to_cpu(u
[1]), be16_to_cpu(u
[2]), be16_to_cpu(u
[3]),
123 be16_to_cpu(u
[4]), be16_to_cpu(u
[5]), be16_to_cpu(u
[6]), be16_to_cpu(u
[7]));
125 strncpy(pr
->name
, name
, sizeof(pr
->name
));
130 int blkid_probe_set_uuid(blkid_probe pr
, unsigned char *uuid
)
132 return blkid_probe_set_uuid_as(pr
, uuid
, NULL
);
135 int blkid_probe_sprintf_uuid(blkid_probe pr
, unsigned char *uuid
,
136 size_t len
, const char *fmt
, ...)
141 vsnprintf(pr
->uuid
, sizeof(pr
->uuid
), fmt
, ap
);
147 static const struct blkid_idinfo
*idinfos
[] =
161 int probe_block(char *block
, struct blkid_struct_probe
*pr
)
166 if (stat(block
, &s
) || !S_ISBLK(s
.st_mode
))
170 pr
->fd
= open(block
, O_RDONLY
);
174 for (i
= 0; i
< ARRAY_SIZE(idinfos
); i
++) {
175 /* loop over all magic handlers */
176 const struct blkid_idmag
*mag
;
178 /* loop over all probe handlers */
179 DEBUG("scanning %s\n", idinfos
[i
]->name
);
181 mag
= &idinfos
[i
]->magics
[0];
184 int off
= (mag
->kboff
* 1024) + mag
->sboff
;
185 char magic
[32] = { 0 };
187 lseek(pr
->fd
, off
, SEEK_SET
);
188 read(pr
->fd
, magic
, mag
->len
);
190 DEBUG("magic: %s %s %d\n", mag
->magic
, magic
, mag
->len
);
191 if (!memcmp(mag
->magic
, magic
, mag
->len
))
196 if (mag
&& mag
->magic
) {
197 DEBUG("probing %s\n", idinfos
[i
]->name
);
198 pr
->err
= idinfos
[i
]->probefunc(pr
, mag
);
200 strcpy(pr
->dev
, block
);