diff options
author | Yoshihisa Uchida <uchida@rockbox.org> | 2010-04-11 06:39:37 +0000 |
---|---|---|
committer | Yoshihisa Uchida <uchida@rockbox.org> | 2010-04-11 06:39:37 +0000 |
commit | 9d633740a402d9b390120b30798661d325fad049 (patch) | |
tree | 91a83a606a59b69fd0d5d9418e798c64053471fe | |
parent | bbbf5290a53da815fe5cfc0f4b14ef56cb4f3eb7 (diff) | |
download | rockbox-9d633740a402d9b390120b30798661d325fad049.tar.gz rockbox-9d633740a402d9b390120b30798661d325fad049.zip |
viewer plugin: when the setting file loads or saves, converts to new version settings file.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@25583 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/plugins/viewer.c | 214 |
1 files changed, 175 insertions, 39 deletions
diff --git a/apps/plugins/viewer.c b/apps/plugins/viewer.c index 890ed629f4..5678c5ef7d 100644 --- a/apps/plugins/viewer.c +++ b/apps/plugins/viewer.c | |||
@@ -1923,51 +1923,55 @@ static void viewer_default_preferences(void) | |||
1923 | prefs.encoding = rb->global_settings->default_codepage; | 1923 | prefs.encoding = rb->global_settings->default_codepage; |
1924 | } | 1924 | } |
1925 | 1925 | ||
1926 | static bool viewer_read_preferences(int pfd, int version) | 1926 | static bool viewer_read_preferences(int pfd, int version, struct preferences *prf) |
1927 | { | 1927 | { |
1928 | unsigned char buf[PREFERENCES_SIZE]; | 1928 | unsigned char buf[PREFERENCES_SIZE]; |
1929 | unsigned char *p = buf; | 1929 | unsigned char *p = buf; |
1930 | int read_size = PREFERENCES_SIZE; | ||
1930 | 1931 | ||
1931 | if (rb->read(pfd, buf, sizeof(buf)) != sizeof(buf)) | 1932 | if (version == 0) |
1933 | read_size--; | ||
1934 | |||
1935 | if (rb->read(pfd, buf, read_size) != read_size) | ||
1932 | return false; | 1936 | return false; |
1933 | 1937 | ||
1934 | prefs.word_mode = *p++; | 1938 | prf->word_mode = *p++; |
1935 | prefs.line_mode = *p++; | 1939 | prf->line_mode = *p++; |
1936 | prefs.view_mode = *p++; | 1940 | prf->view_mode = *p++; |
1937 | if (version > 0) | 1941 | if (version > 0) |
1938 | prefs.alignment = *p++; | 1942 | prf->alignment = *p++; |
1939 | else | 1943 | else |
1940 | prefs.alignment = LEFT; | 1944 | prf->alignment = LEFT; |
1941 | prefs.encoding = *p++; | 1945 | prf->encoding = *p++; |
1942 | prefs.scrollbar_mode = *p++; | 1946 | prf->scrollbar_mode = *p++; |
1943 | prefs.need_scrollbar = *p++; | 1947 | prf->need_scrollbar = *p++; |
1944 | prefs.page_mode = *p++; | 1948 | prf->page_mode = *p++; |
1945 | prefs.header_mode = *p++; | 1949 | prf->header_mode = *p++; |
1946 | prefs.footer_mode = *p++; | 1950 | prf->footer_mode = *p++; |
1947 | prefs.scroll_mode = *p++; | 1951 | prf->scroll_mode = *p++; |
1948 | prefs.autoscroll_speed = *p++; | 1952 | prf->autoscroll_speed = *p++; |
1949 | rb->memcpy(prefs.font, p, MAX_PATH); | 1953 | rb->memcpy(prf->font, p, MAX_PATH); |
1950 | return true; | 1954 | return true; |
1951 | } | 1955 | } |
1952 | 1956 | ||
1953 | static bool viewer_write_preferences(int pfd) | 1957 | static bool viewer_write_preferences(int pfd, const struct preferences *prf) |
1954 | { | 1958 | { |
1955 | unsigned char buf[PREFERENCES_SIZE]; | 1959 | unsigned char buf[PREFERENCES_SIZE]; |
1956 | unsigned char *p = buf; | 1960 | unsigned char *p = buf; |
1957 | 1961 | ||
1958 | *p++ = prefs.word_mode; | 1962 | *p++ = prf->word_mode; |
1959 | *p++ = prefs.line_mode; | 1963 | *p++ = prf->line_mode; |
1960 | *p++ = prefs.view_mode; | 1964 | *p++ = prf->view_mode; |
1961 | *p++ = prefs.alignment; | 1965 | *p++ = prf->alignment; |
1962 | *p++ = prefs.encoding; | 1966 | *p++ = prf->encoding; |
1963 | *p++ = prefs.scrollbar_mode; | 1967 | *p++ = prf->scrollbar_mode; |
1964 | *p++ = prefs.need_scrollbar; | 1968 | *p++ = prf->need_scrollbar; |
1965 | *p++ = prefs.page_mode; | 1969 | *p++ = prf->page_mode; |
1966 | *p++ = prefs.header_mode; | 1970 | *p++ = prf->header_mode; |
1967 | *p++ = prefs.footer_mode; | 1971 | *p++ = prf->footer_mode; |
1968 | *p++ = prefs.scroll_mode; | 1972 | *p++ = prf->scroll_mode; |
1969 | *p++ = prefs.autoscroll_speed; | 1973 | *p++ = prf->autoscroll_speed; |
1970 | rb->memcpy(p, prefs.font, MAX_PATH); | 1974 | rb->memcpy(p, prf->font, MAX_PATH); |
1971 | 1975 | ||
1972 | return (rb->write(pfd, buf, sizeof(buf)) == sizeof(buf)); | 1976 | return (rb->write(pfd, buf, sizeof(buf)) == sizeof(buf)); |
1973 | } | 1977 | } |
@@ -2066,7 +2070,7 @@ static bool viewer_load_global_settings(void) | |||
2066 | (rb->memcmp(buf, GLOBAL_SETTINGS_HEADER, GLOBAL_SETTINGS_H_SIZE - 1) == 0)) | 2070 | (rb->memcmp(buf, GLOBAL_SETTINGS_HEADER, GLOBAL_SETTINGS_H_SIZE - 1) == 0)) |
2067 | { | 2071 | { |
2068 | version = buf[GLOBAL_SETTINGS_H_SIZE - 1] - GLOBAL_SETTINGS_FIRST_VERSION; | 2072 | version = buf[GLOBAL_SETTINGS_H_SIZE - 1] - GLOBAL_SETTINGS_FIRST_VERSION; |
2069 | res = viewer_read_preferences(sfd, version); | 2073 | res = viewer_read_preferences(sfd, version, &prefs); |
2070 | } | 2074 | } |
2071 | rb->close(sfd); | 2075 | rb->close(sfd); |
2072 | return res; | 2076 | return res; |
@@ -2075,13 +2079,16 @@ static bool viewer_load_global_settings(void) | |||
2075 | static bool viewer_save_global_settings(void) | 2079 | static bool viewer_save_global_settings(void) |
2076 | { | 2080 | { |
2077 | int sfd = rb->open(GLOBAL_SETTINGS_TMP_FILE, O_WRONLY|O_CREAT|O_TRUNC); | 2081 | int sfd = rb->open(GLOBAL_SETTINGS_TMP_FILE, O_WRONLY|O_CREAT|O_TRUNC); |
2082 | unsigned char buf[GLOBAL_SETTINGS_H_SIZE]; | ||
2078 | 2083 | ||
2079 | if (sfd < 0) | 2084 | if (sfd < 0) |
2080 | return false; | 2085 | return false; |
2081 | 2086 | ||
2082 | if (rb->write(sfd, &GLOBAL_SETTINGS_HEADER, GLOBAL_SETTINGS_H_SIZE) | 2087 | rb->memcpy(buf, GLOBAL_SETTINGS_HEADER, GLOBAL_SETTINGS_H_SIZE - 1); |
2083 | != GLOBAL_SETTINGS_H_SIZE || | 2088 | buf[GLOBAL_SETTINGS_H_SIZE - 1] = GLOBAL_SETTINGS_VERSION; |
2084 | !viewer_write_preferences(sfd)) | 2089 | |
2090 | if (rb->write(sfd, buf, GLOBAL_SETTINGS_H_SIZE) != GLOBAL_SETTINGS_H_SIZE || | ||
2091 | !viewer_write_preferences(sfd, &prefs)) | ||
2085 | { | 2092 | { |
2086 | rb->close(sfd); | 2093 | rb->close(sfd); |
2087 | rb->remove(GLOBAL_SETTINGS_TMP_FILE); | 2094 | rb->remove(GLOBAL_SETTINGS_TMP_FILE); |
@@ -2093,6 +2100,101 @@ static bool viewer_save_global_settings(void) | |||
2093 | return true; | 2100 | return true; |
2094 | } | 2101 | } |
2095 | 2102 | ||
2103 | static bool viewer_convert_settings(int sfd, int dfd, int old_ver) | ||
2104 | { | ||
2105 | struct preferences new_prefs; | ||
2106 | off_t old_pos; | ||
2107 | off_t new_pos; | ||
2108 | unsigned char buf[MAX_PATH + 2]; | ||
2109 | int settings_size; | ||
2110 | |||
2111 | rb->read(sfd, buf, MAX_PATH + 2); | ||
2112 | rb->write(dfd, buf, MAX_PATH + 2); | ||
2113 | |||
2114 | settings_size = (buf[MAX_PATH] << 8) | buf[MAX_PATH + 1]; | ||
2115 | |||
2116 | old_pos = rb->lseek(sfd, 0, SEEK_CUR); | ||
2117 | new_pos = rb->lseek(dfd, 0, SEEK_CUR); | ||
2118 | |||
2119 | /* | ||
2120 | * when the settings size != preferences size + bookmarks size, | ||
2121 | * settings data are considered to be old version. | ||
2122 | */ | ||
2123 | if (old_ver > 0 && ((settings_size - PREFERENCES_SIZE) % 8) == 0) | ||
2124 | old_ver = 0; | ||
2125 | |||
2126 | if (!viewer_read_preferences(sfd, old_ver, &new_prefs)) | ||
2127 | return false; | ||
2128 | |||
2129 | if (!viewer_write_preferences(dfd, &new_prefs)) | ||
2130 | return false; | ||
2131 | |||
2132 | settings_size -= (rb->lseek(sfd, 0, SEEK_CUR) - old_pos); | ||
2133 | |||
2134 | if (settings_size > 0) | ||
2135 | { | ||
2136 | rb->read(sfd, buf, settings_size); | ||
2137 | rb->write(dfd, buf, settings_size); | ||
2138 | } | ||
2139 | |||
2140 | settings_size = rb->lseek(dfd, 0, SEEK_CUR) - new_pos; | ||
2141 | buf[0] = settings_size >> 8; | ||
2142 | buf[1] = settings_size; | ||
2143 | rb->lseek(dfd, new_pos - 2, SEEK_SET); | ||
2144 | rb->write(dfd, buf, 2); | ||
2145 | rb->lseek(dfd, settings_size, SEEK_CUR); | ||
2146 | return true; | ||
2147 | } | ||
2148 | |||
2149 | static bool viewer_convert_settings_file(void) | ||
2150 | { | ||
2151 | unsigned char buf[SETTINGS_H_SIZE+2]; | ||
2152 | int sfd; | ||
2153 | int tfd; | ||
2154 | int i; | ||
2155 | int fcount; | ||
2156 | int version; | ||
2157 | bool res = true; | ||
2158 | |||
2159 | if ((sfd = rb->open(SETTINGS_FILE, O_RDONLY)) < 0) | ||
2160 | return false; | ||
2161 | |||
2162 | if ((tfd = rb->open(SETTINGS_TMP_FILE, O_WRONLY|O_CREAT|O_TRUNC)) < 0) | ||
2163 | { | ||
2164 | rb->close(sfd); | ||
2165 | return false; | ||
2166 | } | ||
2167 | |||
2168 | rb->read(sfd, buf, SETTINGS_H_SIZE + 2); | ||
2169 | |||
2170 | version = buf[SETTINGS_H_SIZE - 1] - SETTINGS_FIRST_VERSION; | ||
2171 | fcount = (buf[SETTINGS_H_SIZE] << 8) | buf[SETTINGS_H_SIZE + 1]; | ||
2172 | buf[SETTINGS_H_SIZE - 1] = SETTINGS_VERSION; | ||
2173 | |||
2174 | rb->write(tfd, buf, SETTINGS_H_SIZE + 2); | ||
2175 | |||
2176 | for (i = 0; i < fcount; i++) | ||
2177 | { | ||
2178 | if (!viewer_convert_settings(sfd, tfd, version)) | ||
2179 | { | ||
2180 | res = false; | ||
2181 | break; | ||
2182 | } | ||
2183 | } | ||
2184 | |||
2185 | rb->close(sfd); | ||
2186 | rb->close(tfd); | ||
2187 | |||
2188 | if (!res) | ||
2189 | rb->remove(SETTINGS_TMP_FILE); | ||
2190 | else | ||
2191 | { | ||
2192 | rb->remove(SETTINGS_FILE); | ||
2193 | rb->rename(SETTINGS_TMP_FILE, SETTINGS_FILE); | ||
2194 | } | ||
2195 | return res; | ||
2196 | } | ||
2197 | |||
2096 | static bool viewer_load_settings(void) | 2198 | static bool viewer_load_settings(void) |
2097 | { | 2199 | { |
2098 | unsigned char buf[MAX_PATH+2]; | 2200 | unsigned char buf[MAX_PATH+2]; |
@@ -2119,6 +2221,15 @@ static bool viewer_load_settings(void) | |||
2119 | goto read_end; | 2221 | goto read_end; |
2120 | } | 2222 | } |
2121 | 2223 | ||
2224 | if (buf[SETTINGS_H_SIZE - 1] != SETTINGS_VERSION) | ||
2225 | { | ||
2226 | rb->close(sfd); | ||
2227 | if (!viewer_convert_settings_file()) | ||
2228 | goto read_end; | ||
2229 | |||
2230 | return viewer_load_settings(); | ||
2231 | } | ||
2232 | |||
2122 | version = buf[SETTINGS_H_SIZE - 1] - SETTINGS_FIRST_VERSION; | 2233 | version = buf[SETTINGS_H_SIZE - 1] - SETTINGS_FIRST_VERSION; |
2123 | fcount = (buf[SETTINGS_H_SIZE] << 8) | buf[SETTINGS_H_SIZE+1]; | 2234 | fcount = (buf[SETTINGS_H_SIZE] << 8) | buf[SETTINGS_H_SIZE+1]; |
2124 | for (i = 0; i < fcount; i++) | 2235 | for (i = 0; i < fcount; i++) |
@@ -2127,13 +2238,27 @@ static bool viewer_load_settings(void) | |||
2127 | break; | 2238 | break; |
2128 | 2239 | ||
2129 | size = (buf[MAX_PATH] << 8) | buf[MAX_PATH+1]; | 2240 | size = (buf[MAX_PATH] << 8) | buf[MAX_PATH+1]; |
2241 | |||
2242 | /* | ||
2243 | * when the settings size != preferences size + bookmarks size, | ||
2244 | * the settings file converts to the newer. | ||
2245 | */ | ||
2246 | if (version > 0 && ((size - PREFERENCES_SIZE) % 8) == 0) | ||
2247 | { | ||
2248 | rb->close(sfd); | ||
2249 | if (!viewer_convert_settings_file()) | ||
2250 | break; | ||
2251 | |||
2252 | return viewer_load_settings(); | ||
2253 | } | ||
2254 | |||
2130 | if (rb->strcmp(buf, file_name)) | 2255 | if (rb->strcmp(buf, file_name)) |
2131 | { | 2256 | { |
2132 | if (rb->lseek(sfd, size, SEEK_CUR) < 0) | 2257 | if (rb->lseek(sfd, size, SEEK_CUR) < 0) |
2133 | break; | 2258 | break; |
2134 | continue; | 2259 | continue; |
2135 | } | 2260 | } |
2136 | if (!viewer_read_preferences(sfd, version)) | 2261 | if (!viewer_read_preferences(sfd, version, &prefs)) |
2137 | break; | 2262 | break; |
2138 | 2263 | ||
2139 | res = viewer_read_bookmark_infos(sfd); | 2264 | res = viewer_read_bookmark_infos(sfd); |
@@ -2264,11 +2389,21 @@ static bool viewer_save_settings(void) | |||
2264 | if (ofd >= 0) | 2389 | if (ofd >= 0) |
2265 | { | 2390 | { |
2266 | if ((rb->read(ofd, buf, SETTINGS_H_SIZE+2) != SETTINGS_H_SIZE+2) || | 2391 | if ((rb->read(ofd, buf, SETTINGS_H_SIZE+2) != SETTINGS_H_SIZE+2) || |
2267 | rb->memcmp(buf, SETTINGS_HEADER, SETTINGS_H_SIZE)) | 2392 | rb->memcmp(buf, SETTINGS_HEADER, SETTINGS_H_SIZE - 1)) |
2268 | { | 2393 | { |
2269 | rb->close(ofd); | 2394 | rb->close(ofd); |
2270 | goto save_err; | 2395 | goto save_err; |
2271 | } | 2396 | } |
2397 | |||
2398 | if (buf[SETTINGS_H_SIZE - 1] != SETTINGS_VERSION) | ||
2399 | { | ||
2400 | rb->close(ofd); | ||
2401 | if (!viewer_convert_settings_file()) | ||
2402 | goto save_err; | ||
2403 | |||
2404 | viewer_save_settings(); | ||
2405 | } | ||
2406 | |||
2272 | fcount = (buf[SETTINGS_H_SIZE] << 8) | buf[SETTINGS_H_SIZE+1]; | 2407 | fcount = (buf[SETTINGS_H_SIZE] << 8) | buf[SETTINGS_H_SIZE+1]; |
2273 | 2408 | ||
2274 | for (i = 0; i < fcount; i++) | 2409 | for (i = 0; i < fcount; i++) |
@@ -2322,8 +2457,9 @@ static bool viewer_save_settings(void) | |||
2322 | } | 2457 | } |
2323 | else | 2458 | else |
2324 | { | 2459 | { |
2325 | rb->memcpy(buf, SETTINGS_HEADER, SETTINGS_H_SIZE); | 2460 | rb->memcpy(buf, SETTINGS_HEADER, SETTINGS_H_SIZE - 1); |
2326 | buf[SETTINGS_H_SIZE] = 0; | 2461 | buf[SETTINGS_H_SIZE-1] = SETTINGS_VERSION; |
2462 | buf[SETTINGS_H_SIZE ] = 0; | ||
2327 | buf[SETTINGS_H_SIZE+1] = 0; | 2463 | buf[SETTINGS_H_SIZE+1] = 0; |
2328 | if (rb->write(tfd, buf, SETTINGS_H_SIZE+2) != SETTINGS_H_SIZE+2) | 2464 | if (rb->write(tfd, buf, SETTINGS_H_SIZE+2) != SETTINGS_H_SIZE+2) |
2329 | goto save_err; | 2465 | goto save_err; |
@@ -2340,7 +2476,7 @@ static bool viewer_save_settings(void) | |||
2340 | if (rb->write(tfd, buf, MAX_PATH+2) != MAX_PATH+2) | 2476 | if (rb->write(tfd, buf, MAX_PATH+2) != MAX_PATH+2) |
2341 | goto save_err; | 2477 | goto save_err; |
2342 | 2478 | ||
2343 | if (!viewer_write_preferences(tfd)) | 2479 | if (!viewer_write_preferences(tfd, &prefs)) |
2344 | goto save_err; | 2480 | goto save_err; |
2345 | 2481 | ||
2346 | if (!viewer_write_bookmark_infos(tfd)) | 2482 | if (!viewer_write_bookmark_infos(tfd)) |