1/* 2 * Copyright 2019 Intel Corporation 3 * SPDX-License-Identifier: MIT 4 * 5 * File operations helpers 6 */ 7 8#ifndef _OS_FILE_H_ 9#define _OS_FILE_H_ 10 11#include <stdbool.h> 12#include <stdio.h> 13 14#ifdef __cplusplus 15extern "C" { 16#endif 17 18/* 19 * Create a new file and opens it for writing-only. 20 * If the given filename already exists, nothing is done and NULL is returned. 21 * `errno` gets set to the failure reason; if that is not EEXIST, the caller 22 * might want to do something other than trying again. 23 */ 24FILE * 25os_file_create_unique(const char *filename, int filemode); 26 27/* 28 * Duplicate a file descriptor, making sure not to keep it open after an exec*() 29 */ 30int 31os_dupfd_cloexec(int fd); 32 33/* 34 * Read a file. 35 * Returns a char* that the caller must free(), or NULL and sets errno. 36 * If size is not null and no error occurred it's set to the size of the 37 * file. 38 * Reads files as binary and includes a NUL terminator after the end of the 39 * returned buffer. 40 */ 41char * 42os_read_file(const char *filename, size_t *size); 43 44/* 45 * Try to determine if two file descriptors reference the same file description 46 * 47 * Return values: 48 * - 0: They reference the same file description 49 * - > 0: They do not reference the same file description 50 * - < 0: Unable to determine whether they reference the same file description 51 */ 52int 53os_same_file_description(int fd1, int fd2); 54 55#ifdef __cplusplus 56} 57#endif 58 59#endif /* _OS_FILE_H_ */ 60