]> git.ozlabs.org Git - petitboot/blob - discover/parser.h
utils: Add helper to send mailbox request
[petitboot] / discover / parser.h
1 #ifndef _PARSER_H
2 #define _PARSER_H
3
4 #include <stdbool.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 #include <dirent.h>
9
10 #include "device-handler.h"
11
12 struct discover_context;
13 struct device_handler;
14 struct resource;
15
16 /**
17  * Our config parser.
18  *
19  * Each parser is responsible for creating discover_boot_options from config
20  * files found on new devices. The boot items discovered during parse will have
21  * 'resources' attached (see @discover_boot_option), which may already be
22  * resolved (in the case of a device-local filename, or a URL), or unresolved
23  * (in the case of a filename on another device).
24  *
25  * If the boot option contains references to unresolved resources, the
26  * device handler will not inform clients about the boot options, as
27  * they're not properly "available" at this stage. The handler will attempt to
28  * resolve them whenever new devices are discovered, by calling the parser's
29  * resolve_resource function. Once a boot option's resources are full resolved,
30  * the option can be sent to clients.
31  */
32 struct parser {
33         char                    *name;
34         int                     (*parse)(struct discover_context *ctx);
35         bool                    (*resolve_resource)(
36                                                 struct device_handler *handler,
37                                                 struct resource *res);
38 };
39
40 enum generic_icon_type {
41         ICON_TYPE_DISK,
42         ICON_TYPE_USB,
43         ICON_TYPE_OPTICAL,
44         ICON_TYPE_NETWORK,
45         ICON_TYPE_UNKNOWN
46 };
47
48 struct parser_found_file {
49         const char *filename;
50         unsigned ino;
51         struct list_item list;
52 };
53
54 #define streq(a,b) (!strcasecmp((a),(b)))
55
56 void parser_init(void);
57
58 void iterate_parsers(struct discover_context *ctx);
59 int parse_user_event(struct discover_context *ctx, struct event *event);
60
61 /* File IO functions for parsers; these should be the only interface that
62  * parsers use to access a device's filesystem.
63  *
64  * These are intended for small amounts of data, typically text
65  * configuration and state files.  Note that parser_request_file,
66  * and parser_replace_file work only on non-directories.
67  */
68 int parser_request_file(struct discover_context *ctx,
69                 struct discover_device *dev, const char *filename,
70                 char **buf, int *len);
71 int parser_replace_file(struct discover_context *ctx,
72                 struct discover_device *dev, const char *filename,
73                 char *buf, int len);
74 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
75                 char **buf, int *len);
76 /* parser_stat_path returns 0 if path can be stated on dev by the
77  * running user.  Note that this function follows symlinks, like the
78  * stat system call.  When the function returns 0, also fills in
79  * statbuf for the path.  Returns non-zero on error.  This function
80  * does not have the limitations on file size that the functions above
81  * do.  Unlike some of the functions above, this function also works
82  * on directories. */
83 int parser_stat_path(struct discover_context *ctx,
84                 struct discover_device *dev, const char *path,
85                 struct stat *statbuf);
86 /* Function used to list the files on a directory. The dirname should
87  * be relative to the discover context device mount path. It returns
88  * the number of files returned in files or a negative value on error.
89  */
90 int parser_scandir(struct discover_context *ctx, const char *dirname,
91                    struct dirent ***files, int (*filter)(const struct dirent *),
92                    int (*comp)(const struct dirent **, const struct dirent **));
93
94 /* parser_is_unique - Test a file against a list of known files.
95  * If the file @filename exists and the file is not in @found_list add the
96  * file to @found_list and return true.  Use when searching case-insensitive
97  * filesystems.
98  */
99 bool parser_is_unique(struct discover_context *ctx, struct discover_device *dev, const char *filename,
100                 struct list *found_list);
101
102 #endif /* _PARSER_H */