mirror of
https://github.com/troglobit/libite.git
synced 2026-10-13 10:14:42 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b69d8a6013 | ||
|
|
e874608b76 |
No files matched your search
@@ -4,6 +4,16 @@ Change Log
|
||||
All notable changes to the project are documented in this file.
|
||||
|
||||
|
||||
[UNRELEASED][]
|
||||
--------------
|
||||
|
||||
### Fixes
|
||||
- Fix `copyfile()` not preserving the source file mode when the process
|
||||
umask is more restrictive
|
||||
- Fix `mkpath()`, and `fmkpath()`, not applying the given mode when the
|
||||
process umask is more restrictive, e.g., requesting 0755 with umask
|
||||
0027 silently created a 0750 directory
|
||||
|
||||
[v2.6.2][] - 2025-11-22
|
||||
-----------------------
|
||||
|
||||
|
||||
@@ -208,12 +208,20 @@ ssize_t copyfile(const char *src, const char *dst, int len, int opt)
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* open(2) applies umask, we want the source file mode */
|
||||
if (fchmod(out, st.st_mode & 07777)) {
|
||||
saved_errno = errno;
|
||||
size = -1;
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
size = do_copy(in, out, num, buffer, BUFSIZ);
|
||||
if (size < 0)
|
||||
saved_errno = errno;
|
||||
else if (keep_mtim)
|
||||
set_mtime(in, out);
|
||||
|
||||
close_exit:
|
||||
close(out);
|
||||
close(in);
|
||||
|
||||
|
||||
+7
-1
@@ -35,6 +35,9 @@
|
||||
* @param dir Directory to created, relative or absolute
|
||||
* @param mode A &mode_t mode to create @a dir with
|
||||
*
|
||||
* Directories are created with exactly @a mode, the process umask is
|
||||
* not applied like it is for mkdir(2).
|
||||
*
|
||||
* @returns POSIX OK(0) on success, otherwise -1 with @a errno set.
|
||||
*/
|
||||
int mkpath(const char *dir, mode_t mode)
|
||||
@@ -51,7 +54,10 @@ int mkpath(const char *dir, mode_t mode)
|
||||
|
||||
mkpath(dirname(strdupa(dir)), mode);
|
||||
|
||||
return mkdir(dir, mode);
|
||||
if (mkdir(dir, mode))
|
||||
return -1;
|
||||
|
||||
return chmod(dir, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+24
-1
@@ -123,9 +123,32 @@ static int _copyfile()
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* copyfile() must preserve source mode, even with a restrictive umask */
|
||||
static int _mode(void)
|
||||
{
|
||||
const char *src = "/tmp/libite-mode-src", *dst = "/tmp/libite-mode-dst";
|
||||
struct stat st;
|
||||
mode_t old;
|
||||
int rc = 1;
|
||||
|
||||
if (touch(src) || chmod(src, 0644))
|
||||
return test(1, "copyfile() mode, prepare %s", src);
|
||||
|
||||
old = umask(0077);
|
||||
if (copyfile(src, dst, 0, 0) >= 0 && !stat(dst, &st))
|
||||
rc = (st.st_mode & 07777) != 0644;
|
||||
umask(old);
|
||||
|
||||
erase(src);
|
||||
erase(dst);
|
||||
|
||||
return test(rc, "copyfile() preserves mode 0644 with umask 0077");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
return _fcopyfile() ||
|
||||
_copyfile() ||
|
||||
_sz();
|
||||
_sz() ||
|
||||
_mode();
|
||||
}
|
||||
+34
-2
@@ -50,6 +50,34 @@ int test_fmkpath(const char *subdir)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* mkpath() must apply mode as given, not mode & ~umask */
|
||||
int test_umask(void)
|
||||
{
|
||||
const char *dirs[] = { BASEDIR "/um", BASEDIR "/um/sub", NULL };
|
||||
mode_t old = umask(0077);
|
||||
struct stat sb;
|
||||
int i, ret;
|
||||
|
||||
ret = mkpath(dirs[1], 0755);
|
||||
umask(old);
|
||||
if (ret) {
|
||||
perror("Failed");
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (i = 0; dirs[i]; i++) {
|
||||
if (checkpath(dirs[i]) || stat(dirs[i], &sb))
|
||||
return 1;
|
||||
if ((sb.st_mode & 07777) != 0755) {
|
||||
fprintf(stderr, "Failed: %s mode %04o, expected 0755\n",
|
||||
dirs[i], sb.st_mode & 07777);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i, ret = 0;
|
||||
@@ -66,14 +94,18 @@ int main(void)
|
||||
|
||||
mkdir(BASEDIR, 0755);
|
||||
|
||||
printf("Pass 1/2: makepath() =========================\n");
|
||||
printf("Pass 1/3: makepath() =========================\n");
|
||||
for (i = 0; list[i] && !ret; i++)
|
||||
ret |= test_makepath(list[i]);
|
||||
|
||||
printf("Pass 2/2: fmkpath() ==========================\n");
|
||||
printf("Pass 2/3: fmkpath() ==========================\n");
|
||||
for (i = 0; list[i] && !ret; i++)
|
||||
ret |= test_fmkpath(list[i] + strlen(BASEDIR) + 1);
|
||||
|
||||
printf("Pass 3/3: mkpath() with umask ================\n");
|
||||
if (!ret)
|
||||
ret |= test_umask();
|
||||
|
||||
printf("Done: Cleaning up ============================\n");
|
||||
system("rm -rf " BASEDIR);
|
||||
|
||||
|
||||
Reference in new issue
Block a user