a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen#ifndef FILE_CREATE_LOCKED_H
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen#define FILE_CREATE_LOCKED_H
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen
715fa0c143cf457c94bbac7554767c28413cf680Timo Sirainen#include "file-lock.h"
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainenstruct file_create_settings {
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen /* 0 = try locking without waiting */
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen unsigned int lock_timeout_secs;
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen
715fa0c143cf457c94bbac7554767c28413cf680Timo Sirainen enum file_lock_method lock_method;
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen /* 0 = 0600 */
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen int mode;
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen /* 0 = default */
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen uid_t uid;
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen /* 0 = default */
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen gid_t gid;
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen const char *gid_origin;
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen /* If parent directory doesn't exist, mkdir() it with this mode.
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen 0 = don't mkdir(). The parent directories are assumed to be
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen potentially rmdir() simultaneously, so the mkdir()+locking may be
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen attempted multiple times. */
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen int mkdir_mode;
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen /* 0 = default */
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen uid_t mkdir_uid;
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen /* 0 = default */
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen gid_t mkdir_gid;
d6e3deea85740b3af60e9f5bab32e98beba12d93Timo Sirainen const char *mkdir_gid_origin;
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen};
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen/* Either open an existing file and lock it, or create the file locked.
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen The creation is done by creating a temp file and link()ing it to path.
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen If link() fails, opening is retried again. Returns fd on success,
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen -1 on error. errno is preserved for the last failed syscall, so most
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen importantly ENOENT could mean that the directory doesn't exist and EAGAIN
3581ece16eb740a5003c4c11dd3c7d02839a24a4Timo Sirainen means locking timed out.
3581ece16eb740a5003c4c11dd3c7d02839a24a4Timo Sirainen
3581ece16eb740a5003c4c11dd3c7d02839a24a4Timo Sirainen If this function is used to create lock files, file_lock_set_unlink_on_free()
3581ece16eb740a5003c4c11dd3c7d02839a24a4Timo Sirainen should be used for the resulting lock. It attempts to avoid unlinking the
3581ece16eb740a5003c4c11dd3c7d02839a24a4Timo Sirainen file if there are already other processes using the lock. That can help to
3581ece16eb740a5003c4c11dd3c7d02839a24a4Timo Sirainen avoid "Creating a locked file ... keeps failing" errors */
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainenint file_create_locked(const char *path, const struct file_create_settings *set,
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen struct file_lock **lock_r, bool *created_r,
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen const char **error_r);
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen
a63907c14bd96eec1493eb893a043137c1799485Timo Sirainen#endif