commit 5cbba5a759d3d7449e9d66440a435e96baa37c8f
parent b1e7233186b88055c7b1d1dd34013787e64258a5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Tue, 8 Jan 2019 18:54:12 +0000
[ar] Set file attributes in extracted files
Diffstat:
3 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/src/cmd/ar.c b/src/cmd/ar.c
@@ -125,7 +125,7 @@ archive(char *fname, FILE *to, char letter)
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 @@ extract(struct member *m, int argc, char *argv[])
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 @@ extract(struct member *m, int argc, char *argv[])
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 @@ closetmp(int which)
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
diff --git 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 @@ getstat(char *fname, struct fprop *prop)
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;
}
diff --git 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;
};