diff mbox series

[v2] archive handler: add preserve-attributes option option

Message ID 20171215094603.GM27580@vctlabs.com
State Accepted
Headers show
Series [v2] archive handler: add preserve-attributes option option | expand

Commit Message

S. Lockwood-Childs Dec. 15, 2017, 9:46 a.m. UTC
Previously extracted files do not keep attributes stored in
the archive file, e.g. all files and dirs come out root:root
ownership and with current timestamps.

The new preserve-attributes option asks libarchive to preserve
timestamps, uid/gid, file perm bits, acls, file attributes
(immutable, etc), and extended attributes.

      /* sw-description snippet */
      files: (
      {
        filename = "some.tar.gz";
        path = "/opt";
        preserve-attributes = true;
      }
      );

Note that uid/gid preserves numeric values in a tarball, i.e. same as
tar with --numeric-owner flag. At this time libarchive doesn't support
keeping names instead.

Rework of earlier patch by Brendan Simon
https://github.com/sbabic/swupdate/pull/12

Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
---
 doc/source/sw-description.rst |  9 +++++++++
 handlers/archive_handler.c    | 11 +++++++++--
 include/swupdate.h            |  1 +
 parser/parser.c               |  1 +
 4 files changed, 20 insertions(+), 2 deletions(-)

Comments

Stefano Babic Dec. 15, 2017, 9:55 a.m. UTC | #1
On 15/12/2017 10:56, S. Lockwood-Childs wrote:
> On Fri, Dec 15, 2017 at 01:46:03AM -0800, S. Lockwood-Childs wrote:
>> Previously extracted files do not keep attributes stored in
>> the archive file, e.g. all files and dirs come out root:root
>> ownership and with current timestamps.
>>
>> The new preserve-attributes option asks libarchive to preserve
>> timestamps, uid/gid, file perm bits, acls, file attributes
>> (immutable, etc), and extended attributes.
>>
>>       /* sw-description snippet */
>>       files: (
>>       {
>>         filename = "some.tar.gz";
>>         path = "/opt";
>>         preserve-attributes = true;
>>       }
>>       );
>>
>> Note that uid/gid preserves numeric values in a tarball, i.e. same as
>> tar with --numeric-owner flag. At this time libarchive doesn't support
>> keeping names instead.
>>
>> Rework of earlier patch by Brendan Simon
>> https://github.com/sbabic/swupdate/pull/12
>>
>> Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
> 
> Heh, I mostly fixed up the commit message but somehow botched up the
> subject line -- "option option"??

> 
> Please amend if/when you commit.
> 
Sure, I do it.

Best regards,
Stefano Babic
S. Lockwood-Childs Dec. 15, 2017, 9:56 a.m. UTC | #2
On Fri, Dec 15, 2017 at 01:46:03AM -0800, S. Lockwood-Childs wrote:
> Previously extracted files do not keep attributes stored in
> the archive file, e.g. all files and dirs come out root:root
> ownership and with current timestamps.
> 
> The new preserve-attributes option asks libarchive to preserve
> timestamps, uid/gid, file perm bits, acls, file attributes
> (immutable, etc), and extended attributes.
> 
>       /* sw-description snippet */
>       files: (
>       {
>         filename = "some.tar.gz";
>         path = "/opt";
>         preserve-attributes = true;
>       }
>       );
> 
> Note that uid/gid preserves numeric values in a tarball, i.e. same as
> tar with --numeric-owner flag. At this time libarchive doesn't support
> keeping names instead.
> 
> Rework of earlier patch by Brendan Simon
> https://github.com/sbabic/swupdate/pull/12
> 
> Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>

Heh, I mostly fixed up the commit message but somehow botched up the
subject line -- "option option"??

Please amend if/when you commit.
diff mbox series

Patch

diff --git a/doc/source/sw-description.rst b/doc/source/sw-description.rst
index 4a798ee..71bdeff 100644
--- a/doc/source/sw-description.rst
+++ b/doc/source/sw-description.rst
@@ -742,6 +742,7 @@  attribute. For example:
 				type = "archive";
 				path = "/tmp/test";
 				hook = "set_version";
+				preserve-attributes = true;
 			}
 		);
 
@@ -847,6 +848,14 @@  There are 4 main sections inside sw-description:
    |             |          |            | "filesystem" type. (path is always    |
    |             |          |            | relative to the mount point.)         |
    +-------------+----------+------------+---------------------------------------+
+   | preserve-   | bool     | files      | flag to control whether the following |
+   | attributes  |          |            | attributes will be preserved when     |
+   |             |          |            | files are unpacked from an archive    |
+   |             |          |            | (assuming destination filesystem      |
+   |             |          |            | supports them, of course):            |
+   |             |          |            | timestamp, uid/gid (numeric), perms,  |
+   |             |          |            | file attributes, extended attributes  |
+   +-------------+----------+------------+---------------------------------------+
    | type        | string   | images     | string identifier for the handler,    |
    |             |          | files      | as it is set by the handler when it   |
    |             |          | scripts    | regitsters itself.                    |
diff --git a/handlers/archive_handler.c b/handlers/archive_handler.c
index 0568a41..b4895f0 100644
--- a/handlers/archive_handler.c
+++ b/handlers/archive_handler.c
@@ -200,11 +200,18 @@  static int install_archive_image(struct img_type *img,
 		return -EFAULT;
 	}
 
-	TRACE("Installing file %s on %s\n",
-		img->fname, path);
+	TRACE("Installing file %s on %s, %s attributes\n",
+		img->fname, path, 
+		img->preserve_attributes ? "preserving" : "ignoring");
 
 	tf.flags = 0;
 
+	if (img->preserve_attributes) {
+		tf.flags |= ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | 
+				ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL | 
+				ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR;
+	}
+
 	ret = pthread_create(&extract_thread, &attr, extract, &tf);
 	if (ret) {
 		ERROR("Code from pthread_create() is %d\n",
diff --git a/include/swupdate.h b/include/swupdate.h
index 09e81d5..6424fc7 100644
--- a/include/swupdate.h
+++ b/include/swupdate.h
@@ -73,6 +73,7 @@  struct img_type {
 	int required;
 	int provided;
 	int compressed;
+	int preserve_attributes; /* whether to preserve attributes in archives */
 	int is_encrypted;
 	int install_directly;
 	int is_script;
index c8a51e2..1adce54 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -517,6 +517,7 @@  static int parse_files(parsertype p, void *cfg, struct swupdate_cfg *swcfg, lua_
 			strcpy(file->type, "rawfile");
 		}
 		get_field(p, elem, "compressed", &file->compressed);
+		get_field(p, elem, "preserve-attributes", &file->preserve_attributes);
 		get_field(p, elem, "installed-directly", &file->install_directly);
 		get_field(p, elem, "install-if-different", &file->id.install_if_different);
 		get_field(p, elem, "encrypted", &file->is_encrypted);