Linux Hotplug development
 help / color / mirror / Atom feed
From: harald@redhat.com
To: linux-hotplug@vger.kernel.org
Subject: [PATCH] udevadm-info: add query for properties with quoted output
Date: Thu, 09 Dec 2010 14:26:33 +0000	[thread overview]
Message-ID: <1291904793-7271-1-git-send-email-harald@redhat.com> (raw)

From: Harald Hoyer <harald@redhat.com>

$ udevadm info --query=shproperty --name=/dev/sda
UDEV_LOG='3'
DEVPATH='/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda'
MAJOR='8'
MINOR='0'
DEVNAME='/dev/sda'
DEVTYPE='disk'
...
DEVLINKS='/dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ /dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0'

This enables the use of "eval" in shell scripts:
$ eval $(udevadm info --query=shenv --name=/dev/sda)
$ echo $DEVLINKS
/dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ /dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0
---
 libudev/libudev-private.h |    1 +
 libudev/libudev-util.c    |   25 +++++++++++++++++++++++++
 udev/udevadm-info.c       |   19 +++++++++++++++++++
 3 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/libudev/libudev-private.h b/libudev/libudev-private.h
index c9ed462..277a8bf 100644
--- a/libudev/libudev-private.h
+++ b/libudev/libudev-private.h
@@ -197,6 +197,7 @@ ssize_t util_get_sys_subsystem(struct udev *udev, const char *syspath, char *sub
 ssize_t util_get_sys_driver(struct udev *udev, const char *syspath, char *driver, size_t size);
 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size);
 int util_log_priority(const char *priority);
+size_t util_shell_encode(const char *src, char *dest, size_t size);
 size_t util_path_encode(const char *src, char *dest, size_t size);
 size_t util_path_decode(char *s);
 void util_remove_trailing_chars(char *path, char c);
diff --git a/libudev/libudev-util.c b/libudev/libudev-util.c
index 030b78c..12b2b3d 100644
--- a/libudev/libudev-util.c
+++ b/libudev/libudev-util.c
@@ -98,6 +98,31 @@ int util_log_priority(const char *priority)
 	return 0;
 }
 
+size_t util_shell_encode(const char *src, char *dest, size_t size)
+{
+	size_t i, j;
+
+	for (i = 0, j = 0; src[i] != '\0'; i++) {
+		if (src[i] = '\'') {
+			if (j+4 >= size) {
+				j = 0;
+				break;
+			}
+			memcpy(&dest[j], "'\\''", 4);
+			j += 4;
+		} else {
+			if (j+1 >= size) {
+				j = 0;
+				break;
+			}
+			dest[j] = src[i];
+			j++;
+		}
+	}
+	dest[j] = '\0';
+	return j;
+}
+
 size_t util_path_encode(const char *src, char *dest, size_t size)
 {
 	size_t i, j;
diff --git a/udev/udevadm-info.c b/udev/udevadm-info.c
index 9bd60c7..6e0feea 100644
--- a/udev/udevadm-info.c
+++ b/udev/udevadm-info.c
@@ -235,6 +235,7 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
 		QUERY_PATH,
 		QUERY_SYMLINK,
 		QUERY_PROPERTY,
+		QUERY_PROPERTY_QUOTED,
 		QUERY_ALL,
 	} query = QUERY_NONE;
 
@@ -307,6 +308,8 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
 			action = ACTION_QUERY;
 			if (strcmp(optarg, "property") = 0 || strcmp(optarg, "env") = 0) {
 				query = QUERY_PROPERTY;
+			} else if (strcmp(optarg, "shproperty") = 0 || strcmp(optarg, "shenv") = 0) {
+				query = QUERY_PROPERTY_QUOTED;
 			} else if (strcmp(optarg, "name") = 0) {
 				query = QUERY_NAME;
 			} else if (strcmp(optarg, "symlink") = 0) {
@@ -352,6 +355,7 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
 			       "      symlink                  pointing to node\n"
 			       "      path                     sys device path\n"
 			       "      property                 the device properties\n"
+			       "      shproperty               the device properties in shell style key='value'\n"
 			       "      all                      all values\n"
 			       "  --path=<syspath>           sys device path used for query or attribute walk\n"
 			       "  --name=<name>              node or symlink name used for query or attribute walk\n"
@@ -421,6 +425,21 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
 				list_entry = udev_list_entry_get_next(list_entry);
 			}
 			break;
+		case QUERY_PROPERTY_QUOTED:
+			list_entry = udev_device_get_properties_list_entry(device);
+			while (list_entry != NULL) {
+				char *val = NULL;
+				const char *oval = udev_list_entry_get_value(list_entry);
+				size_t len = strlen(oval)*4 + 1;
+				val = malloc(len);
+				if (val) {
+					util_shell_encode(oval, val, len);
+					printf("%s='%s'\n", udev_list_entry_get_name(list_entry), val);
+					list_entry = udev_list_entry_get_next(list_entry);
+					free(val);
+				}
+			}
+			break;
 		case QUERY_ALL:
 			print_record(device);
 			break;
-- 
1.7.3.2


             reply	other threads:[~2010-12-09 14:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-09 14:26 harald [this message]
2010-12-09 18:03 ` [PATCH] udevadm-info: add query for properties with quoted output David Zeuthen
2010-12-09 18:17 ` Kay Sievers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1291904793-7271-1-git-send-email-harald@redhat.com \
    --to=harald@redhat.com \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox