[PATCH 03/18] fs: add symlink support

Jean-Christophe PLAGNIOL-VILLARD plagnioj at jcrosoft.com
Fri Aug 24 00:50:03 EDT 2012


Limit it's support to existing file only

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>
---
 fs/fs.c      |   43 +++++++++++++++++++++++++++++++++++++++++++
 include/fs.h |    4 ++++
 2 files changed, 47 insertions(+)

diff --git a/fs/fs.c b/fs/fs.c
index c950054..a19e1f4 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -949,6 +949,49 @@ out:
 }
 EXPORT_SYMBOL(readlink);
 
+int symlink(const char *pathname, const char *newpath)
+{
+	struct fs_driver_d *fsdrv;
+	struct fs_device_d *fsdev;
+	char *p = normalise_path(pathname);
+	int ret;
+	struct stat s;
+
+	if (!stat(p, &s) && S_ISDIR(s.st_mode)) {
+		ret = -ENOSYS;
+		goto out;
+	}
+
+	free(p);
+	p = normalise_path(newpath);
+
+	ret = stat(p, &s);
+	if (!ret) {
+		ret = -EEXIST;
+		goto out;
+	}
+
+	fsdev = get_fs_device_and_root_path(&p);
+	if (!fsdev) {
+		ret = -ENODEV;
+		goto out;
+	}
+	fsdrv = fsdev->driver;
+
+	if (fsdrv->symlink) {
+		ret = fsdrv->symlink(&fsdev->dev, pathname, p);
+	} else {
+		ret = -EROFS;
+	}
+
+out:
+	if (ret)
+		errno = -ret;
+
+	return ret;
+}
+EXPORT_SYMBOL(symlink);
+
 static int fs_match(struct device_d *dev, struct driver_d *drv)
 {
 	return strcmp(dev->name, drv->name) ? -1 : 0;
diff --git a/include/fs.h b/include/fs.h
index 52e0f4f..d652ade 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -51,6 +51,8 @@ struct fs_driver_d {
 	/* Truncate a file to given size */
 	int (*truncate)(struct device_d *dev, FILE *f, ulong size);
 
+	int (*symlink)(struct device_d *dev, const char *pathname,
+		       const char *newpath);
 	int (*readlink)(struct device_d *dev, const char *pathname, char *name,
 			size_t size);
 
@@ -136,6 +138,8 @@ DIR *opendir(const char *pathname);
 struct dirent *readdir(DIR *dir);
 int closedir(DIR *dir);
 
+
+int symlink(const char *pathname, const char *newpath);
 ssize_t readlink(const char *path, char *buf, size_t bufsiz);
 
 int mount (const char *device, const char *fsname, const char *path);
-- 
1.7.10.4




More information about the barebox mailing list