Skip to content
Myr

fix stb_fclose to do a better job of preserving content

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parent 84e42c2 · Browse files · 1 file+90 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+90 added−38 removed

  • other:+90 added−38 removed

1 file+90 added−38 removed

modified:stb.h+90 added-38 removed
1022 unmodified lines
1023}10241025stb__wchar *stb__from_utf8_alt(char *str)1026{1027 static stb__wchar buffer[64];1028 return stb_from_utf8(buffer, str, 64);1027 static stb__wchar buffer[4096];1028 return stb_from_utf8(buffer, str, 4096);1029}10301031char *stb__to_utf8(stb__wchar *str)1032{
4401 unmodified lines
5434 char *name;5435 int errors;5436} stb__file_data;54375438FILE * stb_fopen(char *filename, char *mode)5438static FILE *stb__open_temp_file(char *temp_name, char *src_name, char *mode)5439{5440 FILE *f;5441 char name_full[4096];5442 char temp_full[sizeof(name_full) + 12];5440 int p;5441#ifdef _MSC_VER5442 int j;5443#endif5447 if (mode[0] != 'w' && !strchr(mode, '+'))5448 return stb__fopen(filename, mode);54495450 // save away the full path to the file so if the program5451 // changes the cwd everything still works right! unix has5452 // better ways to do this, but we have to work in windows5453 name_full[0] = '\0'; // stb_fullpath reads name_full[0]5454 if (stb_fullpath(name_full, sizeof(name_full), filename)==0)5455 return 0;54565444 FILE *f;5445 // try to generate a temporary file in the same directory5458 p = strlen(name_full)-1;5459 while (p > 0 && name_full[p] != '/' && name_full[p] != '\\'5460 && name_full[p] != ':' && name_full[p] != '~')5446 p = strlen(src_name)-1;5447 while (p > 0 && src_name[p] != '/' && src_name[p] != '\\'5448 && src_name[p] != ':' && src_name[p] != '~')5449 --p;5450 ++p;54515464 memcpy(temp_full, name_full, p);5452 memcpy(temp_name, src_name, p);54535454 #ifdef _MSC_VER5455 // try multiple times to make a temp file... just in5456 // case some other process makes the name first5457 for (j=0; j < 32; ++j) {5470 strcpy(temp_full+p, "stmpXXXXXX");5471 if (stb_mktemp(temp_full) == NULL)5458 strcpy(temp_name+p, "stmpXXXXXX");5459 if (stb_mktemp(temp_name) == NULL)5460 return 0;54615474 f = fopen(temp_full, mode);5462 f = fopen(temp_name, mode);5463 if (f != NULL)5464 break;5465 }5466 #else5467 {5480 strcpy(temp_full+p, "stmpXXXXXX");5468 strcpy(temp_name+p, "stmpXXXXXX");5469 #ifdef __MINGW32__5482 int fd = open(mktemp(temp_full), O_RDWR);5470 int fd = open(mktemp(temp_name), O_RDWR);5471 #else5484 int fd = mkstemp(temp_full);5472 int fd = mkstemp(temp_name);5473 #endif5474 if (fd == -1) return NULL;5475 f = fdopen(fd, mode);5476 if (f == NULL) {5489 unlink(temp_full);5477 unlink(temp_name);5478 close(fd);5479 return NULL;5480 }5481 }5482 #endif5483 return f;5484}548554865487FILE * stb_fopen(char *filename, char *mode)5488{5489 FILE *f;5490 char name_full[4096];5491 char temp_full[sizeof(name_full) + 12];54925493 // @TODO: if the file doesn't exist, we can also use the fastpath here5494 if (mode[0] != 'w' && !strchr(mode, '+'))5495 return stb__fopen(filename, mode);54965497 // save away the full path to the file so if the program5498 // changes the cwd everything still works right! unix has5499 // better ways to do this, but we have to work in windows5500 name_full[0] = '\0'; // stb_fullpath reads name_full[0]5501 if (stb_fullpath(name_full, sizeof(name_full), filename)==0)5502 return 0;55035504 f = stb__open_temp_file(temp_full, name_full, mode);5505 if (f != NULL) {5506 stb__file_data *d = (stb__file_data *) malloc(sizeof(*d));5507 if (!d) { assert(0); /* NOTREACHED */fclose(f); return NULL; }5508 if (stb__files == NULL) stb__files = stb_ptrmap_create();
34 unmodified lines
5543 ok = STB_TRUE; // report success if no change5544 }5545 }55465537 if (keep != stb_keep_no) {5538 if (stb_fexists(d->name) && remove(d->name)) {5539 // failed to delete old, so don't keep new5540 keep = stb_keep_no;5547 if (keep == stb_keep_no) {5548 remove(d->temp_name);5549 } else {5550 if (!stb_fexists(d->name)) {5551 // old file doesn't exist, so just move the new file over it5552 stb_rename(d->temp_name, d->name);5553 } else {5542 if (!stb_rename(d->temp_name, d->name))5543 ok = STB_TRUE;5544 else5545 keep=stb_keep_no;5554 // don't delete the old file yet in case there are troubles! First rename it!5555 char preserved_old_file[4096];55565557 // generate a temp filename in the same directory (also creates it, which we don't need)5558 FILE *dummy = stb__open_temp_file(preserved_old_file, d->name, "wb");5559 if (dummy != NULL) {5560 // we don't actually want the open file5561 fclose(dummy);55625563 // discard what we just created5564 remove(preserved_old_file); // if this fails, there's nothing we can do, and following logic handles it as best as possible anyway55655566 // move the existing file to the preserved name5567 if (0 != stb_rename(d->name, preserved_old_file)) { // 0 on success5568 // failed, state is:5569 // filename -> old file5570 // tempname -> new file5571 // keep tempname around so we don't lose data5572 } else {5573 // state is:5574 // preserved -> old file5575 // tempname -> new file5576 // move the new file to the old name5577 if (0 == stb_rename(d->temp_name, d->name)) {5578 // state is:5579 // preserved -> old file5580 // filename -> new file5581 ok = STB_TRUE;55825583 // 'filename -> new file' has always been the goal, so clean up5584 remove(preserved_old_file); // nothing to be done if it fails5585 } else {5586 // couldn't rename, so try renaming preserved file back55875588 // state is:5589 // preserved -> old file5590 // tempname -> new file5591 stb_rename(preserved_old_file, d->name);5592 // if the rename failed, there's nothing more we can do5593 }5594 }5595 } else {5596 // we couldn't get a temp filename. do this the naive way; the worst case failure here5597 // leaves the filename pointing to nothing and the new file as a tempfile5598 remove(d->name);5599 stb_rename(d->temp_name, d->name);5600 }5601 }5602 }56035549 if (keep == stb_keep_no)5550 remove(d->temp_name);55515604 free(d->temp_name);5605 free(d->name);5606 free(d);5607
8769 unmodified lines