diff options
author | kay.sievers@vrfy.org <kay.sievers@vrfy.org> | 2003-12-07 09:12:07 -0800 |
---|---|---|
committer | Greg KH <gregkh@suse.de> | 2005-04-26 21:13:06 -0700 |
commit | 3d150dfb28efbaf0b25f154fb8955c47d606c3d5 (patch) | |
tree | d94d704c7c0aa21ccfacf0f821a6e7071af22c46 /udev-add.c | |
parent | eadb1bbc2eece84d9aff79bf17e252106c37f355 (diff) |
[PATCH] experimental (very simple) SYMLINK creation
> > here is a experimental symlink creation patch - for discussion,
> > in which direction we should go.
> > It is possible now to define SYMLINK= after the NAME= in udev.rules.
> > The link is relative to the node, but the path is not optimized now
> > if the node and the link are in the same nested directory.
> > Only one link is supported, cause i need to sleep now :)
> >
> > 06-simple-symlink-creation.diff
> > simple symlink creation
> > reorganized udev-remove to have access to the symlink field
> > subdir creation/removal are functions now
> > udev-test.pl tests for link creation/removal
Here is a new version with relative link target path optimization
an better tests in udev-test.pl:
LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
Dec 7 06:48:34 pim udev[13789]: create_node: symlink 'udev-root/1/2/c/d/symlink' to node '1/2/a/b/node' requested
Dec 7 06:48:34 pim udev[13789]: create_path: created 'udev-root/1/2/c'
Dec 7 06:48:34 pim udev[13789]: create_path: created 'udev-root/1/2/c/d'
Dec 7 06:48:34 pim udev[13789]: create_node: symlink(../../a/b/node, udev-root/1/2/c/d/symlink)
Diffstat (limited to 'udev-add.c')
-rw-r--r-- | udev-add.c | 99 |
1 files changed, 72 insertions, 27 deletions
diff --git a/udev-add.c b/udev-add.c index a71d435e32..ddf432bbc7 100644 --- a/udev-add.c +++ b/udev-add.c @@ -72,6 +72,34 @@ exit: return retval; } +static int create_path(char *file) +{ + char p[NAME_SIZE]; + char *pos; + int retval; + struct stat stats; + + strncpy(p, file, sizeof(p)); + pos = strchr(p+1, '/'); + while (1) { + pos = strchr(pos+1, '/'); + if (pos == NULL) + break; + *pos = 0x00; + if (stat(p, &stats)) { + retval = mkdir(p, 0755); + if (retval) { + dbg("mkdir(%s) failed with error '%s'", + p, strerror(errno)); + return retval; + } + dbg("created '%s'", p); + } + *pos = '/'; + } + return 0; +} + /* * we possibly want to add some symlinks here * only numeric owner/group id's are supported @@ -79,10 +107,14 @@ exit: static int create_node(struct udevice *dev) { char filename[255]; + char linktarget[255]; int retval = 0; uid_t uid = 0; gid_t gid = 0; dev_t res; + int i; + int tail; + strncpy(filename, udev_root, sizeof(filename)); strncat(filename, dev->name, sizeof(filename)); @@ -109,31 +141,9 @@ static int create_node(struct udevice *dev) 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 = '/'; - } - } + /* create parent directories if needed */ + if (strrchr(dev->name, '/')) + create_path(filename); dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor); retval = mknod(filename, dev->mode, res); @@ -179,8 +189,43 @@ static int create_node(struct udevice *dev) dbg("chown(%s, %u, %u)", filename, uid, gid); retval = chown(filename, uid, gid); if (retval) - dbg("chown(%s, %u, %u) failed with error '%s'", filename, - uid, gid, strerror(errno)); + dbg("chown(%s, %u, %u) failed with error '%s'", + filename, uid, gid, strerror(errno)); + } + + + /* create symlink if requested */ + if (*dev->symlink) { + strncpy(filename, udev_root, sizeof(filename)); + strncat(filename, dev->symlink, sizeof(filename)); + dbg("symlink '%s' to node '%s' requested", filename, dev->name); + if (strrchr(dev->symlink, '/')) + create_path(filename); + + /* optimize relative link */ + linktarget[0] = '\0'; + i = 0; + tail = 0; + while ((dev->name[i] == dev->symlink[i]) && dev->name[i]) { + if (dev->name[i] == '/') + tail = i+1; + i++; + } + while (dev->symlink[i]) { + if (dev->symlink[i] == '/') + strcat(linktarget, "../"); + i++; + } + + if (*linktarget == '\0') + strcpy(linktarget, "./"); + strcat(linktarget, &dev->name[tail]); + + dbg("symlink(%s, %s)", linktarget, filename); + retval = symlink(linktarget, filename); + if (retval) + dbg("symlink(%s, %s) failed with error '%s'", + linktarget, filename, strerror(errno)); } return retval; |