shithub: scc

Download patch

ref: 4effab9743c86099794e0bca4035a59d7405de47
parent: 634d58176f11bc196c40934ca24a633f11315cb5
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Jan 8 13:54:12 EST 2019

[ar] Set file attributes in extracted files

--- a/src/cmd/ar.c
+++ b/src/cmd/ar.c
@@ -125,7 +125,7 @@
 		error("error getting '%s' attributes", fname);
 	strftime(mtime, sizeof(mtime), "%s", gmtime(&prop.time));
 	fprintf(to,
-	        "%-16.16s%-12s%-6u%-6u%-8o%-10llu`\n",
+	        "%-16.16s%-12s%-6u%-6u%-8lo%-10llu`\n",
 	        fname,
 	        mtime,
 	        prop.uid,
@@ -261,6 +261,8 @@
 	int c;
 	long siz;
 	FILE *fp;
+	struct fprop prop;
+	struct ar_hdr *hdr = &m->hdr;
 
 	if (argc > 0 && !inlist(m->fname, argc, argv))
 		return;
@@ -276,13 +278,16 @@
 	if (fclose(fp) == EOF)
 		goto error_file;
 
-	/* TODO: set attributes */
+	prop.uid = atol(hdr->ar_uid);
+	prop.gid = atol(hdr->ar_gid);
+	prop.mode = m->mode;
+	prop.time = totime(m->date);
+	if (setstat(m->fname, &prop) < 0)
+		error("%s: setting file attributes", m->fname);
 	return;
 
-
 error_file:
-	perror("ar:error extracting file");
-	exit(1);
+	error("error extracting file: %s", errstr());
 }
 
 static void
@@ -448,10 +453,8 @@
 
 	if (!tmp->fp)
 		return;
-	if (fclose(tmp->fp) == EOF) {
-		perror("ar:closing temporaries");
-		exit(1);
-	}
+	if (fclose(tmp->fp) == EOF)
+		error("closing temporaries: %s", errstr());
 }
 
 static void
--- a/src/cmd/posix.c
+++ b/src/cmd/posix.c
@@ -1,8 +1,10 @@
 static char sccsid[] = "@(#) ./ar/posix/driver.c";
 
 #include <sys/types.h>
+#include <sys/time.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <utime.h>
 
 #include "sys.h"
 
@@ -30,4 +32,19 @@
 int
 setstat(char *fname, struct fprop *prop)
 {
+	struct utimbuf ut = {prop->time, prop->time};
+	uid_t uid;
+	gid_t gid;
+
+	
+
+	if (chown(fname, prop->uid, prop->gid) < 0) {
+		if (chown(fname, getuid(), getgid()) < 0)
+			return -1;
+	}
+	if (chmod(fname, prop->mode) < 0)
+		return -1;
+	if (utime(fname, &ut) < 0)
+		return -1;
+	return 0;
 }
--- a/src/cmd/sys.h
+++ b/src/cmd/sys.h
@@ -1,7 +1,7 @@
 struct fprop {
 	unsigned uid;
 	unsigned gid;
-	unsigned mode;
+	unsigned long mode;
 	long size;
 	time_t time;
 };