diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/settings.c | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/apps/settings.c b/apps/settings.c index 3f257e093a..c7dd2cf9ab 100644 --- a/apps/settings.c +++ b/apps/settings.c | |||
@@ -96,6 +96,29 @@ struct system_status global_status; | |||
96 | #include "usb-ibasso.h" | 96 | #include "usb-ibasso.h" |
97 | #endif | 97 | #endif |
98 | 98 | ||
99 | #ifdef ROCKBOX_NO_TEMP_SETTINGS_FILE /* Overwrites same file each time */ | ||
100 | #define CONFIGFILE_TEMP CONFIGFILE | ||
101 | #define NVRAM_FILE_TEMP NVRAM_FILE | ||
102 | #define rename_temp_file(a,b,c) | ||
103 | #else /* creates temp files on save, renames next load, saves old file if desired */ | ||
104 | #define CONFIGFILE_TEMP CONFIGFILE".new" | ||
105 | #define NVRAM_FILE_TEMP NVRAM_FILE".new" | ||
106 | static void rename_temp_file(const char *tempfile, | ||
107 | const char *file, | ||
108 | const char *oldfile) | ||
109 | { | ||
110 | /* if tempfile does not exist -- Return | ||
111 | * if oldfile is supplied -- Rename file to oldfile | ||
112 | * if tempfile does exist -- Rename tempfile to file | ||
113 | */ | ||
114 | if (file_exists(tempfile)) | ||
115 | { | ||
116 | if (oldfile != NULL && file_exists(file)) | ||
117 | rename(file, oldfile); | ||
118 | rename(tempfile, file); | ||
119 | } | ||
120 | } | ||
121 | #endif | ||
99 | 122 | ||
100 | long lasttime = 0; | 123 | long lasttime = 0; |
101 | 124 | ||
@@ -112,6 +135,7 @@ static char nvram_buffer[NVRAM_BLOCK_SIZE]; | |||
112 | 135 | ||
113 | static bool read_nvram_data(char* buf, int max_len) | 136 | static bool read_nvram_data(char* buf, int max_len) |
114 | { | 137 | { |
138 | rename_temp_file(NVRAM_FILE_TEMP, NVRAM_FILE, NVRAM_FILE".old"); | ||
115 | unsigned crc32 = 0xffffffff; | 139 | unsigned crc32 = 0xffffffff; |
116 | int var_count = 0, i = 0, buf_pos = 0; | 140 | int var_count = 0, i = 0, buf_pos = 0; |
117 | int fd = open(NVRAM_FILE, O_RDONLY); | 141 | int fd = open(NVRAM_FILE, O_RDONLY); |
@@ -182,7 +206,7 @@ static bool write_nvram_data(char* buf, int max_len) | |||
182 | crc32 = crc_32(&buf[NVRAM_DATA_START], | 206 | crc32 = crc_32(&buf[NVRAM_DATA_START], |
183 | max_len-NVRAM_DATA_START-1,0xffffffff); | 207 | max_len-NVRAM_DATA_START-1,0xffffffff); |
184 | memcpy(&buf[4],&crc32,4); | 208 | memcpy(&buf[4],&crc32,4); |
185 | fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY, 0666); | 209 | fd = open(NVRAM_FILE_TEMP,O_CREAT|O_TRUNC|O_WRONLY, 0666); |
186 | if (fd >= 0) | 210 | if (fd >= 0) |
187 | { | 211 | { |
188 | int len = write(fd,buf,max_len); | 212 | int len = write(fd,buf,max_len); |
@@ -203,6 +227,7 @@ void settings_load(int which) | |||
203 | read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE); | 227 | read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE); |
204 | if (which&SETTINGS_HD) | 228 | if (which&SETTINGS_HD) |
205 | { | 229 | { |
230 | rename_temp_file(CONFIGFILE_TEMP, CONFIGFILE, CONFIGFILE".old"); | ||
206 | settings_load_config(CONFIGFILE, false); | 231 | settings_load_config(CONFIGFILE, false); |
207 | settings_load_config(FIXEDSETTINGSFILE, false); | 232 | settings_load_config(FIXEDSETTINGSFILE, false); |
208 | } | 233 | } |
@@ -571,7 +596,7 @@ static void flush_global_status_callback(void) | |||
571 | static void flush_config_block_callback(void) | 596 | static void flush_config_block_callback(void) |
572 | { | 597 | { |
573 | write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE); | 598 | write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE); |
574 | settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED); | 599 | settings_write_config(CONFIGFILE_TEMP, SETTINGS_SAVE_CHANGED); |
575 | } | 600 | } |
576 | 601 | ||
577 | void reset_runtime(void) { | 602 | void reset_runtime(void) { |
@@ -609,6 +634,11 @@ int settings_save(void) | |||
609 | 634 | ||
610 | bool settings_save_config(int options) | 635 | bool settings_save_config(int options) |
611 | { | 636 | { |
637 | /* if we have outstanding temp files it would be a good idea to flush | ||
638 | them before the user starts saving things */ | ||
639 | rename_temp_file(NVRAM_FILE_TEMP, NVRAM_FILE, NULL); /* dont overwrite .old */ | ||
640 | rename_temp_file(CONFIGFILE_TEMP, CONFIGFILE, NULL); /* files from last boot */ | ||
641 | |||
612 | char filename[MAX_PATH]; | 642 | char filename[MAX_PATH]; |
613 | const char *folder, *namebase; | 643 | const char *folder, *namebase; |
614 | switch (options) | 644 | switch (options) |