mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-11 05:33:47 +01:00
Introduce write_bytes_to_new_file().
Introduce write_bytes_to_new_file(), a function which writes bytes to a file only if that file did not exist.
This commit is contained in:
parent
fa013e1bc5
commit
f28014bf1a
@ -2137,13 +2137,12 @@ write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
|
|||||||
return write_chunks_to_file_impl(fname, chunks, flags);
|
return write_chunks_to_file_impl(fname, chunks, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** As write_str_to_file, but does not assume a NUL-terminated
|
/** Write <b>len</b> bytes, starting at <b>str</b>, to <b>fname</b>
|
||||||
* string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
|
using the open() flags passed in <b>flags</b>. */
|
||||||
int
|
static int
|
||||||
write_bytes_to_file(const char *fname, const char *str, size_t len,
|
write_bytes_to_file_impl(const char *fname, const char *str, size_t len,
|
||||||
int bin)
|
int flags)
|
||||||
{
|
{
|
||||||
int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
|
|
||||||
int r;
|
int r;
|
||||||
sized_chunk_t c = { str, len };
|
sized_chunk_t c = { str, len };
|
||||||
smartlist_t *chunks = smartlist_create();
|
smartlist_t *chunks = smartlist_create();
|
||||||
@ -2153,20 +2152,35 @@ write_bytes_to_file(const char *fname, const char *str, size_t len,
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** As write_str_to_file, but does not assume a NUL-terminated
|
||||||
|
* string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
|
||||||
|
int
|
||||||
|
write_bytes_to_file(const char *fname, const char *str, size_t len,
|
||||||
|
int bin)
|
||||||
|
{
|
||||||
|
return write_bytes_to_file_impl(fname, str, len,
|
||||||
|
OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT));
|
||||||
|
}
|
||||||
|
|
||||||
/** As write_bytes_to_file, but if the file already exists, append the bytes
|
/** As write_bytes_to_file, but if the file already exists, append the bytes
|
||||||
* to the end of the file instead of overwriting it. */
|
* to the end of the file instead of overwriting it. */
|
||||||
int
|
int
|
||||||
append_bytes_to_file(const char *fname, const char *str, size_t len,
|
append_bytes_to_file(const char *fname, const char *str, size_t len,
|
||||||
int bin)
|
int bin)
|
||||||
{
|
{
|
||||||
int flags = OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT);
|
return write_bytes_to_file_impl(fname, str, len,
|
||||||
int r;
|
OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT));
|
||||||
sized_chunk_t c = { str, len };
|
}
|
||||||
smartlist_t *chunks = smartlist_create();
|
|
||||||
smartlist_add(chunks, &c);
|
/** Like write_str_to_file(), but also return -1 if there was a file
|
||||||
r = write_chunks_to_file_impl(fname, chunks, flags);
|
already residing in <b>fname</b>. */
|
||||||
smartlist_free(chunks);
|
int
|
||||||
return r;
|
write_bytes_to_new_file(const char *fname, const char *str, size_t len,
|
||||||
|
int bin)
|
||||||
|
{
|
||||||
|
return write_bytes_to_file_impl(fname, str, len,
|
||||||
|
OPEN_FLAGS_DONT_REPLACE|
|
||||||
|
(bin?O_BINARY:O_TEXT));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read the contents of <b>filename</b> into a newly allocated
|
/** Read the contents of <b>filename</b> into a newly allocated
|
||||||
|
@ -315,6 +315,7 @@ int check_private_dir(const char *dirname, cpd_check_t check,
|
|||||||
const char *effective_user);
|
const char *effective_user);
|
||||||
#define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
|
#define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
|
||||||
#define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
|
#define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
|
||||||
|
#define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
|
||||||
typedef struct open_file_t open_file_t;
|
typedef struct open_file_t open_file_t;
|
||||||
int start_writing_to_file(const char *fname, int open_flags, int mode,
|
int start_writing_to_file(const char *fname, int open_flags, int mode,
|
||||||
open_file_t **data_out);
|
open_file_t **data_out);
|
||||||
@ -336,6 +337,8 @@ int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
|
|||||||
int bin);
|
int bin);
|
||||||
int append_bytes_to_file(const char *fname, const char *str, size_t len,
|
int append_bytes_to_file(const char *fname, const char *str, size_t len,
|
||||||
int bin);
|
int bin);
|
||||||
|
int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
|
||||||
|
int bin);
|
||||||
|
|
||||||
/** Flag for read_file_to_str: open the file in binary mode. */
|
/** Flag for read_file_to_str: open the file in binary mode. */
|
||||||
#define RFTS_BIN 1
|
#define RFTS_BIN 1
|
||||||
|
Loading…
Reference in New Issue
Block a user