From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kay Sievers Date: Fri, 31 Oct 2003 14:19:46 +0000 Subject: udev creation/removal of subdirectories MIME-Version: 1 Content-Type: multipart/mixed; boundary="ibTvN161/egqYuK8" Message-Id: List-Id: To: linux-hotplug@vger.kernel.org --ibTvN161/egqYuK8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, here is a patch to implement subdirectory creation/removal: LABEL, BUS="usb", model="Creative Labs WebCam 3", NAME="devfs/scheme/is/crazy" it does on ADD: Oct 31 15:00:48 pim udev[21614]: udev_add_device: name = devfs/scheme/is/crazy Oct 31 15:00:48 pim udev[21614]: create_node: created /udev/devfs Oct 31 15:00:48 pim udev[21614]: create_node: created /udev/devfs/scheme Oct 31 15:00:48 pim udev[21614]: create_node: created /udev/devfs/scheme/is Oct 31 15:00:48 pim udev[21614]: create_node:mknod(/udev/devfs/scheme/is/crazy, 020666, 81, 0) and on REMOVE: Oct 31 15:01:40 pim udev[21622]: get_name: name is devfs/scheme/is/crazy Oct 31 15:01:40 pim udev[21622]: delete_node: unlinking /udev/devfs/scheme/is/crazy Oct 31 15:01:40 pim udev[21622]: delete_node: removed /udev/devfs/scheme/is Oct 31 15:01:40 pim udev[21622]: delete_node: removed /udev/devfs/scheme Oct 31 15:01:40 pim udev[21622]: delete_node: removed /udev/devfs thanks, Kay support for subdirectory creation/removal for NAME="printer/hp" 03-udev-add.c-subdirs.diff o create parent subdirs for device node if needed 03-udev-remove.c-subdirs.diff o remove subdirs if empty --ibTvN161/egqYuK8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline; filename="03-udev-add.c-subdirs.diff" --- ../udev/udev-add.c 2003-10-30 22:46:08.000000000 +0100 +++ udev-add.c 2003-10-31 14:38:48.000000000 +0100 @@ -100,6 +100,32 @@ return -EINVAL; } + /* create subdirectories if requested */ + if (strchr(dev->name, '/')) { + char path[255]; + char *pos; + struct stat stats; + + strncpy(path, filename, sizeof(path)); + pos = strchr(path+1, '/'); + while (1) { + pos = strchr(pos+1, '/'); + if (pos == NULL) + break; + *pos = 0x00; + if (stat(path, &stats)) { + retval = mkdir(path, 0755); + if (retval) { + dbg("mkdir(%s) failed with error '%s'", + path, strerror(errno)); + return retval; + } + dbg("created %s", path); + } + *pos = '/'; + } + } + dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor); retval = mknod(filename, dev->mode, res); if (retval) --ibTvN161/egqYuK8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline; filename="03-udev-remove.c-subdirs.diff" --- ../udev/udev-remove.c 2003-10-30 22:46:08.000000000 +0100 +++ udev-remove.c 2003-10-31 14:38:12.000000000 +0100 @@ -69,12 +69,45 @@ static int delete_node(char *name) { char filename[255]; + int retval; strncpy(filename, udev_root, sizeof(filename)); strncat(filename, name, sizeof(filename)); dbg("unlinking %s", filename); - return unlink(filename); + retval = unlink(filename); + if (retval) { + dbg("unlink(%s) failed with error '%s'", + filename, strerror(errno)); + return retval; + } + + /* remove subdirectories */ + if (strchr(name, '/')) { + char *pos; + + pos = strrchr(filename, '/'); + while (1) { + *pos = 0x00; + pos = strrchr(filename, '/'); + + /* don't remove the last one */ + if ((pos == filename) || (pos == NULL)) + break; + + /* remove if empty */ + retval = rmdir(filename); + if (retval) { + if (errno == ENOTEMPTY) + return 0; + dbg("rmdir(%s) failed with error '%s'", + strerror(errno)); + break; + } + dbg("removed %s", filename); + } + } + return retval; } int udev_remove_device(char *device, char *subsystem) --ibTvN161/egqYuK8-- ------------------------------------------------------- This SF.net email is sponsored by: SF.net Giveback Program. Does SourceForge.net help you be more productive? Does it help you create better code? SHARE THE LOVE, and help us help YOU! Click Here: http://sourceforge.net/donate/ _______________________________________________ Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net Linux-hotplug-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel