annotate src/lib/file-create-locked.h @ 22664:fea53c2725c0

director: Fix director_max_parallel_moves/kicks type Should be uint, not time.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 09 Nov 2017 12:24:16 +0200
parents 99a2b945045f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18740
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 #ifndef FILE_CREATE_LOCKED_H
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2 #define FILE_CREATE_LOCKED_H
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3
18744
57f8c2a6209b lib: file_create_locked() lock method is now configurable
Timo Sirainen <tss@iki.fi>
parents: 18740
diff changeset
4 #include "file-lock.h"
18740
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 struct file_create_settings {
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 /* 0 = try locking without waiting */
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 unsigned int lock_timeout_secs;
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9
18744
57f8c2a6209b lib: file_create_locked() lock method is now configurable
Timo Sirainen <tss@iki.fi>
parents: 18740
diff changeset
10 enum file_lock_method lock_method;
18740
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 /* 0 = 0600 */
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 int mode;
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 /* 0 = default */
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 uid_t uid;
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 /* 0 = default */
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 gid_t gid;
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 const char *gid_origin;
22272
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
18
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
19 /* If parent directory doesn't exist, mkdir() it with this mode.
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
20 0 = don't mkdir(). The parent directories are assumed to be
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
21 potentially rmdir() simultaneously, so the mkdir()+locking may be
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
22 attempted multiple times. */
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
23 int mkdir_mode;
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
24 /* 0 = default */
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
25 uid_t mkdir_uid;
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
26 /* 0 = default */
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
27 gid_t mkdir_gid;
651fa80715a3 lib: file_create_locked() - Add settings to mkdir() missing parent directories
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 18744
diff changeset
28 const char *mkdir_gid_origin;
18740
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 };
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 /* Either open an existing file and lock it, or create the file locked.
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 The creation is done by creating a temp file and link()ing it to path.
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 If link() fails, opening is retried again. Returns fd on success,
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 -1 on error. errno is preserved for the last failed syscall, so most
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 importantly ENOENT could mean that the directory doesn't exist and EAGAIN
22501
99a2b945045f lib: file_lock_set_unlink_on_free() - Avoid unlink() if another process is waiting on the lock
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 22272
diff changeset
36 means locking timed out.
99a2b945045f lib: file_lock_set_unlink_on_free() - Avoid unlink() if another process is waiting on the lock
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 22272
diff changeset
37
99a2b945045f lib: file_lock_set_unlink_on_free() - Avoid unlink() if another process is waiting on the lock
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 22272
diff changeset
38 If this function is used to create lock files, file_lock_set_unlink_on_free()
99a2b945045f lib: file_lock_set_unlink_on_free() - Avoid unlink() if another process is waiting on the lock
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 22272
diff changeset
39 should be used for the resulting lock. It attempts to avoid unlinking the
99a2b945045f lib: file_lock_set_unlink_on_free() - Avoid unlink() if another process is waiting on the lock
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 22272
diff changeset
40 file if there are already other processes using the lock. That can help to
99a2b945045f lib: file_lock_set_unlink_on_free() - Avoid unlink() if another process is waiting on the lock
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 22272
diff changeset
41 avoid "Creating a locked file ... keeps failing" errors */
18740
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 int file_create_locked(const char *path, const struct file_create_settings *set,
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 struct file_lock **lock_r, bool *created_r,
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 const char **error_r);
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45
78bbfe4e4e8e lib: Added file_create_locked()
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 #endif