+/* Helper for functions that need an argument ("path") that is absolute.
+ * NB. Cannot be used for FileIn functions.
+ */
+#define ABS_PATH(path,cancel_stmt,fail_stmt) \
+ do { \
+ if ((path)[0] != '/') { \
+ if ((cancel_stmt) != -2) \
+ reply_with_error ("%s: path must start with a / character", __func__); \
+ fail_stmt; \
+ } \
+ } while (0)
+
+/* All functions that need an argument that is a device or partition name
+ * must call this macro. It checks that the device exists and does
+ * device name translation (described in the guestfs(3) manpage).
+ * Note that the "path" argument may be modified.
+ *
+ * NB. Cannot be used for FileIn functions.
+ */
+#define RESOLVE_DEVICE(path,cancel_stmt,fail_stmt) \
+ do { \
+ if (STRNEQLEN ((path), "/dev/", 5)) { \
+ if ((cancel_stmt) != -2) \
+ reply_with_error ("%s: %s: expecting a device name", __func__, (path)); \
+ fail_stmt; \
+ } \
+ if (is_root_device (path)) \
+ reply_with_error ("%s: %s: device not found", __func__, path); \
+ if (device_name_translation ((path)) == -1) { \
+ int err = errno; \
+ int r = cancel_stmt; \
+ errno = err; \
+ if (r != -2) \
+ reply_with_perror ("%s: %s", __func__, path); \
+ fail_stmt; \
+ } \
+ } while (0)
+
+/* Helper for functions which need either an absolute path in the
+ * mounted filesystem, OR a /dev/ device which exists.
+ *
+ * NB. Cannot be used for FileIn functions.
+ *
+ * NB #2: Functions which mix filenames and device paths should be
+ * avoided, and existing functions should be deprecated. This is
+ * because we intend in future to make device parameters a distinct
+ * type from filenames.
+ */
+#define REQUIRE_ROOT_OR_RESOLVE_DEVICE(path,cancel_stmt,fail_stmt) \
+ do { \
+ if (STREQLEN ((path), "/dev/", 5)) \
+ RESOLVE_DEVICE ((path), cancel_stmt, fail_stmt); \
+ else { \
+ NEED_ROOT (cancel_stmt, fail_stmt); \
+ ABS_PATH ((path), cancel_stmt, fail_stmt); \
+ } \
+ } while (0)
+