diff options
author | Tom Gundersen <teg@jklm.no> | 2015-03-18 19:38:08 -0400 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2015-03-18 19:56:01 -0400 |
commit | 5c3965d1a89539ad6042bbfdcc844fc9ed8aa379 (patch) | |
tree | 07ea294763228c5a0ae10b16568ca65b48811a65 /src/shared | |
parent | ced0d2419e3fc88e29f8076363deb4214b87f0f2 (diff) |
libudev: udev_device_add_property - implicitly mark properties for saving to db
Note: We also ported touch() and touch_file() from upstream. -AGB.
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/util.c | 43 | ||||
-rw-r--r-- | src/shared/util.h | 3 |
2 files changed, 46 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index f68b687006..d1fd3c80f3 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1001,6 +1001,49 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { return 0; } +int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) { + _cleanup_close_ int fd; + int r; + + assert(path); + + if (parents) + mkdir_parents(path, 0755); + + fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode > 0 ? mode : 0644); + if (fd < 0) + return -errno; + + if (mode > 0) { + r = fchmod(fd, mode); + if (r < 0) + return -errno; + } + + if (uid != UID_INVALID || gid != GID_INVALID) { + r = fchown(fd, uid, gid); + if (r < 0) + return -errno; + } + + if (stamp != USEC_INFINITY) { + struct timespec ts[2]; + + timespec_store(&ts[0], stamp); + ts[1] = ts[0]; + r = futimens(fd, ts); + } else + r = futimens(fd, NULL); + if (r < 0) + return -errno; + + return 0; +} + +int touch(const char *path) { + return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0); +} + bool null_or_empty(struct stat *st) { assert(st); diff --git a/src/shared/util.h b/src/shared/util.h index 55701cf7a5..fc4f9f2e22 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -282,6 +282,9 @@ char* dirname_malloc(const char *path); int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); +int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode); +int touch(const char *path); + bool null_or_empty(struct stat *st) _pure_; int null_or_empty_path(const char *fn); int null_or_empty_fd(int fd); |