-
Notifications
You must be signed in to change notification settings - Fork 820
/
writefileheader.c
82 lines (68 loc) · 1.55 KB
/
writefileheader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "rvn.h"
#include "status_codes.h"
#include "internal_posix.h"
EXPORT int32_t
rvn_write_header(const char *path,
void *header, int32_t size,
int32_t *detailed_error_code)
{
int32_t rc;
bool sync_is_needed = false;
int32_t fd = open(path, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
if (fd == -1)
{
rc = FAIL_OPEN_FILE;
goto error_cleanup;
}
int32_t remaining = size;
struct stat buf;
if (stat(path, &buf) == -1)
{
rc = FAIL_STAT_FILE;
goto error_cleanup;
}
if (buf.st_size != remaining)
{
sync_is_needed = true;
if (rvn_ftruncate(fd, size) == -1)
{
rc = FAIL_TRUNCATE_FILE;
goto error_cleanup;
}
}
while (remaining > 0)
{
uint64_t written = write(fd, header, (uint64_t)remaining);
if (written == -1)
{
rc = FAIL_WRITE_FILE;
goto error_cleanup;
}
remaining -= (int)written;
header += written;
}
if (_flush_file(fd) == -1)
{
rc = FAIL_FLUSH_FILE;
goto error_cleanup;
}
close(fd);
fd = -1;
if (sync_is_needed == true)
return _sync_directory_for(path, detailed_error_code);
return SUCCESS;
error_cleanup:
*detailed_error_code = errno;
if (fd != -1)
close(fd);
return rc;
}