Skip to contentSkip to conversation

misc: copy extended attributes in populate_fs

PATCH Superseded Ross Burton 2 participants 3 messages Jun 30, 2014 – Jul 9, 2014

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 misc: copy extended attributes in populate_fs

When creating a file system using a source directory, also copy any extended attributes that have been set.

---
 misc/create_inode.c |   92 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
misc/create_inode.c +92 -0
index c9c99b5..31f2d3c 100644
@@ -103,6 +103,91 @@ static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
 	return retval;
 }
 
+static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino, const char *filename)
+{
+	errcode_t			retval, close_retval;
+	struct ext2_inode		inode;
+	struct ext2_xattr_handle	*handle;
+	ssize_t				size, value_size;
+	char				*list;
+	int				i;
+
+	size = llistxattr(filename, NULL, 0);
+	if (size == -1) {
+		com_err(__func__, errno, "llistxattr failed on %s", filename);
+		return errno;
+	} else if (size == 0) {
+		return 0;
+	}
+
+	retval = ext2fs_xattrs_open(fs, ino, &handle);
+	if (retval) {
+		if (retval == EXT2_ET_MISSING_EA_FEATURE)
+			return 0;
+		com_err(__func__, retval, "while opening inode %u", ino);
+		return retval;
+	}
+
+	list = malloc(size);
+	if (!list) {
+		com_err(__func__, errno, "whilst allocating memory");
+		retval = errno;
+		goto out;
+	}
+
+	size = llistxattr(filename, list, size);
+	if (size == -1) {
+		com_err(__func__, errno, "llistxattr failed on %s", filename);
+		retval = errno;
+		goto out;
+        }
+
+	for (i = 0; i < size; i += strlen(&list[i]) + 1) {
+		const char *name = &list[i];
+		char *value;
+
+		value_size = getxattr(filename, name, NULL, 0);
+		if (value_size == -1) {
+			com_err(__func__, errno, "getxattr failed on %s", filename);
+			retval = errno;
+			break;
+		}
+
+		value = malloc(value_size);
+		if (!value) {
+			com_err(__func__, errno, "whilst allocating memory");
+			retval = errno;
+			break;
+		}
+
+		value_size = getxattr(filename, name, value, value_size);
+		if (value_size == -1) {
+			free(value);
+			com_err(__func__, errno, "getxattr failed on %s", filename);
+			retval = errno;
+			break;
+		}
+
+		retval = ext2fs_xattr_set(handle, name, value, value_size);
+		free (value);
+		if (retval) {
+			com_err(__func__, retval, "while writing xattr %u", ino);
+			break;
+		}
+
+	}
+ out:
+	free(list);
+
+	close_retval = ext2fs_xattrs_close(&handle);
+	if (close_retval) {
+		com_err(__func__, retval, "while closing inode %u", ino);
+		retval = retval ? close_retval;
+	}
+
+	return retval;
+}
+
 /* Make a special files (block and character devices), fifo's, and sockets  */
 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
 			    struct stat *st)
@@ -615,6 +700,13 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
 			goto out;
 		}
 
+		retval = set_inode_xattr(fs, ino, name);
+		if (retval) {
+			com_err(__func__, retval,
+				_("while setting xattrs for \"%s\""), name);
+			goto out;
+		}
+
 		/* Save the hardlink ino */
 		if (save_inode) {
 			/*
Signature · 1 line
1.7.10.4
Reviewed-by on v2 1/1
#2 lore raw
In reply to Ross Burton
Quoted 2 lines · Ross Burton

When creating a file system using a source directory, also copy any extended attributes that have been set.

Minor quibble:
ext2fs_get_mem(value_len, &value) instead of malloc, and ext2fs_free_mem(&value) instead of free().

Otherwise, you can add:

--D

Quoted 119 lines
---
 misc/create_inode.c |   92 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
misc/create_inode.c +92 -0
index c9c99b5..31f2d3c 100644
@@ -103,6 +103,91 @@ static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
 	return retval;
 }
 
+static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino, const char *filename)
+{
+	errcode_t			retval, close_retval;
+	struct ext2_inode		inode;
+	struct ext2_xattr_handle	*handle;
+	ssize_t				size, value_size;
+	char				*list;
+	int				i;
+
+	size = llistxattr(filename, NULL, 0);
+	if (size == -1) {
+		com_err(__func__, errno, "llistxattr failed on %s", filename);
+		return errno;
+	} else if (size == 0) {
+		return 0;
+	}
+
+	retval = ext2fs_xattrs_open(fs, ino, &handle);
+	if (retval) {
+		if (retval == EXT2_ET_MISSING_EA_FEATURE)
+			return 0;
+		com_err(__func__, retval, "while opening inode %u", ino);
+		return retval;
+	}
+
+	list = malloc(size);
+	if (!list) {
+		com_err(__func__, errno, "whilst allocating memory");
+		retval = errno;
+		goto out;
+	}
+
+	size = llistxattr(filename, list, size);
+	if (size == -1) {
+		com_err(__func__, errno, "llistxattr failed on %s", filename);
+		retval = errno;
+		goto out;
+        }
+
+	for (i = 0; i < size; i += strlen(&list[i]) + 1) {
+		const char *name = &list[i];
+		char *value;
+
+		value_size = getxattr(filename, name, NULL, 0);
+		if (value_size == -1) {
+			com_err(__func__, errno, "getxattr failed on %s", filename);
+			retval = errno;
+			break;
+		}
+
+		value = malloc(value_size);
+		if (!value) {
+			com_err(__func__, errno, "whilst allocating memory");
+			retval = errno;
+			break;
+		}
+
+		value_size = getxattr(filename, name, value, value_size);
+		if (value_size == -1) {
+			free(value);
+			com_err(__func__, errno, "getxattr failed on %s", filename);
+			retval = errno;
+			break;
+		}
+
+		retval = ext2fs_xattr_set(handle, name, value, value_size);
+		free (value);
+		if (retval) {
+			com_err(__func__, retval, "while writing xattr %u", ino);
+			break;
+		}
+
+	}
+ out:
+	free(list);
+
+	close_retval = ext2fs_xattrs_close(&handle);
+	if (close_retval) {
+		com_err(__func__, retval, "while closing inode %u", ino);
+		retval = retval ? close_retval;
+	}
+
+	return retval;
+}
+
 /* Make a special files (block and character devices), fifo's, and sockets  */
 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
 			    struct stat *st)
@@ -615,6 +700,13 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
 			goto out;
 		}
 
+		retval = set_inode_xattr(fs, ino, name);
+		if (retval) {
+			com_err(__func__, retval,
+				_("while setting xattrs for \"%s\""), name);
+			goto out;
+		}
+
 		/* Save the hardlink ino */
 		if (save_inode) {
 			/*
Signature · 1 line
1.7.10.4