19685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey/* 29685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Copyright (C) 2012 The Android Open Source Project 39685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * 49685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License"); 59685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * you may not use this file except in compliance with the License. 69685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * You may obtain a copy of the License at 79685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * 89685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * http://www.apache.org/licenses/LICENSE-2.0 99685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * 109685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Unless required by applicable law or agreed to in writing, software 119685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS, 129685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * See the License for the specific language governing permissions and 149685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * limitations under the License. 159685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey */ 169685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey 179685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#ifndef __CUTILS_FS_H 189685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#define __CUTILS_FS_H 199685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey 209685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <sys/types.h> 219685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey 22ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey/* 23ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey * TEMP_FAILURE_RETRY is defined by some, but not all, versions of 24ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's 25ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey * not already defined, then define it here. 26ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey */ 27ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey#ifndef TEMP_FAILURE_RETRY 28ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey/* Used to retry syscalls that can return EINTR. */ 29ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey#define TEMP_FAILURE_RETRY(exp) ({ \ 30ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey typeof (exp) _rc; \ 31ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey do { \ 32ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey _rc = (exp); \ 33ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey } while (_rc == -1 && errno == EINTR); \ 34ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey _rc; }) 35ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey#endif 36ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey 379685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#ifdef __cplusplus 389685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyextern "C" { 399685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#endif 409685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey 419685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey/* 429685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Ensure that directory exists with given mode and owners. 439685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey */ 449685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyextern int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid); 459685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey 469685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey/* 479685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Read single plaintext integer from given file, correctly handling files 489685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * partially written with fs_write_atomic_int(). 499685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey */ 509685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyextern int fs_read_atomic_int(const char* path, int* value); 519685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey 529685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey/* 539685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Write single plaintext integer to given file, creating backup while 549685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * in progress. 559685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey */ 569685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyextern int fs_write_atomic_int(const char* path, int value); 579685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey 589685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#ifdef __cplusplus 599685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey} 609685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#endif 619685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey 629685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#endif /* __CUTILS_FS_H */ 63