1/**
2 * @file locate_images.h
3 * Location of binary images
4 *
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author Philippe Elie
9 * @author John Levon
10 */
11
12#ifndef LOCATE_IMAGES_H
13#define LOCATE_IMAGES_H
14
15#include <string>
16#include <map>
17#include <vector>
18
19#include "image_errors.h"
20
21/**
22 * A class containing mappings from an image basename,
23 * such as 'floppy.ko', to locations in the paths passed
24 * in to populate().
25 *
26 * The name may exist multiple times; all locations are recorded
27 * in this container.
28 */
29class extra_images {
30public:
31	extra_images();
32
33	/// add all filenames found in the given paths prefixed by the
34	/// archive path or the root path, recursively
35	void populate(std::vector<std::string> const & paths,
36		      std::string const & archive_path,
37		      std::string const & root_path);
38
39	/// base class for matcher functors object
40	struct matcher {
41		std::string const & value;
42	public:
43		explicit matcher(std::string const & v) : value(v) {}
44		virtual ~matcher() {}
45		/// default functor allowing trivial match
46		virtual bool operator()(std::string const & str) const {
47			return str == value;
48		}
49	};
50
51	/**
52	 * return a vector of all directories that match the functor
53	 */
54	std::vector<std::string> const find(matcher const & match) const;
55
56	/// return a vector of all directories that match the given name
57	std::vector<std::string> const find(std::string const & name) const;
58
59	/**
60	 * @param image_name binary image name
61	 * @param error errors are flagged in this passed enum ref
62	 * @param fixup if true return the fixed image name else always return
63	 *  image_name and update error
64	 *
65	 * Locate a (number of) matching absolute paths to the given image
66	 * name. If we fail to find the file we fill in error and return the
67	 * original string.
68	 */
69	std::string const find_image_path(std::string const & image_name,
70				image_error & error, bool fixup) const;
71
72	/// return the archive path used to populate the images name map
73	std::string get_archive_path() const { return archive_path; }
74
75	/// Given an image name returned by find_image_path() return
76	/// a filename with the archive_path or root_path stripped.
77	std::string strip_path_prefix(std::string const & image) const;
78
79	/// return the uid for this extra_images, first valid uid is 1
80	int get_uid() const { return uid; }
81
82private:
83	void populate(std::vector<std::string> const & paths,
84		      std::string const & prefix_path);
85
86	std::string const locate_image(std::string const & image_name,
87				image_error & error, bool fixup) const;
88
89	typedef std::multimap<std::string, std::string> images_t;
90	typedef images_t::value_type value_type;
91	typedef images_t::const_iterator const_iterator;
92
93	/// map from image basename to owning directory
94	images_t images;
95	/// the archive path passed to populate the images name map.
96	std::string archive_path;
97	/// A prefix added to locate binaries if they can't be found
98	/// through the archive path
99	std::string root_path;
100
101	/// unique identifier, first valid uid is 1
102	int uid;
103	/// unique uid generator
104	static int suid;
105};
106
107#endif /* LOCATE_IMAGES_H */
108