1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * devname.c --- Support function to translate a user provided string
3*6a54128fSAndroid Build Coastguard Worker * identifying a device to an actual device path
4*6a54128fSAndroid Build Coastguard Worker *
5*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 2022 Red Hat, Inc., Lukas Czerner <[email protected]>
6*6a54128fSAndroid Build Coastguard Worker *
7*6a54128fSAndroid Build Coastguard Worker * %Begin-Header%
8*6a54128fSAndroid Build Coastguard Worker * This file may be redistributed under the terms of the GNU Public
9*6a54128fSAndroid Build Coastguard Worker * License.
10*6a54128fSAndroid Build Coastguard Worker * %End-Header%
11*6a54128fSAndroid Build Coastguard Worker */
12*6a54128fSAndroid Build Coastguard Worker
13*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
14*6a54128fSAndroid Build Coastguard Worker #include <string.h>
15*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
16*6a54128fSAndroid Build Coastguard Worker
17*6a54128fSAndroid Build Coastguard Worker #include "config.h"
18*6a54128fSAndroid Build Coastguard Worker #include "devname.h"
19*6a54128fSAndroid Build Coastguard Worker #include "nls-enable.h"
20*6a54128fSAndroid Build Coastguard Worker
21*6a54128fSAndroid Build Coastguard Worker /*
22*6a54128fSAndroid Build Coastguard Worker * blkid_get_devname() is primarily intended for parsing "NAME=value"
23*6a54128fSAndroid Build Coastguard Worker * tokens. It will return the device matching the specified token, NULL if
24*6a54128fSAndroid Build Coastguard Worker * nothing is found, or copy of the string if it's not in "NAME=value"
25*6a54128fSAndroid Build Coastguard Worker * format.
26*6a54128fSAndroid Build Coastguard Worker * get_devname() takes the same parameters and works the same way as
27*6a54128fSAndroid Build Coastguard Worker * blkid_get_devname() except it can handle '=' in the file name.
28*6a54128fSAndroid Build Coastguard Worker */
get_devname(blkid_cache cache,const char * token,const char * value)29*6a54128fSAndroid Build Coastguard Worker char *get_devname(blkid_cache cache, const char *token, const char *value)
30*6a54128fSAndroid Build Coastguard Worker {
31*6a54128fSAndroid Build Coastguard Worker int is_file = 0;
32*6a54128fSAndroid Build Coastguard Worker char *ret = NULL;
33*6a54128fSAndroid Build Coastguard Worker
34*6a54128fSAndroid Build Coastguard Worker if (!token)
35*6a54128fSAndroid Build Coastguard Worker goto out;
36*6a54128fSAndroid Build Coastguard Worker
37*6a54128fSAndroid Build Coastguard Worker if (value) {
38*6a54128fSAndroid Build Coastguard Worker ret = blkid_get_devname(cache, token, value);
39*6a54128fSAndroid Build Coastguard Worker goto out;
40*6a54128fSAndroid Build Coastguard Worker }
41*6a54128fSAndroid Build Coastguard Worker
42*6a54128fSAndroid Build Coastguard Worker if (access(token, F_OK) == 0)
43*6a54128fSAndroid Build Coastguard Worker is_file = 1;
44*6a54128fSAndroid Build Coastguard Worker
45*6a54128fSAndroid Build Coastguard Worker ret = blkid_get_devname(cache, token, NULL);
46*6a54128fSAndroid Build Coastguard Worker if (ret) {
47*6a54128fSAndroid Build Coastguard Worker /*
48*6a54128fSAndroid Build Coastguard Worker * In case of collision prefer the result from
49*6a54128fSAndroid Build Coastguard Worker * blkid_get_devname() to avoid a file masking file system with
50*6a54128fSAndroid Build Coastguard Worker * existing tag.
51*6a54128fSAndroid Build Coastguard Worker */
52*6a54128fSAndroid Build Coastguard Worker if (is_file && (strcmp(ret, token) != 0)) {
53*6a54128fSAndroid Build Coastguard Worker fprintf(stderr,
54*6a54128fSAndroid Build Coastguard Worker _("Collision found: '%s' refers to both '%s' "
55*6a54128fSAndroid Build Coastguard Worker "and a file '%s'. Using '%s'!\n"),
56*6a54128fSAndroid Build Coastguard Worker token, ret, token, ret);
57*6a54128fSAndroid Build Coastguard Worker }
58*6a54128fSAndroid Build Coastguard Worker goto out;
59*6a54128fSAndroid Build Coastguard Worker }
60*6a54128fSAndroid Build Coastguard Worker
61*6a54128fSAndroid Build Coastguard Worker if (is_file)
62*6a54128fSAndroid Build Coastguard Worker ret = strdup(token);
63*6a54128fSAndroid Build Coastguard Worker out:
64*6a54128fSAndroid Build Coastguard Worker return ret;
65*6a54128fSAndroid Build Coastguard Worker }
66