volude_id: remove unused fields and functions which were setting them
[oweals/busybox.git] / util-linux / volume_id / volume_id.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include "volume_id_internal.h"
22
23 typedef int (*raid_probe_fptr)(struct volume_id *id, uint64_t off, uint64_t size);
24 typedef int (*probe_fptr)(struct volume_id *id, uint64_t off);
25
26 static const raid_probe_fptr raid1[] = {
27 #if ENABLE_FEATURE_VOLUMEID_LINUXRAID
28         volume_id_probe_linux_raid,
29 #endif
30 #if ENABLE_FEATURE_VOLUMEID_ISWRAID
31         volume_id_probe_intel_software_raid,
32 #endif
33 #if ENABLE_FEATURE_VOLUMEID_LSIRAID
34         volume_id_probe_lsi_mega_raid,
35 #endif
36 #if ENABLE_FEATURE_VOLUMEID_VIARAID
37         volume_id_probe_via_raid,
38 #endif
39 #if ENABLE_FEATURE_VOLUMEID_SILICONRAID
40         volume_id_probe_silicon_medley_raid,
41 #endif
42 #if ENABLE_FEATURE_VOLUMEID_NVIDIARAID
43         volume_id_probe_nvidia_raid,
44 #endif
45 #if ENABLE_FEATURE_VOLUMEID_PROMISERAID
46         volume_id_probe_promise_fasttrack_raid,
47 #endif
48 #if ENABLE_FEATURE_VOLUMEID_HIGHPOINTRAID
49         volume_id_probe_highpoint_45x_raid,
50 #endif
51 };
52
53 static const probe_fptr raid2[] = {
54 #if ENABLE_FEATURE_VOLUMEID_LVM
55         volume_id_probe_lvm1,
56         volume_id_probe_lvm2,
57 #endif
58 #if ENABLE_FEATURE_VOLUMEID_HIGHPOINTRAID
59         volume_id_probe_highpoint_37x_raid,
60 #endif
61 #if ENABLE_FEATURE_VOLUMEID_LUKS
62         volume_id_probe_luks,
63 #endif
64 };
65
66 /* signature in the first block, only small buffer needed */
67 static const probe_fptr fs1[] = {
68 #if ENABLE_FEATURE_VOLUMEID_FAT
69         volume_id_probe_vfat,
70 #endif
71 // This one only looks for partitions, we don't use it
72 //#if ENABLE_FEATURE_VOLUMEID_MAC
73 //      volume_id_probe_mac_partition_map,
74 //#endif
75 #if ENABLE_FEATURE_VOLUMEID_XFS
76         volume_id_probe_xfs,
77 #endif
78 };
79
80 /* fill buffer with maximum */
81 static const probe_fptr fs2[] = {
82 #if ENABLE_FEATURE_VOLUMEID_LINUXSWAP
83         volume_id_probe_linux_swap,
84 #endif
85 #if ENABLE_FEATURE_VOLUMEID_EXT
86         volume_id_probe_ext,
87 #endif
88 #if ENABLE_FEATURE_VOLUMEID_REISERFS
89         volume_id_probe_reiserfs,
90 #endif
91 #if ENABLE_FEATURE_VOLUMEID_JFS
92         volume_id_probe_jfs,
93 #endif
94 #if ENABLE_FEATURE_VOLUMEID_UDF
95         volume_id_probe_udf,
96 #endif
97 #if ENABLE_FEATURE_VOLUMEID_ISO9660
98         volume_id_probe_iso9660,
99 #endif
100 #if ENABLE_FEATURE_VOLUMEID_HFS
101         volume_id_probe_hfs_hfsplus,
102 #endif
103 #if ENABLE_FEATURE_VOLUMEID_UFS
104         volume_id_probe_ufs,
105 #endif
106 #if ENABLE_FEATURE_VOLUMEID_NTFS
107         volume_id_probe_ntfs,
108 #endif
109 #if ENABLE_FEATURE_VOLUMEID_CRAMFS
110         volume_id_probe_cramfs,
111 #endif
112 #if ENABLE_FEATURE_VOLUMEID_ROMFS
113         volume_id_probe_romfs,
114 #endif
115 #if ENABLE_FEATURE_VOLUMEID_HPFS
116         volume_id_probe_hpfs,
117 #endif
118 #if ENABLE_FEATURE_VOLUMEID_SYSV
119         volume_id_probe_sysv,
120 #endif
121 #if ENABLE_FEATURE_VOLUMEID_MINIX
122         volume_id_probe_minix,
123 #endif
124 #if ENABLE_FEATURE_VOLUMEID_OCFS2
125         volume_id_probe_ocfs2,
126 #endif
127 };
128
129 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
130 {
131         int i;
132
133         if (id == NULL)
134                 return -EINVAL;
135
136         /* probe for raid first, cause fs probes may be successful on raid members */
137         if (size) {
138                 for (i = 0; i < ARRAY_SIZE(raid1); i++)
139                         if (raid1[i](id, off, size) == 0)
140                                 goto ret;
141         }
142
143         for (i = 0; i < ARRAY_SIZE(raid2); i++)
144                 if (raid2[i](id, off) == 0)
145                         goto ret;
146
147         /* signature in the first block, only small buffer needed */
148         for (i = 0; i < ARRAY_SIZE(fs1); i++)
149                 if (fs1[i](id, off) == 0)
150                         goto ret;
151
152         /* fill buffer with maximum */
153         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
154
155         for (i = 0; i < ARRAY_SIZE(fs2); i++)
156                 if (fs2[i](id, off) == 0)
157                         goto ret;
158         return -1;
159
160  ret:
161         /* If the filestystem in recognized, we free the allocated buffers,
162            otherwise they will stay in place for the possible next probe call */
163         volume_id_free_buffer(id);
164
165         return 0;
166 }
167
168 /* open volume by device node */
169 struct volume_id *volume_id_open_node(const char *path)
170 {
171         struct volume_id *id;
172         int fd;
173
174         fd = open(path, O_RDONLY);
175         if (fd < 0)
176                 return NULL;
177         id = xzalloc(sizeof(struct volume_id));
178         id->fd = fd;
179         ///* close fd on device close */
180         //id->fd_close = 1;
181
182         return id;
183 }
184
185 #ifdef UNUSED
186 /* open volume by major/minor */
187 struct volume_id *volume_id_open_dev_t(dev_t devt)
188 {
189         struct volume_id *id;
190         char *tmp_node[VOLUME_ID_PATH_MAX];
191
192         tmp_node = xasprintf("/dev/.volume_id-%u-%u-%u",
193                 (unsigned)getpid(), (unsigned)major(devt), (unsigned)minor(devt));
194
195         /* create temporary node to open block device */
196         unlink(tmp_node);
197         if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
198                 bb_perror_msg_and_die("cannot mknod(%s)", tmp_node);
199
200         id = volume_id_open_node(tmp_node);
201         unlink(tmp_node);
202         free(tmp_node);
203         return id;
204 }
205 #endif
206
207 void free_volume_id(struct volume_id *id)
208 {
209         if (id == NULL)
210                 return;
211
212         //if (id->fd_close != 0) - always true
213                 close(id->fd);
214         volume_id_free_buffer(id);
215 #ifdef UNUSED_PARTITION_CODE
216         free(id->partitions);
217 #endif
218         free(id);
219 }