From: Felix Fietkau Date: Tue, 23 Sep 2008 16:12:40 +0000 (+0000) Subject: firmware-utils: when using open with O_CREAT and O_WRONLY, also use O_TRUNC to ensure... X-Git-Url: http://git.cdn.openwrt.org/?a=commitdiff_plain;h=0c734f8fb340d5e562dcddbbde48f8deb40ae0ca;p=project%2Ffirmware-utils.git firmware-utils: when using open with O_CREAT and O_WRONLY, also use O_TRUNC to ensure that overwritten files have the right size (fixes #3505) SVN-Revision: 12667 --- diff --git a/src/add_header.c b/src/add_header.c index 946068c..37775c3 100644 --- a/src/add_header.c +++ b/src/add_header.c @@ -122,7 +122,7 @@ int main(int argc, char **argv) memcpy(buf, &header, sizeof(header)); // write the buf - if ((fd = open(argv[3], O_CREAT|O_WRONLY,0644)) < 0 + if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0 || write(fd, buf, buflen) != buflen || close(fd) < 0) { diff --git a/src/motorola-bin.c b/src/motorola-bin.c index 680249c..58563ad 100644 --- a/src/motorola-bin.c +++ b/src/motorola-bin.c @@ -170,7 +170,7 @@ int main(int argc, char **argv) exit(3); } else { // all is well, write the file without the prefix - if ((fd = open(argv[3], O_CREAT|O_WRONLY,0644)) < 0 + if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0 || write(fd, trx + sizeof(struct motorola), len - sizeof(struct motorola)) != len - sizeof(struct motorola) || close(fd) < 0) { @@ -211,7 +211,7 @@ int main(int argc, char **argv) firmware->crc = htonl(crc32buf((unsigned char *)&firmware->flags, sizeof(firmware->flags) + len)); // write the firmware - if ((fd = open(argv[3], O_CREAT|O_WRONLY,0644)) < 0 + if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0 || write(fd, firmware, sizeof(struct motorola) + len) != sizeof(struct motorola) + len || close(fd) < 0) { diff --git a/src/ptgen.c b/src/ptgen.c index 8733e25..d94aabb 100644 --- a/src/ptgen.c +++ b/src/ptgen.c @@ -143,7 +143,7 @@ static int gen_ptable(int nr) printf("%ld\n", ((long) len * 512)); } - if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) < 0) { + if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { fprintf(stderr, "Can't open output file '%s'\n",filename); return -1; }