-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.c
More file actions
228 lines (189 loc) · 7.18 KB
/
patch.c
File metadata and controls
228 lines (189 loc) · 7.18 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "patch.h"
#include "utils.h"
Patch *create_patch(char *filepath, int mode, size_t memory_size, Point *memory) {
Patch *patch = calloc(1, sizeof(Patch) + memory_size + 1);
strcpy(patch->filepath, filepath);
patch->mode = mode;
patch->memory_size = memory_size;
memcpy(patch->pts, memory, memory_size);
return patch;
}
void visualize_patch(Patch *patch) {
printf("----Visualizing Patch: ------\n");
printf("filepath: |%s|\n", patch->filepath);
printf("mode: |%d|\n", patch->mode);
if (patch->mode == MODE_MODIFY) {
printf("num pts: |%ld|\n", patch->memory_size);
printf("Memory size: |%ld|\n", patch->memory_size / sizeof(Point));
for (int i = 0; i < patch->memory_size / sizeof(Point); i++) {
printf("|%d| |%d| |%c|\n", patch->pts[i].type, patch->pts[i].pos, patch->pts[i].ch);
}
printf("----End visualizing. ------\n");
}
}
void write_patch(char *filename, Patch *patch) {
int out_file = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (out_file == -1) {
err();
}
size_t struct_byte_size = sizeof(Patch) + patch->memory_size; // min size is sizeof(Patch)
int write_status = write(out_file, patch, struct_byte_size);
if (write_status == -1) {
err();
}
close(out_file);
}
Patch *read_patch(char *filename) {
int in_file = open(filename, O_RDONLY);
if (in_file == -1) {
err();
}
struct stat file_stat;
int stat_status = stat(filename, &file_stat);
if (stat_status == -1) {
err();
}
size_t patch_file_size = file_stat.st_size;
Patch *patch = calloc(1, patch_file_size);
int read_status = read(in_file, patch, patch_file_size);
if (read_status == -1) {
err();
}
close(in_file);
return patch;
}
/*
void apply_modify_patch(Patch *patch) {
// Step 1: Get str from file
int in_file = open(patch->filepath, O_RDONLY);
if (in_file == -1) {
if (errno == ENOENT) {
printf("MODIFY PATCH FAILED: FILE |%s| DOES NOT EXIST!!\n", patch->filepath);
exit(errno);
}
err();
}
struct stat file_stat;
int stat_status = stat(patch->filepath, &file_stat);
if (stat_status == -1) {
err();
}
size_t in_file_size = file_stat.st_size;
char *str = calloc(1, in_file_size + 1);
int read_status = read(in_file, str, in_file_size);
if (read_status == -1) {
err();
}
printf("Read file to buffer str: |%s|\n", str);
close(in_file);
// Step 2: Generate new text from patch
char *head = (patch->memory); // point to start
while (head < patch->memory + patch->memory_size) { // loop until hits end of memory buffer
// THIS IS THE ONE CHANGE BLOCK
// grab char
char plus_or_minus = *(head);
head += sizeof(char);
// grab int
int location = *(int *)(head);
head += sizeof(int);
// grab int
int number_of_bytes = *(int *)(head);
head += sizeof(int);
printf("Plus or Minus: |%c|\n", plus_or_minus);
printf("Location: |%d|\n", location);
printf("number_of_bytes: |%d|\n", number_of_bytes);
if (plus_or_minus == MODE_PLUS) {
// assume worst case scenario that buffer is exactly size of str
size_t new_str_len = strlen(str) + number_of_bytes + 1; // +1 for null byte
str = realloc(str, sizeof(char) * (new_str_len));
str[new_str_len - 1] = '\0'; // just in case so you don't shoot yourself in the foot
// shift stuff after our location (including the byte at location) to the right
int max_number_of_bytes_to_copy = strlen(str) - location;
strncpy(str + location + number_of_bytes, str + location, max_number_of_bytes_to_copy); // dest, src, # to copy
// now read bytes
for (int i = 0; i < number_of_bytes; i++) {
// grab char
char new_byte = *(head);
str[location + i] = new_byte;
head += sizeof(char); // advance to next byte
}
} else if (plus_or_minus == MODE_MINUS) {
// only a shift left is needed (no need to shrink str)
// then move null byte back accordingly (strlen in the write will handle this!)
}
}
// Step 3: Write to file!
int out_file = open(patch->filepath, O_WRONLY | O_TRUNC, 0644);
if (out_file == -1) {
err();
}
int write_status = write(out_file, str, strlen(str));
if (write_status == -1) {
err();
}
close(out_file);
}
*/
void apply_touch_patch(Patch *patch) {
char *filepath = patch->filepath;
int fd = open(filepath, O_WRONLY | O_CREAT | O_EXCL, 0644); // O_EXCL is "error if create and file exists"
if (fd == -1) {
if (errno == EEXIST) {
printf("TOUCH PATCH FAILED: FILE |%s| ALREADY EXISTS!!\n", patch->filepath);
exit(errno);
}
err();
}
if (patch->memory_size > 0) {
printf("Trying to write text from patch memory to newly created file...\n");
// treat memory as JUST a str (char*) NOT a fancy encoded modification patch memory thingy
int write_status = write(fd, patch->pts, patch->memory_size);
if (write_status == -1) {
err();
}
}
close(fd);
}
void apply_delete_patch(Patch *patch) {
char *filepath = patch->filepath;
int removal_status = remove(filepath);
if (removal_status == -1) {
err();
}
}
// int main() {
// char *txt = calloc(100, sizeof(char));
// strcpy(txt, "hi\nline2\nline3");
// printf("Creating patch...\n");
// Patch *mypatch = create_patch("dit_test_dir/test.txt", MODE_TOUCH, strlen(txt), (Point *)txt); // do not do strlen() + 1 bc we want to exclude null byte
// printf("Patch created!\n");
// visualize_patch(mypatch);
// char filename[] = ".dit/patch1.patch";
// printf("Writing patch...\n");
// write_patch(filename, mypatch);
// printf("Patch written!\n");
// Patch *mypatch2 = read_patch(filename);
// printf("Patch 2 read... visualizing now!\n");
// visualize_patch(mypatch2);
// printf("Writing the patch that we read off disk...\n");
// apply_touch_patch(mypatch2);
// // char mem[] = {
// // '+', 3, 0, 0, 0, 2, 0, 0, 0, 'n', 'm',
// // // '+', 7, 0, 0, 0, 2, 0, 0, 0, 'x', 'y'
// // };
// // // '+', 3, 0, 0, 0, 2, 0, 0, 0, 'n', 'm' is one change
// // // '+', 7, 0, 0, 0, 2, 0, 0, 0, 'x', 'y' is one change
// // Patch *test_patch = create_patch("test/matthew.txt", MODE_MODIFY, sizeof(mem), mem);
// // write_patch(".dit/matthew1.patch", test_patch);
// // apply_modify_patch(test_patch);
// printf("About to create a file using a touch patch...\n");
// Patch *test_touch_patch = create_patch("test/hi.txt", MODE_TOUCH, 0, NULL);
// apply_touch_patch(test_touch_patch);
// printf("Created!\n");
// sleep(2);
// printf("About to delete a file using a touch patch...\n");
// Patch *test_removal_patch = create_patch("test/hi.txt", MODE_REMOVE, 0, NULL);
// apply_delete_patch(test_removal_patch);
// printf("Deleted!\n");
// return 0;
// }