| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 1 | /* | 
|  | 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> | 
| Mark Salyzyn | 4887842 | 2014-05-22 16:08:52 -0700 | [diff] [blame] | 21 | #include <unistd.h> | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 22 |  | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 23 | /* | 
|  | 24 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of | 
|  | 25 | * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's | 
|  | 26 | * not already defined, then define it here. | 
|  | 27 | */ | 
|  | 28 | #ifndef TEMP_FAILURE_RETRY | 
|  | 29 | /* Used to retry syscalls that can return EINTR. */ | 
|  | 30 | #define TEMP_FAILURE_RETRY(exp) ({         \ | 
|  | 31 | typeof (exp) _rc;                      \ | 
|  | 32 | do {                                   \ | 
|  | 33 | _rc = (exp);                       \ | 
|  | 34 | } while (_rc == -1 && errno == EINTR); \ | 
|  | 35 | _rc; }) | 
|  | 36 | #endif | 
|  | 37 |  | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 38 | #ifdef __cplusplus | 
|  | 39 | extern "C" { | 
|  | 40 | #endif | 
|  | 41 |  | 
|  | 42 | /* | 
| Jeff Sharkey | cf94fe1 | 2016-01-12 12:42:32 -0700 | [diff] [blame] | 43 | * Ensure that directory exists with given mode and owners.  If it exists | 
|  | 44 | * with a different mode or owners, they are fixed to match the given values. | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 45 | */ | 
|  | 46 | extern int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid); | 
|  | 47 |  | 
|  | 48 | /* | 
| Jeff Sharkey | cf94fe1 | 2016-01-12 12:42:32 -0700 | [diff] [blame] | 49 | * Ensure that directory exists with given mode and owners.  If it exists | 
| Jeff Sharkey | 8146403 | 2016-01-14 11:53:42 -0700 | [diff] [blame] | 50 | * with different owners, they are not fixed and -1 is returned. | 
| Jeff Sharkey | cf94fe1 | 2016-01-12 12:42:32 -0700 | [diff] [blame] | 51 | */ | 
|  | 52 | extern int fs_prepare_dir_strict(const char* path, mode_t mode, uid_t uid, gid_t gid); | 
|  | 53 |  | 
|  | 54 | /* | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 55 | * Ensure that file exists with given mode and owners.  If it exists | 
|  | 56 | * with different owners, they are not fixed and -1 is returned. | 
|  | 57 | */ | 
|  | 58 | extern int fs_prepare_file_strict(const char* path, mode_t mode, uid_t uid, gid_t gid); | 
|  | 59 |  | 
|  | 60 |  | 
|  | 61 | /* | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 62 | * Read single plaintext integer from given file, correctly handling files | 
|  | 63 | * partially written with fs_write_atomic_int(). | 
|  | 64 | */ | 
|  | 65 | extern int fs_read_atomic_int(const char* path, int* value); | 
|  | 66 |  | 
|  | 67 | /* | 
|  | 68 | * Write single plaintext integer to given file, creating backup while | 
|  | 69 | * in progress. | 
|  | 70 | */ | 
|  | 71 | extern int fs_write_atomic_int(const char* path, int value); | 
|  | 72 |  | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 73 | /* | 
|  | 74 | * Ensure that all directories along given path exist, creating parent | 
|  | 75 | * directories as needed.  Validates that given path is absolute and that | 
|  | 76 | * it contains no relative "." or ".." paths or symlinks.  Last path segment | 
|  | 77 | * is treated as filename and ignored, unless the path ends with "/". | 
|  | 78 | */ | 
|  | 79 | extern int fs_mkdirs(const char* path, mode_t mode); | 
|  | 80 |  | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 81 | #ifdef __cplusplus | 
|  | 82 | } | 
|  | 83 | #endif | 
|  | 84 |  | 
|  | 85 | #endif /* __CUTILS_FS_H */ |