summaryrefslogtreecommitdiff
path: root/apps/tagcache.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/tagcache.c')
-rw-r--r--apps/tagcache.c142
1 files changed, 99 insertions, 43 deletions
diff --git a/apps/tagcache.c b/apps/tagcache.c
index 1094c92a97..898263ef23 100644
--- a/apps/tagcache.c
+++ b/apps/tagcache.c
@@ -73,6 +73,7 @@
73#include "buffer.h" 73#include "buffer.h"
74#include "crc32.h" 74#include "crc32.h"
75#include "misc.h" 75#include "misc.h"
76#include "filefuncs.h"
76#include "settings.h" 77#include "settings.h"
77#include "dir.h" 78#include "dir.h"
78#include "structec.h" 79#include "structec.h"
@@ -292,15 +293,17 @@ static bool is_dircache_intact(void)
292static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write) 293static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
293{ 294{
294 int fd; 295 int fd;
295 char buf[MAX_PATH]; 296 char buf[MAX_PATH], path[MAX_PATH];
297 const char * file;
296 int rc; 298 int rc;
297 299
298 if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT) 300 if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT)
299 return -1; 301 return -1;
300 302
301 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag); 303 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag);
304 file = get_user_file_path(buf, IS_FILE | NEED_WRITE, path, sizeof(path));
302 305
303 fd = open(buf, write ? O_RDWR : O_RDONLY); 306 fd = open(file, write ? O_RDWR : O_RDONLY);
304 if (fd < 0) 307 if (fd < 0)
305 { 308 {
306 logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf); 309 logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf);
@@ -325,8 +328,12 @@ static int open_master_fd(struct master_header *hdr, bool write)
325{ 328{
326 int fd; 329 int fd;
327 int rc; 330 int rc;
331 char path[MAX_PATH];
328 332
329 fd = open(TAGCACHE_FILE_MASTER, write ? O_RDWR : O_RDONLY); 333 fd = open(get_user_file_path(TAGCACHE_FILE_MASTER,
334 IS_FILE|NEED_WRITE,
335 path, sizeof(path)),
336 write ? O_RDWR : O_RDONLY);
330 if (fd < 0) 337 if (fd < 0)
331 { 338 {
332 logf("master file open failed for R/W"); 339 logf("master file open failed for R/W");
@@ -668,9 +675,11 @@ static bool open_files(struct tagcache_search *tcs, int tag)
668{ 675{
669 if (tcs->idxfd[tag] < 0) 676 if (tcs->idxfd[tag] < 0)
670 { 677 {
671 char fn[MAX_PATH]; 678 char fn[MAX_PATH], path[MAX_PATH];
679 const char *file;
672 680
673 snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag); 681 snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag);
682 file = get_user_file_path(fn, IS_FILE | NEED_WRITE, path, sizeof(path));
674 tcs->idxfd[tag] = open(fn, O_RDONLY); 683 tcs->idxfd[tag] = open(fn, O_RDONLY);
675 } 684 }
676 685
@@ -1159,14 +1168,17 @@ static void remove_files(void)
1159 tc_stat.ready = false; 1168 tc_stat.ready = false;
1160 tc_stat.ramcache = false; 1169 tc_stat.ramcache = false;
1161 tc_stat.econ = false; 1170 tc_stat.econ = false;
1162 remove(TAGCACHE_FILE_MASTER); 1171 remove(get_user_file_path(TAGCACHE_FILE_MASTER, NEED_WRITE|IS_FILE,
1172 buf, sizeof(buf)));
1163 for (i = 0; i < TAG_COUNT; i++) 1173 for (i = 0; i < TAG_COUNT; i++)
1164 { 1174 {
1175 char buf2[MAX_PATH];
1165 if (TAGCACHE_IS_NUMERIC(i)) 1176 if (TAGCACHE_IS_NUMERIC(i))
1166 continue; 1177 continue;
1167 1178
1179 /* database_%d.tcd -> database_0.tcd */
1168 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, i); 1180 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, i);
1169 remove(buf); 1181 remove(get_user_file_path(buf, NEED_WRITE | IS_FILE, buf2, sizeof(buf2)));
1170 } 1182 }
1171} 1183}
1172 1184
@@ -1317,10 +1329,11 @@ bool tagcache_search_add_clause(struct tagcache_search *tcs,
1317 1329
1318 if (!TAGCACHE_IS_NUMERIC(clause->tag) && tcs->idxfd[clause->tag] < 0) 1330 if (!TAGCACHE_IS_NUMERIC(clause->tag) && tcs->idxfd[clause->tag] < 0)
1319 { 1331 {
1320 char buf[MAX_PATH]; 1332 char buf[MAX_PATH], path[MAX_PATH];
1321 1333 const char *file;
1322 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, clause->tag); 1334 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, clause->tag);
1323 tcs->idxfd[clause->tag] = open(buf, O_RDONLY); 1335 file = get_user_file_path(buf, IS_FILE | NEED_WRITE, path, sizeof(path));
1336 tcs->idxfd[clause->tag] = open(file, O_RDONLY);
1324 } 1337 }
1325 1338
1326 tcs->clause[tcs->clause_count] = clause; 1339 tcs->clause[tcs->clause_count] = clause;
@@ -2344,7 +2357,8 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2344 struct master_header tcmh; 2357 struct master_header tcmh;
2345 struct index_entry idxbuf[IDX_BUF_DEPTH]; 2358 struct index_entry idxbuf[IDX_BUF_DEPTH];
2346 int idxbuf_pos; 2359 int idxbuf_pos;
2347 char buf[TAG_MAXLEN+32]; 2360 char buf[TAG_MAXLEN+32], path[MAX_PATH];
2361 const char *file;
2348 int fd = -1, masterfd; 2362 int fd = -1, masterfd;
2349 bool error = false; 2363 bool error = false;
2350 int init; 2364 int init;
@@ -2492,7 +2506,8 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2492 * anything whether the index type is sorted or not. 2506 * anything whether the index type is sorted or not.
2493 */ 2507 */
2494 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, index_type); 2508 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, index_type);
2495 fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0666); 2509 file = get_user_file_path(buf, IS_FILE | NEED_WRITE, path, sizeof(path));
2510 fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
2496 if (fd < 0) 2511 if (fd < 0)
2497 { 2512 {
2498 logf("%s open fail", buf); 2513 logf("%s open fail", buf);
@@ -2512,18 +2527,21 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2512 } 2527 }
2513 } 2528 }
2514 2529
2530 file = get_user_file_path(TAGCACHE_FILE_MASTER,
2531 IS_FILE|NEED_WRITE,
2532 buf, sizeof(buf));
2515 /* Loading the tag lookup file as "master file". */ 2533 /* Loading the tag lookup file as "master file". */
2516 logf("Loading index file"); 2534 logf("Loading index file");
2517 masterfd = open(TAGCACHE_FILE_MASTER, O_RDWR); 2535 masterfd = open(file, O_RDWR);
2518 2536
2519 if (masterfd < 0) 2537 if (masterfd < 0)
2520 { 2538 {
2521 logf("Creating new DB"); 2539 logf("Creating new DB");
2522 masterfd = open(TAGCACHE_FILE_MASTER, O_WRONLY | O_CREAT | O_TRUNC, 0666); 2540 masterfd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
2523 2541
2524 if (masterfd < 0) 2542 if (masterfd < 0)
2525 { 2543 {
2526 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER); 2544 logf("Failure to create index file (%s)", file);
2527 close(fd); 2545 close(fd);
2528 return -2; 2546 return -2;
2529 } 2547 }
@@ -2831,6 +2849,8 @@ static bool commit(void)
2831{ 2849{
2832 struct tagcache_header tch; 2850 struct tagcache_header tch;
2833 struct master_header tcmh; 2851 struct master_header tcmh;
2852 char path[MAX_PATH];
2853 const char *file;
2834 int i, len, rc; 2854 int i, len, rc;
2835 int tmpfd; 2855 int tmpfd;
2836 int masterfd; 2856 int masterfd;
@@ -2844,7 +2864,10 @@ static bool commit(void)
2844 while (write_lock) 2864 while (write_lock)
2845 sleep(1); 2865 sleep(1);
2846 2866
2847 tmpfd = open(TAGCACHE_FILE_TEMP, O_RDONLY); 2867 file = get_user_file_path(TAGCACHE_FILE_TEMP,
2868 IS_FILE|NEED_WRITE, path, sizeof(path));
2869
2870 tmpfd = open(file, O_RDONLY);
2848 if (tmpfd < 0) 2871 if (tmpfd < 0)
2849 { 2872 {
2850 logf("nothing to commit"); 2873 logf("nothing to commit");
@@ -2860,7 +2883,7 @@ static bool commit(void)
2860 { 2883 {
2861 logf("incorrect tmpheader"); 2884 logf("incorrect tmpheader");
2862 close(tmpfd); 2885 close(tmpfd);
2863 remove(TAGCACHE_FILE_TEMP); 2886 remove(file);
2864 return false; 2887 return false;
2865 } 2888 }
2866 2889
@@ -2868,7 +2891,7 @@ static bool commit(void)
2868 { 2891 {
2869 logf("nothing to commit"); 2892 logf("nothing to commit");
2870 close(tmpfd); 2893 close(tmpfd);
2871 remove(TAGCACHE_FILE_TEMP); 2894 remove(file);
2872 return true; 2895 return true;
2873 } 2896 }
2874 2897
@@ -2876,7 +2899,8 @@ static bool commit(void)
2876 tc_stat.ready = check_all_headers(); 2899 tc_stat.ready = check_all_headers();
2877 2900
2878#ifdef HAVE_EEPROM_SETTINGS 2901#ifdef HAVE_EEPROM_SETTINGS
2879 remove(TAGCACHE_STATEFILE); 2902 remove(get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
2903 path, sizeof(path)));
2880#endif 2904#endif
2881 2905
2882 /* At first be sure to unload the ramcache! */ 2906 /* At first be sure to unload the ramcache! */
@@ -2966,7 +2990,7 @@ static bool commit(void)
2966 } 2990 }
2967 2991
2968 close(tmpfd); 2992 close(tmpfd);
2969 remove(TAGCACHE_FILE_TEMP); 2993 remove(file);
2970 2994
2971 tc_stat.commit_step = 0; 2995 tc_stat.commit_step = 0;
2972 2996
@@ -3386,15 +3410,18 @@ bool tagcache_import_changelog(void)
3386 struct tagcache_header tch; 3410 struct tagcache_header tch;
3387 int clfd; 3411 int clfd;
3388 long masterfd; 3412 long masterfd;
3389 char buf[2048]; 3413 char buf[MAX(MAX_PATH, 2048)];
3414 const char *file;
3390 3415
3391 if (!tc_stat.ready) 3416 if (!tc_stat.ready)
3392 return false; 3417 return false;
3393 3418
3394 while (read_lock) 3419 while (read_lock)
3395 sleep(1); 3420 sleep(1);
3396 3421
3397 clfd = open(TAGCACHE_FILE_CHANGELOG, O_RDONLY); 3422 file = get_user_file_path(TAGCACHE_FILE_CHANGELOG,
3423 IS_FILE|NEED_WRITE, buf, sizeof(buf));
3424 clfd = open(file, O_RDONLY);
3398 if (clfd < 0) 3425 if (clfd < 0)
3399 { 3426 {
3400 logf("failure to open changelog"); 3427 logf("failure to open changelog");
@@ -3436,7 +3463,8 @@ bool tagcache_create_changelog(struct tagcache_search *tcs)
3436{ 3463{
3437 struct master_header myhdr; 3464 struct master_header myhdr;
3438 struct index_entry idx; 3465 struct index_entry idx;
3439 char buf[TAG_MAXLEN+32]; 3466 const char *file;
3467 char buf[MAX(TAG_MAXLEN+32, MAX_PATH)];
3440 char temp[32]; 3468 char temp[32];
3441 int clfd; 3469 int clfd;
3442 int i, j; 3470 int i, j;
@@ -3448,7 +3476,9 @@ bool tagcache_create_changelog(struct tagcache_search *tcs)
3448 return false; 3476 return false;
3449 3477
3450 /* Initialize the changelog */ 3478 /* Initialize the changelog */
3451 clfd = open(TAGCACHE_FILE_CHANGELOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); 3479 file = get_user_file_path(TAGCACHE_FILE_CHANGELOG, IS_FILE | NEED_WRITE,
3480 buf, sizeof(buf));
3481 clfd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
3452 if (clfd < 0) 3482 if (clfd < 0)
3453 { 3483 {
3454 logf("failure to open changelog"); 3484 logf("failure to open changelog");
@@ -3766,11 +3796,15 @@ static bool allocate_tagcache(void)
3766static bool tagcache_dumpload(void) 3796static bool tagcache_dumpload(void)
3767{ 3797{
3768 struct statefile_header shdr; 3798 struct statefile_header shdr;
3799 char path[MAX_PATH];
3800 const char *file;
3769 int fd, rc; 3801 int fd, rc;
3770 long offpos; 3802 long offpos;
3771 int i; 3803 int i;
3772 3804
3773 fd = open(TAGCACHE_STATEFILE, O_RDONLY); 3805 file = get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
3806 path, sizeof(path));
3807 fd = open(file, O_RDONLY);
3774 if (fd < 0) 3808 if (fd < 0)
3775 { 3809 {
3776 logf("no tagcache statedump"); 3810 logf("no tagcache statedump");
@@ -3816,12 +3850,16 @@ static bool tagcache_dumpload(void)
3816static bool tagcache_dumpsave(void) 3850static bool tagcache_dumpsave(void)
3817{ 3851{
3818 struct statefile_header shdr; 3852 struct statefile_header shdr;
3853 char path[MAX_PATH];
3854 const char *file;
3819 int fd; 3855 int fd;
3820 3856
3821 if (!tc_stat.ramcache) 3857 if (!tc_stat.ramcache)
3822 return false; 3858 return false;
3823 3859
3824 fd = open(TAGCACHE_STATEFILE, O_WRONLY | O_CREAT | O_TRUNC, 0666); 3860 file = get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
3861 path, sizeof(path));
3862 fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
3825 if (fd < 0) 3863 if (fd < 0)
3826 { 3864 {
3827 logf("failed to create a statedump"); 3865 logf("failed to create a statedump");
@@ -3847,7 +3885,8 @@ static bool load_tagcache(void)
3847 long bytesleft = tc_stat.ramcache_allocated; 3885 long bytesleft = tc_stat.ramcache_allocated;
3848 struct index_entry *idx; 3886 struct index_entry *idx;
3849 int rc, fd; 3887 int rc, fd;
3850 char *p; 3888 char *p, path[MAX_PATH];
3889 const char *file;
3851 int i, tag; 3890 int i, tag;
3852 3891
3853# ifdef HAVE_DIRCACHE 3892# ifdef HAVE_DIRCACHE
@@ -3858,8 +3897,11 @@ static bool load_tagcache(void)
3858# endif 3897# endif
3859 3898
3860 logf("loading tagcache to ram..."); 3899 logf("loading tagcache to ram...");
3861 3900
3862 fd = open(TAGCACHE_FILE_MASTER, O_RDONLY); 3901 file = get_user_file_path(TAGCACHE_FILE_MASTER,
3902 IS_FILE|NEED_WRITE,
3903 path, sizeof(path));
3904 fd = open(file, O_RDONLY);
3863 if (fd < 0) 3905 if (fd < 0)
3864 { 3906 {
3865 logf("tagcache open failed"); 3907 logf("tagcache open failed");
@@ -4069,12 +4111,14 @@ static bool load_tagcache(void)
4069static bool check_deleted_files(void) 4111static bool check_deleted_files(void)
4070{ 4112{
4071 int fd; 4113 int fd;
4072 char buf[TAG_MAXLEN+32]; 4114 char buf[TAG_MAXLEN+32], path[MAX_PATH];
4115 const char *file;
4073 struct tagfile_entry tfe; 4116 struct tagfile_entry tfe;
4074 4117
4075 logf("reverse scan..."); 4118 logf("reverse scan...");
4076 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag_filename); 4119 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag_filename);
4077 fd = open(buf, O_RDONLY); 4120 file = get_user_file_path(buf, IS_FILE | NEED_WRITE, path, sizeof(path));
4121 fd = open(file, O_RDONLY);
4078 4122
4079 if (fd < 0) 4123 if (fd < 0)
4080 { 4124 {
@@ -4232,6 +4276,8 @@ void tagcache_build(const char *path)
4232{ 4276{
4233 struct tagcache_header header; 4277 struct tagcache_header header;
4234 bool ret; 4278 bool ret;
4279 char buf[MAX_PATH];
4280 const char *file;
4235 4281
4236 curpath[0] = '\0'; 4282 curpath[0] = '\0';
4237 data_size = 0; 4283 data_size = 0;
@@ -4244,19 +4290,21 @@ void tagcache_build(const char *path)
4244#endif 4290#endif
4245 4291
4246 logf("updating tagcache"); 4292 logf("updating tagcache");
4293
4294 file = get_user_file_path(TAGCACHE_FILE_TEMP,
4295 IS_FILE|NEED_WRITE, buf, sizeof(buf));
4247 4296
4248 cachefd = open(TAGCACHE_FILE_TEMP, O_RDONLY); 4297
4249 if (cachefd >= 0) 4298 if (file_exists(file))
4250 { 4299 {
4251 logf("skipping, cache already waiting for commit"); 4300 logf("skipping, cache already waiting for commit");
4252 close(cachefd);
4253 return ; 4301 return ;
4254 } 4302 }
4255 4303
4256 cachefd = open(TAGCACHE_FILE_TEMP, O_RDWR | O_CREAT | O_TRUNC, 0666); 4304 cachefd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
4257 if (cachefd < 0) 4305 if (cachefd < 0)
4258 { 4306 {
4259 logf("master file open failed: %s", TAGCACHE_FILE_TEMP); 4307 logf("master file open failed: %s", file);
4260 return ; 4308 return ;
4261 } 4309 }
4262 4310
@@ -4300,7 +4348,7 @@ void tagcache_build(const char *path)
4300#endif 4348#endif
4301 if (commit()) 4349 if (commit())
4302 { 4350 {
4303 remove(TAGCACHE_FILE_TEMP); 4351 remove(file);
4304 logf("tagcache built!"); 4352 logf("tagcache built!");
4305 } 4353 }
4306#ifdef __PCTOOL__ 4354#ifdef __PCTOOL__
@@ -4345,7 +4393,12 @@ void tagcache_unload_ramcache(void)
4345{ 4393{
4346 tc_stat.ramcache = false; 4394 tc_stat.ramcache = false;
4347 /* Just to make sure there is no statefile present. */ 4395 /* Just to make sure there is no statefile present. */
4348 // remove(TAGCACHE_STATEFILE); 4396
4397#if 0
4398 char path[MAX_PATH];
4399 remove(get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
4400 path, sizeof(path)));
4401#endif
4349} 4402}
4350#endif 4403#endif
4351 4404
@@ -4354,6 +4407,7 @@ static void tagcache_thread(void)
4354{ 4407{
4355 struct queue_event ev; 4408 struct queue_event ev;
4356 bool check_done = false; 4409 bool check_done = false;
4410 char path[MAX_PATH];
4357 4411
4358 /* If the previous cache build/update was interrupted, commit 4412 /* If the previous cache build/update was interrupted, commit
4359 * the changes first in foreground. */ 4413 * the changes first in foreground. */
@@ -4370,7 +4424,8 @@ static void tagcache_thread(void)
4370 check_done = tagcache_dumpload(); 4424 check_done = tagcache_dumpload();
4371 } 4425 }
4372 4426
4373 remove(TAGCACHE_STATEFILE); 4427 remove(get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
4428 buf, sizeof(buf)));
4374# endif 4429# endif
4375 4430
4376 /* Allocate space for the tagcache if found on disk. */ 4431 /* Allocate space for the tagcache if found on disk. */
@@ -4403,7 +4458,8 @@ static void tagcache_thread(void)
4403 4458
4404 case Q_REBUILD: 4459 case Q_REBUILD:
4405 remove_files(); 4460 remove_files();
4406 remove(TAGCACHE_FILE_TEMP); 4461 remove(get_user_file_path(TAGCACHE_FILE_TEMP,
4462 IS_FILE|NEED_WRITE, path, sizeof(path)));
4407 tagcache_build("/"); 4463 tagcache_build("/");
4408 break; 4464 break;
4409 4465