Commit 78456ca
committed
Fix: resolve issue #4478 by using a temporary file for non-append writes
To address the issue where a failed write operation results in an empty file, we can use a temporary file for non-append writes. This ensures that the original file is only replaced once the new content is fully written and committed.
**Key Changes:**
1. **Temporary File Handling:**
- For non-append writes, a temporary file is created in the same directory as the target file.
- All write operations are performed on the temporary file first.
2. **Atomic Commit:**
- The temporary file is only renamed to the target path during `Commit()`, ensuring atomic replacement.
- If `Commit()` fails, the temporary file is cleaned up.
3. **Error Handling:**
- `Cancel()` properly removes temporary files if the operation is aborted.
- `Close()` is made idempotent to handle multiple calls safely.
4. **Data Integrity:**
- Directory sync after rename ensures metadata persistence.
- Proper file flushing and syncing before rename operations.
Signed-off-by: Oded Porat <onporat@gmail.com>1 parent 523791c commit 78456ca
2 files changed
Lines changed: 72 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
| 191 | + | |
| 192 | + | |
192 | 193 | | |
193 | | - | |
194 | 194 | | |
195 | 195 | | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
196 | 206 | | |
197 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
198 | 213 | | |
199 | 214 | | |
200 | 215 | | |
201 | 216 | | |
202 | | - | |
203 | 217 | | |
204 | 218 | | |
205 | | - | |
| 219 | + | |
206 | 220 | | |
207 | 221 | | |
208 | 222 | | |
| |||
337 | 351 | | |
338 | 352 | | |
339 | 353 | | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
346 | 362 | | |
347 | 363 | | |
348 | | - | |
| 364 | + | |
349 | 365 | | |
350 | | - | |
351 | | - | |
352 | | - | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
353 | 371 | | |
354 | 372 | | |
355 | 373 | | |
| |||
372 | 390 | | |
373 | 391 | | |
374 | 392 | | |
375 | | - | |
| 393 | + | |
376 | 394 | | |
377 | 395 | | |
378 | 396 | | |
| |||
397 | 415 | | |
398 | 416 | | |
399 | 417 | | |
400 | | - | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
401 | 427 | | |
402 | 428 | | |
403 | 429 | | |
| |||
412 | 438 | | |
413 | 439 | | |
414 | 440 | | |
415 | | - | |
416 | 441 | | |
417 | 442 | | |
418 | 443 | | |
419 | 444 | | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
420 | 465 | | |
421 | 466 | | |
422 | 467 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
410 | 410 | | |
411 | 411 | | |
412 | 412 | | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
413 | 416 | | |
414 | 417 | | |
415 | 418 | | |
| |||
0 commit comments