Browse Source

Merge pull request #1234 from Skytrias/master

fix strip-semicolon using original file handle size
gingerBill 3 years ago
parent
commit
8868bcf57a
2 changed files with 8 additions and 4 deletions
  1. 4 1
      src/gb/gb.h
  2. 4 3
      src/main.cpp

+ 4 - 1
src/gb/gb.h

@@ -6030,8 +6030,11 @@ gb_inline b32 gb_file_copy(char const *existing_filename, char const *new_filena
 
 	struct stat stat_existing;
 	fstat(existing_fd, &stat_existing);
-
 	size = sendfile(new_fd, existing_fd, 0, stat_existing.st_size);
+	
+	// set new handle to wanted size for safety
+	int i = ftruncate(new_fd, size);
+	GB_ASSERT(i == 0);
 
 	close(new_fd);
 	close(existing_fd);

+ 4 - 3
src/main.cpp

@@ -2144,8 +2144,8 @@ int strip_semicolons(Parser *parser) {
 		generated_count += 1;
 		
 		i64 written = 0;
-		defer (gb_file_truncate(&f, written));
-		
+		defer (err = gb_file_truncate(&f, written));
+
 		debugf("Write file with stripped tokens: %s\n", filename);
 		err = write_file_with_stripped_tokens(&f, file->file, &written);
 		if (err) {
@@ -2181,6 +2181,7 @@ int strip_semicolons(Parser *parser) {
 		}
 		
 		debugf("Copy '%s' to '%s'\n", new_fullpath, old_fullpath);
+
 		if (!gb_file_copy(new_fullpath, old_fullpath, false)) {
 			gb_printf_err("failed to copy '%s' to '%s'\n", old_fullpath, new_fullpath);
 			debugf("Copy '%s' to '%s'\n", old_fullpath_backup, old_fullpath);
@@ -2190,7 +2191,7 @@ int strip_semicolons(Parser *parser) {
 			failed = true;
 			break;
 		}
-		
+
 		debugf("Remove '%s'\n", old_fullpath_backup);
 		if (!gb_file_remove(old_fullpath_backup)) {
 			gb_printf_err("failed to remove '%s'\n", old_fullpath_backup);