-
Notifications
You must be signed in to change notification settings - Fork 6
/
swap_files_err_handling.c
141 lines (122 loc) · 2.61 KB
/
swap_files_err_handling.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
typedef struct
{
int fds[5];
} ctx_t;
void init_ctx(ctx_t *ctx)
{
for (int i = 0; i < 5; i++)
{
ctx->fds[i] = -1;
}
}
void cleanup(ctx_t *ctx)
{
int old_errno = errno;
for (int i = 0; i < 5; i++)
{
if (ctx->fds[i] >= 0)
{
close(ctx->fds[i]);
}
}
errno = old_errno;
}
void add_fd_to_ctx(ctx_t *ctx, int fd)
{
for (int i = 0; i < 5; i++)
{
if (ctx->fds[i] < 0)
{
ctx->fds[i] = fd;
return;
}
}
cleanup(ctx);
errx(1, "too many open files");
}
int open_safe(ctx_t *ctx, const char *filename, int flags, mode_t mode)
{
int fd = open(filename, flags, mode);
if (fd < 0)
{
cleanup(ctx);
err(1, "could not open %s", filename);
}
add_fd_to_ctx(ctx, fd);
return fd;
}
off_t lseek_safe(ctx_t *ctx, int fd, off_t offset, int whence)
{
off_t result = lseek(fd, offset, whence);
if (result < 0)
{
cleanup(ctx);
err(1, "could not seek");
}
return result;
}
int ftruncate_safe(ctx_t *ctx, int fd, off_t length)
{
int result = ftruncate(fd, length);
if (result < 0)
{
cleanup(ctx);
err(1, "could not truncate");
}
return result;
}
void copy_data(ctx_t *ctx, int fd_from, int fd_to)
{
char c[4096];
ssize_t read_size;
ssize_t write_size;
while ((read_size = read(fd_from, c, sizeof(c))) > 0)
{
write_size = write(fd_to, &c, read_size);
if (write_size < 0)
{
cleanup(ctx);
err(1, "Error while writing");
}
if (write_size != read_size)
{
cleanup(ctx);
errx(1, "Did not write everything at once");
}
}
if (read_size != 0)
{
cleanup(ctx);
err(2, "Error while reading");
}
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
errx(1, "err");
}
ctx_t ctx;
init_ctx(&ctx);
int fd1 = open_safe(&ctx, argv[1], O_RDWR, 0666);
int fd2 = open_safe(&ctx, argv[2], O_RDWR, 0666);
int fd3 = open_safe(&ctx, "my_temp_file", O_CREAT | O_RDWR | O_TRUNC, 0666);
copy_data(&ctx, fd1, fd3);
lseek_safe(&ctx, fd1, 0, SEEK_SET);
ftruncate_safe(&ctx, fd1, 0);
copy_data(&ctx, fd2, fd1);
lseek_safe(&ctx, fd2, 0, SEEK_SET);
lseek_safe(&ctx, fd3, 0, SEEK_SET);
ftruncate_safe(&ctx, fd2, 0);
copy_data(&ctx, fd3, fd2);
cleanup(&ctx);
exit(0);
}