struct squat_trie *
squat_trie_init(const char *path, uint32_t uidvalidity,
- enum file_lock_method lock_method, enum squat_index_flags flags)
+ enum file_lock_method lock_method, enum squat_index_flags flags,
+ mode_t mode, gid_t gid)
{
struct squat_trie *trie;
trie->lock_method = lock_method;
trie->uidvalidity = uidvalidity;
trie->flags = flags;
+ trie->create_mode = mode;
+ trie->create_gid = gid;
squat_trie_normalize_map_build(trie);
trie->dotlock_set.use_excl_lock =
node_read_children(trie, &trie->root, 1);
}
+int squat_trie_create_fd(struct squat_trie *trie, const char *path, int flags)
+{
+ mode_t old_mask;
+ int fd;
+
+ old_mask = umask(0);
+ fd = open(path, O_RDWR | O_CREAT | flags, trie->create_mode);
+ umask(old_mask);
+ if (fd == -1) {
+ i_error("creat(%s) failed: %m", path);
+ return -1;
+ }
+ if (trie->create_gid != (gid_t)-1) {
+ if (fchown(fd, (uid_t)-1, trie->create_gid) < 0) {
+ i_error("fchown(%s, -1, %ld) failed: %m",
+ path, (long)trie->create_gid);
+ return -1;
+ }
+ }
+ return fd;
+}
+
int squat_trie_build_init(struct squat_trie *trie, uint32_t *last_uid_r,
struct squat_trie_build_context **ctx_r)
{
struct squat_uidlist_build_context *uidlist_build_ctx;
if (trie->fd == -1) {
- trie->fd = open(trie->path, O_RDWR | O_CREAT, 0600);
- if (trie->fd == -1) {
- i_error("creat(%s) failed: %m", trie->path);
+ trie->fd = squat_trie_create_fd(trie, trie->path, 0);
+ if (trie->fd == -1)
return -1;
- }
+
if (trie->file_cache != NULL)
file_cache_set_fd(trie->file_cache, trie->fd);
i_assert(trie->locked_file_size == 0);
ctx->compress_nodes = TRUE;
path = t_strconcat(trie->path, ".tmp", NULL);
- fd = open(path, O_RDWR | O_CREAT, 0600);
- if (fd == -1) {
- i_error("creat(%s) failed: %m", path);
+ fd = squat_trie_create_fd(trie, path, O_TRUNC);
+ if (fd == -1)
return -1;
- }
+
if (trie->lock_method != FILE_LOCK_METHOD_DOTLOCK) {
ret = file_wait_lock(fd, path, F_WRLCK,
trie->lock_method,
return -1;
squat_uidlist_close(uidlist);
- uidlist->fd = open(uidlist->path, O_RDWR | O_CREAT, 0600);
- if (uidlist->fd == -1) {
- i_error("open(%s) failed: %m", uidlist->path);
+ uidlist->fd = squat_trie_create_fd(uidlist->trie,
+ uidlist->path, 0);
+ if (uidlist->fd == -1)
return -1;
- }
}
return 1;
}
int ret;
if (uidlist->fd == -1) {
- uidlist->fd = open(uidlist->path, O_RDWR | O_CREAT, 0600);
- if (uidlist->fd == -1) {
- i_error("creat(%s) failed: %m", uidlist->path);
+ uidlist->fd = squat_trie_create_fd(uidlist->trie,
+ uidlist->path, 0);
+ if (uidlist->fd == -1)
return -1;
- }
}
if (squat_uidlist_lock(uidlist) <= 0)
return -1;
return -1;
temp_path = t_strconcat(build_ctx->uidlist->path, ".tmp", NULL);
- fd = open(temp_path, O_RDWR | O_TRUNC | O_CREAT, 0600);
- if (fd < 0) {
- i_error("open(%s) failed: %m", temp_path);
+ fd = squat_trie_create_fd(build_ctx->uidlist->trie, temp_path, O_TRUNC);
+ if (fd == -1)
return -1;
- }
ctx = i_new(struct squat_uidlist_rebuild_context, 1);
ctx->uidlist = build_ctx->uidlist;