blob: d1d4cf28d6d79aae31495b6cf8f445b17d45bb2f [file] [log] [blame]
Jeff Sharkey96851942012-08-27 15:03:37 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __CUTILS_FS_H
18#define __CUTILS_FS_H
19
20#include <sys/types.h>
21
Jeff Sharkeyddb17332012-09-13 14:47:23 -070022/*
23 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
24 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
25 * not already defined, then define it here.
26 */
27#ifndef TEMP_FAILURE_RETRY
28/* Used to retry syscalls that can return EINTR. */
29#define TEMP_FAILURE_RETRY(exp) ({ \
30 typeof (exp) _rc; \
31 do { \
32 _rc = (exp); \
33 } while (_rc == -1 && errno == EINTR); \
34 _rc; })
35#endif
36
Jeff Sharkey96851942012-08-27 15:03:37 -070037#ifdef __cplusplus
38extern "C" {
39#endif
40
41/*
42 * Ensure that directory exists with given mode and owners.
43 */
44extern int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
45
46/*
47 * Read single plaintext integer from given file, correctly handling files
48 * partially written with fs_write_atomic_int().
49 */
50extern int fs_read_atomic_int(const char* path, int* value);
51
52/*
53 * Write single plaintext integer to given file, creating backup while
54 * in progress.
55 */
56extern int fs_write_atomic_int(const char* path, int value);
57
Jeff Sharkey44d63422013-09-12 09:44:48 -070058/*
59 * Ensure that all directories along given path exist, creating parent
60 * directories as needed. Validates that given path is absolute and that
61 * it contains no relative "." or ".." paths or symlinks. Last path segment
62 * is treated as filename and ignored, unless the path ends with "/".
63 */
64extern int fs_mkdirs(const char* path, mode_t mode);
65
Jeff Sharkey96851942012-08-27 15:03:37 -070066#ifdef __cplusplus
67}
68#endif
69
70#endif /* __CUTILS_FS_H */