Skip to content

Commit be81e51

Browse files
author
Michael Runge
committed
Use more aggressive sync writing to applypatch.
We have seen cases where the boot partition is patched, but upon recovery the partition appears to be corrupted. Open up all patched files/partitions with O_SYNC, and do not ignore the errors from fsync/close operations. Bug: 18170529 Change-Id: I392ad0a321d937c4ad02eaeea9170be384a4744b
1 parent 042c3cd commit be81e51

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

applypatch/applypatch.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
309309
// Save the contents of the given FileContents object under the given
310310
// filename. Return 0 on success.
311311
int SaveFileContents(const char* filename, const FileContents* file) {
312-
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
312+
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR);
313313
if (fd < 0) {
314314
printf("failed to open \"%s\" for write: %s\n",
315315
filename, strerror(errno));
@@ -324,8 +324,14 @@ int SaveFileContents(const char* filename, const FileContents* file) {
324324
close(fd);
325325
return -1;
326326
}
327-
fsync(fd);
328-
close(fd);
327+
if (fsync(fd) != 0) {
328+
printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
329+
return -1;
330+
}
331+
if (close(fd) != 0) {
332+
printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
333+
return -1;
334+
}
329335

330336
if (chmod(filename, file->st.st_mode) != 0) {
331337
printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
@@ -408,7 +414,7 @@ int WriteToPartition(unsigned char* data, size_t len,
408414
{
409415
size_t start = 0;
410416
int success = 0;
411-
int fd = open(partition, O_RDWR);
417+
int fd = open(partition, O_RDWR | O_SYNC);
412418
if (fd < 0) {
413419
printf("failed to open %s: %s\n", partition, strerror(errno));
414420
return -1;
@@ -433,7 +439,22 @@ int WriteToPartition(unsigned char* data, size_t len,
433439
}
434440
start += written;
435441
}
436-
fsync(fd);
442+
if (fsync(fd) != 0) {
443+
printf("failed to sync to %s (%s)\n",
444+
partition, strerror(errno));
445+
return -1;
446+
}
447+
if (close(fd) != 0) {
448+
printf("failed to close %s (%s)\n",
449+
partition, strerror(errno));
450+
return -1;
451+
}
452+
fd = open(partition, O_RDONLY);
453+
if (fd < 0) {
454+
printf("failed to reopen %s for verify (%s)\n",
455+
partition, strerror(errno));
456+
return -1;
457+
}
437458

438459
// drop caches so our subsequent verification read
439460
// won't just be reading the cache.
@@ -919,7 +940,8 @@ static int GenerateTarget(FileContents* source_file,
919940
strcpy(outname, target_filename);
920941
strcat(outname, ".patch");
921942

922-
output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
943+
output = open(outname, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
944+
S_IRUSR | S_IWUSR);
923945
if (output < 0) {
924946
printf("failed to open output file %s: %s\n",
925947
outname, strerror(errno));
@@ -950,8 +972,14 @@ static int GenerateTarget(FileContents* source_file,
950972
}
951973

952974
if (output >= 0) {
953-
fsync(output);
954-
close(output);
975+
if (fsync(output) != 0) {
976+
printf("failed to fsync file \"%s\" (%s)\n", outname, strerror(errno));
977+
result = 1;
978+
}
979+
if (close(output) != 0) {
980+
printf("failed to close file \"%s\" (%s)\n", outname, strerror(errno));
981+
result = 1;
982+
}
955983
}
956984

957985
if (result != 0) {

0 commit comments

Comments
 (0)