Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Preserve file attributes when copying with lua
Baptiste Daroussin committed 5 years ago
commit 694301edc3b408f4e2bbd7df22d1ea7cc6aec2f4
parent 7ec764b
4 files changed +27 -7
modified libpkg/lua_scripts.c
@@ -111,6 +111,8 @@ lua_pkg_copy(lua_State *L)
	struct stat s1;
	int fd1, fd2;

+
	bool install_as_user = (getenv("INSTALL_AS_USER") != NULL);
+

	lua_getglobal(L, "package");
	struct pkg *pkg = lua_touserdata(L, -1);

@@ -151,6 +153,23 @@ lua_pkg_copy(lua_State *L)

	close(fd1);
	close(fd2);
+

+
	if (set_attrsat(pkg->rootfd, RELATIVE_PATH(dst), s1.st_mode, s1.st_uid,
+
	  s1.st_gid, &s1.st_atim, &s1.st_mtim) != EPKG_OK) {
+
		lua_pushinteger(L, -1);
+
		return (1);
+
	}
+

+
#ifdef HAVE_CHFLAGSAT
+
	if (!install_as_user && s1.st_flags != 0) {
+
		if (chflagsat(pkg->rootfd, RELATIVE_PATH(dst),
+
		    s1.st_flags, AT_SYMLINK_NOFOLLOW) == -1) {
+
			pkg_fatal_errno("Fail to chflags %s", dst);
+
			lua_pushinteger(L, -1);
+
			return (1);
+
		}
+
	}
+
#endif
	return (0);
}

modified libpkg/pkg_add.c
@@ -221,8 +221,8 @@ out:
	return (grent.gr_gid);
}

-
static int
-
set_attrs(int fd, char *path, mode_t perm, uid_t uid, gid_t gid,
+
int
+
set_attrsat(int fd, const char *path, mode_t perm, uid_t uid, gid_t gid,
    const struct timespec *ats, const struct timespec *mts)
{

@@ -420,7 +420,7 @@ retry:
		pkg_fatal_errno("Fail to create symlink: %s", f->temppath);
	}

-
	if (set_attrs(pkg->rootfd, f->temppath, f->perm, f->uid, f->gid,
+
	if (set_attrsat(pkg->rootfd, f->temppath, f->perm, f->uid, f->gid,
	    &f->time[0], &f->time[1]) != EPKG_OK) {
		return (EPKG_FATAL);
	}
@@ -587,7 +587,7 @@ retry:
		close(fd);
	}

-
	if (set_attrs(pkg->rootfd, f->temppath, f->perm, f->uid, f->gid,
+
	if (set_attrsat(pkg->rootfd, f->temppath, f->perm, f->uid, f->gid,
	    &f->time[0], &f->time[1]) != EPKG_OK)
			return (EPKG_FATAL);

@@ -788,7 +788,7 @@ pkg_extract_finalize(struct pkg *pkg)
	while (pkg_dirs(pkg, &d) == EPKG_OK) {
		if (d->noattrs)
			continue;
-
		if (set_attrs(pkg->rootfd, d->path, d->perm,
+
		if (set_attrsat(pkg->rootfd, d->path, d->perm,
		    d->uid, d->gid, &d->time[0], &d->time[1]) != EPKG_OK)
			return (EPKG_FATAL);
	}
modified libpkg/private/pkg.h
@@ -890,5 +890,6 @@ struct pkg_dep* pkg_adddep_chain(struct pkg_dep *chain,
		char *version, bool locked);
void backup_library(struct pkgdb *, struct pkg *, const char *);
int suggest_arch(struct pkg *, bool);
+
int set_attrsat(int fd, const char *path, mode_t perm, uid_t uid, gid_t gid, const struct timespec *ats, const struct timespec *mts);

#endif
modified tests/frontend/lua.sh
@@ -269,7 +269,7 @@ files: {
lua_scripts: {
  post-install: [ <<EOS
  if pkg_filecmp("${TMPDIR}/a.sample", "${TMPDIR}/a") == 2 then
-
     pkg_copy("${TMPDIR}/a.sample", "${TMPDIR}/a", true)
+
     pkg_copy("${TMPDIR}/a.sample", "${TMPDIR}/a")
  end
EOS
, ]
@@ -308,7 +308,7 @@ files: {
lua_scripts: {
  post-install: [ <<EOS
  if pkg_filecmp("${TMPDIR}/a.sample", "${TMPDIR}/a") == 2 then
-
     pkg_copy("${TMPDIR}/a.sample", "${TMPDIR}/a", true)
+
     pkg_copy("${TMPDIR}/a.sample", "${TMPDIR}/a")
  end
EOS
, ]