more protection against data loss

This commit is contained in:
Kartik K. Agaram 2021-12-17 08:46:11 -08:00
parent 59ef5da1d9
commit 12b0a2a7b6
2 changed files with 13 additions and 3 deletions

View File

@ -376,7 +376,7 @@ void save_to_current_definition_and_editor_buffer (lua_State *L, const char *def
int outfd = mkstemp(outfilename);
if (outfd == -1) {
endwin();
perror("error in creating temporary file");
perror("save_to_current_definition_and_editor_buffer: error in creating temporary file");
abort();
}
FILE *out = fdopen(outfd, "w");
@ -634,7 +634,7 @@ void save_note_to_editor_buffer (lua_State *L, int cursor) {
int outfd = mkstemp(outfilename);
if (outfd == -1) {
endwin();
perror("error in creating temporary file");
perror("save_note_to_editor_buffer: error in creating temporary file");
abort();
}
FILE *out = fdopen(outfd, "w");

View File

@ -124,11 +124,20 @@ static void emit_multiline_string(FILE* out, const char* value) {
}
}
extern int mkstemp(char *template);
extern FILE *fdopen(int fd, const char *mode);
void save_tlv(lua_State* L, char* filename) {
lua_getglobal(L, "teliva_program");
int history_array = lua_gettop(L);
int history_array_size = luaL_getn(L, history_array);
FILE *out = fopen(filename, "w");
char outfilename[] = "teliva_out_XXXXXX";
int outfd = mkstemp(outfilename);
if (outfd == -1) {
endwin();
perror("save_tlv: error in creating temporary file");
abort();
}
FILE *out = fdopen(outfd, "w");
fprintf(out, "# .tlv file generated by https://github.com/akkartik/teliva\n");
fprintf(out, "# You may edit it if you are careful; however, you may see cryptic errors if you\n");
fprintf(out, "# violate Teliva's assumptions.\n");
@ -172,5 +181,6 @@ void save_tlv(lua_State* L, char* filename) {
lua_pop(L, 1);
}
fclose(out);
rename(outfilename, filename);
lua_pop(L, 1);
}