Theodore Ts'o | 2859522 | 2003-03-01 20:01:27 -0500 | [diff] [blame] | 1 | libblkid - a library to handle device identification and token extraction |
| 2 | |
| 3 | Basic usage is as follows - there are two normal usage patterns: |
| 4 | |
| 5 | For cases where a program wants information about multiple devices, or |
| 6 | expects to be doing multiple token searches, the program should directly |
| 7 | initialize cache file via (second parameter is cache filename, NULL = default): |
| 8 | |
| 9 | blkid_cache cache = NULL; |
| 10 | if (blkid_get_cache(&cache, NULL) < 0) |
| 11 | /* error reading the cache file, not really fatal */ |
| 12 | |
| 13 | Note that if no cache file exists, an empty cache struct is still allocated. |
| 14 | Usage of libblkid functions will use the cache to avoid needless device scans. |
| 15 | |
| 16 | For cases where a program only wants to find a single token, or display |
| 17 | information about a specific device, it is often not necessary to manually |
| 18 | load the cache. For functions which deal with a single device, they will |
| 19 | probe the device directly if no cache is given. For "get" functions which |
| 20 | do searching, they will read/free the cache internally. |
| 21 | |
| 22 | |
| 23 | How to use libblkid? Normally, you either want to find a device with a |
| 24 | specific NAME=value token, or you want to output token(s) from a device. |
| 25 | To locate a specific token, you can call: |
| 26 | |
| 27 | if ((devname = blkid_get_devname(cache, token, value))) { |
| 28 | /* do something with devname */ |
| 29 | string_free(devname); |
| 30 | } |
| 31 | |
| 32 | where cache is optionally loaded, token is either a "NAME=value" string, |
| 33 | or "NAME" and value gives the desired value to look for. The return |
| 34 | value is an allocated string which holds the resulting device name (if |
| 35 | found) or "NAME" if we do not have a proper value, or NULL if a specific |
| 36 | token was not found. This allows you to pass token = "/dev/hda1" or |
| 37 | token = "LABEL=root" and get a valid device name back if possible. The |
| 38 | returned string must be freed with "free(devname)". |
| 39 | |
| 40 | The other common usage is to want the value of a specific tag on a device. |
| 41 | |
| 42 | if ((value = blkid_get_tag_value(cache, tagname, devname))) { |
| 43 | /* do something with value */ |
| 44 | string_free(value); |
| 45 | } |
| 46 | |
| 47 | If you have directly loaded the cache from the program, you need to clean |
| 48 | up by saving the cache (assuming you have write access to the cache, this |
| 49 | happens automatically if you didn't load it directly), and freeing it (this |
| 50 | will also free all associated devices/tags): |
| 51 | |
| 52 | blkid_put_cache(cache); |
| 53 | |
| 54 | In all cases, any data returned by the searching functions from the cache |
| 55 | is first verified from disk to ensure that it is still valid. |