2 * This file is used to test simple I/O operation for accurate mode
25 g++ -g -Wall -I../../src -I../../src/lib -L../../src/lib justdisk.c -lbac -lpthread -lssl -D_TEST_TREE
36 #include "dird/dird.h"
60 static int my_index_cmp(void *item1, void *item2)
62 MY_INDEX *elt1, *elt2;
63 elt1 = (MY_INDEX *) item1;
64 elt2 = (MY_INDEX *) item2;
65 int r = strcmp(elt1->fname, elt2->fname);
69 r = strcmp(elt1->path, elt2->path);
73 static int my_cmp(void *item1, void *item2)
78 return strcmp(elt1->buf, elt2->buf);
81 rblist *load_rb(const char *file)
87 lst = New(rblist(res, &res->link));
89 fp = fopen(file, "r");
93 while (fgets(buffer, sizeof(buffer), fp)) {
95 int len = strlen(buffer);
97 buffer[len-1]=0; /* zap \n */
99 MY *buf = (MY *)malloc(sizeof(MY));
100 memset(buf, 0, sizeof(MY));
101 buf->buf = bstrdup(buffer);
102 res = (MY *)lst->insert(buf, my_cmp);
114 /* buffer used for 4k io operations */
123 memset(_buf, 0, sizeof(_buf));
125 void *get_elt(int nb);
128 void update_elt(int nb);
140 void AccurateBuffer::init()
142 _fp = fopen("testfile", "w+");
150 void AccurateBuffer::destroy()
158 void *AccurateBuffer::get_elt(int nb)
160 int page=nb*sizeof(AccurateElt)/sizeof(_buf);
166 if (page != _nb) { /* not the same page */
167 if (_dirty) { /* have to sync on disk */
168 // printf("put dirty page on disk %i\n", _nb);
169 if (fseek(_fp, _nb*sizeof(_buf), SEEK_SET) == -1) {
173 if (fwrite(_buf, sizeof(_buf), 1, _fp) != 1) {
174 perror("writing...");
179 if (page <= _max_nb) { /* we read it only if the page exists */
180 // printf("read page from disk %i <= %i\n", page, _max_nb);
181 fseek(_fp, page*sizeof(_buf), SEEK_SET);
182 if (fread(_buf, sizeof(_buf), 1, _fp) != 1) {
183 // printf("memset to zero\n");
184 memset(_buf, 0, sizeof(_buf));
187 memset(_buf, 0, sizeof(_buf));
190 _max_nb = MAX(_max_nb, page);
193 /* compute addr of the element in _buf */
194 int addr=(nb%(sizeof(_buf)/sizeof(AccurateElt)))*sizeof(AccurateElt);
195 // printf("addr=%i\n", addr);
196 return (void *) (_buf + addr);
199 void AccurateBuffer::update_elt(int nb)
213 * Given a full filename, split it into its path
214 * and filename parts. They are returned in pool memory
215 * in the mdb structure.
217 void split_path_and_file(B_DBx *mdb, const char *fname)
221 /* Find path without the filename.
222 * I.e. everything after the last / is a "filename".
223 * OK, maybe it is a directory name, but we treat it like
224 * a filename. If we don't find a / then the whole name
225 * must be a path name (e.g. c:).
227 for (p=f=fname; *p; p++) {
228 if (IsPathSeparator(*p)) {
229 f = p; /* set pos of last slash */
232 if (IsPathSeparator(*f)) { /* did we find a slash? */
233 f++; /* yes, point to filename */
234 } else { /* no, whole thing must be path name */
238 /* If filename doesn't exist (i.e. root directory), we
239 * simply create a blank name consisting of a single
240 * space. This makes handling zero length filenames
245 mdb->fname = check_pool_memory_size(mdb->fname, mdb->fnl+1);
246 memcpy(mdb->fname, f, mdb->fnl); /* copy filename */
247 mdb->fname[mdb->fnl] = 0;
253 mdb->pnl = f - fname;
255 mdb->path = check_pool_memory_size(mdb->path, mdb->pnl+1);
256 memcpy(mdb->path, fname, mdb->pnl);
257 mdb->path[mdb->pnl] = 0;
263 Dmsg2(500, "split path=%s file=%s\n", mdb->path, mdb->fname);
270 char *start_heap = (char *)sbrk(0);
275 fp = fopen("lst", "r");
279 rblist *index, *rb_file, *rb_path;
280 index = New(rblist(idx, &idx->link)); /* (pathid, filenameid) => index */
281 rb_path = New(rblist(myp, &myp->link)); /* pathid => path */
282 rb_file = New(rblist(myp, &myp->link)); /* filenameid => filename */
285 mdb.fname = get_pool_memory(PM_FNAME);
286 mdb.path = get_pool_memory(PM_FNAME);
288 AccurateBuffer *accbuf = new AccurateBuffer;
291 printf("loading...\n");
292 while (fgets(buf, sizeof(buf), fp)) {
293 int len = strlen(buf);
295 buf[len-1]=0; /* zap \n */
297 split_path_and_file(&mdb, buf);
299 MY_INDEX *idx = (MY_INDEX *)malloc(sizeof(MY_INDEX));
300 memset(idx, 0, sizeof(MY_INDEX));
302 myp = (MY *)malloc(sizeof(MY));
303 memset(myp, 0, sizeof(MY));
304 myp->buf = bstrdup(mdb.path);
305 res = (MY *)rb_path->insert(myp, my_cmp);
311 idx->path = res->buf;
313 myp = (MY *)malloc(sizeof(MY));
314 memset(myp, 0, sizeof(MY));
315 myp->buf = bstrdup(mdb.fname);
316 res = (MY *)rb_file->insert(myp, my_cmp);
322 idx->fname = res->buf;
325 index->insert(idx, my_index_cmp);
327 elt = (AccurateElt *)accbuf->get_elt(count);
329 elt->path = idx->path;
330 elt->fname = idx->fname;
331 accbuf->update_elt(count);
336 printf("fetch and update...\n");
337 fprintf(stderr, "count;%lli\n", count);
338 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
340 fseek(fp, 0, SEEK_SET);
342 while (fgets(buf, sizeof(buf), fp)) {
343 int len = strlen(buf);
345 buf[len-1]=0; /* zap \n */
347 split_path_and_file(&mdb, buf);
350 search.buf = mdb.fname;
351 MY *f = (MY *)rb_file->search(&search, my_cmp);
354 continue; /* not found, skip */
357 search.buf = mdb.path;
358 MY *p = (MY *)rb_path->search(&search, my_cmp);
360 continue; /* not found, skip */
366 MY_INDEX *res = (MY_INDEX *)index->search(&idx, my_index_cmp);
369 continue; /* not found skip */
372 AccurateElt *elt = (AccurateElt *)accbuf->get_elt(res->index);
374 accbuf->update_elt(res->index);
375 toggle = (toggle+1)%100000;
378 for(int j=0;j<=count; j++) {
379 AccurateElt *elt = (AccurateElt *)accbuf->get_elt(j);
380 if (elt->path && elt->fname) {
382 printf("%s%s\n", elt->path, elt->fname);
394 char *start_heap = (char *)sbrk(0);
396 fd = open("testfile", O_CREAT | O_RDWR, 0600);
398 perror("E: Can't open testfile ");
402 memset(&elt, 0, sizeof(elt));
405 for (i=0; i<NB_ELT; i++) {
406 write(fd, &elt, sizeof(elt));
409 lseek(fd, 0, SEEK_SET); /* rewind */
411 /* 2) load and update */
412 for (i=0; i<NB_ELT; i++) {
413 lseek(fd, i*sizeof(AccurateElt), SEEK_SET);
414 read(fd, &elt, sizeof(elt));
415 lseek(fd, i*sizeof(AccurateElt), SEEK_SET);
416 write(fd, &elt, sizeof(elt));
419 lseek(fd, 0, SEEK_SET); /* rewind */
421 /* 3) Fetch all of them */
422 for (i=0; i<NB_ELT; i++) {
423 read(fd, &elt, sizeof(elt));
428 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
432 #else /* _TEST_OPEN */
436 char *start_heap = (char *)sbrk(0);
440 AccurateBuffer *buf = new AccurateBuffer;
443 printf("Loading...\n");
444 for (i=0; i<NB_ELT; i++) {
445 elt = (AccurateElt *) buf->get_elt(i);
450 /* 2) load and update */
451 printf("Load and update...\n");
452 for (i=0; i<NB_ELT; i++) {
453 elt = (AccurateElt *) buf->get_elt(i);
454 if (elt->mtime != i) {
455 printf("Something is wrong with elt %i mtime=%lli\n", i, elt->mtime);
462 /* 3) Fetch all of them */
463 printf("Fetch them...\n");
464 for (i=0; i<NB_ELT; i++) {
465 elt = (AccurateElt *) buf->get_elt(i);
466 if (elt->seen != i || elt->mtime != i) {
467 printf("Something is wrong with elt %i mtime=%lli seen=%i\n", i, elt->mtime, elt->seen);
471 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
483 fd = fopen("testfile", "w+");
485 perror("E: Can't open testfile ");
489 memset(&elt, 0, sizeof(elt));
492 printf("Loading...\n");
493 for (i=0; i<NB_ELT; i++) {
494 fwrite(&elt, sizeof(elt), 1, fd);
497 fseek(fd, 0, SEEK_SET); /* rewind */
499 /* 2) load and update */
500 printf("Load and update...\n");
501 for (i=0; i<NB_ELT; i++) {
502 fseek(fd, i*sizeof(AccurateElt), SEEK_SET);
503 fread(&elt, sizeof(elt), 1, fd);
504 fseek(fd, i*sizeof(AccurateElt), SEEK_SET);
505 fwrite(&elt, sizeof(elt), 1, fd);
508 fseek(fd, 0, SEEK_SET); /* rewind */
510 /* 3) Fetch all of them */
511 printf("Fetch them...\n");
512 for (i=0; i<NB_ELT; i++) {
513 fread(&elt, sizeof(elt), 1, fd);
519 #endif /* _TEST_BUF */
520 #endif /* _TEST_OPEN */
521 #endif /* _TEST_TREE */