2019-01-28 15:44:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Intel Corporation
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
* File operations helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _OS_FILE_H_
|
|
|
|
#define _OS_FILE_H_
|
|
|
|
|
2020-01-06 17:24:52 +00:00
|
|
|
#include <stdbool.h>
|
2019-06-03 17:51:37 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-01-28 15:44:12 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2019-06-03 17:51:37 +01:00
|
|
|
/*
|
|
|
|
* Create a new file and opens it for writing-only.
|
|
|
|
* If the given filename already exists, nothing is done and NULL is returned.
|
|
|
|
* `errno` gets set to the failure reason; if that is not EEXIST, the caller
|
|
|
|
* might want to do something other than trying again.
|
|
|
|
*/
|
|
|
|
FILE *
|
|
|
|
os_file_create_unique(const char *filename, int filemode);
|
|
|
|
|
2020-06-05 00:08:33 +01:00
|
|
|
/*
|
|
|
|
* Duplicate a file descriptor, making sure not to keep it open after an exec*()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
os_dupfd_cloexec(int fd);
|
|
|
|
|
2019-01-28 15:44:12 +00:00
|
|
|
/*
|
|
|
|
* Read a file.
|
|
|
|
* Returns a char* that the caller must free(), or NULL and sets errno.
|
2021-03-11 21:29:07 +00:00
|
|
|
* If size is not null and no error occurred it's set to the size of the
|
2020-03-16 09:48:48 +00:00
|
|
|
* file.
|
2021-03-11 21:29:07 +00:00
|
|
|
* Reads files as binary and includes a NUL terminator after the end of the
|
|
|
|
* returned buffer.
|
2019-01-28 15:44:12 +00:00
|
|
|
*/
|
|
|
|
char *
|
2020-03-16 09:48:48 +00:00
|
|
|
os_read_file(const char *filename, size_t *size);
|
2019-01-28 15:44:12 +00:00
|
|
|
|
2020-01-06 17:24:52 +00:00
|
|
|
/*
|
2020-02-18 18:04:00 +00:00
|
|
|
* Try to determine if two file descriptors reference the same file description
|
|
|
|
*
|
|
|
|
* Return values:
|
|
|
|
* - 0: They reference the same file description
|
|
|
|
* - > 0: They do not reference the same file description
|
|
|
|
* - < 0: Unable to determine whether they reference the same file description
|
2020-01-06 17:24:52 +00:00
|
|
|
*/
|
2020-02-18 18:04:00 +00:00
|
|
|
int
|
2020-01-06 17:24:52 +00:00
|
|
|
os_same_file_description(int fd1, int fd2);
|
|
|
|
|
2019-01-28 15:44:12 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _OS_FILE_H_ */
|