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_BUF
35 #include "dird/dird.h"
59 static int my_index_cmp(void *item1, void *item2)
61 MY_INDEX *elt1, *elt2;
62 elt1 = (MY_INDEX *) item1;
63 elt2 = (MY_INDEX *) item2;
64 int r = strcmp(elt1->fname, elt2->fname);
68 r = strcmp(elt1->path, elt2->path);
72 static int my_cmp(void *item1, void *item2)
77 return strcmp(elt1->buf, elt2->buf);
80 rblist *load_rb(const char *file)
86 lst = New(rblist(res, &res->link));
88 fp = fopen(file, "r");
92 while (fgets(buffer, sizeof(buffer), fp)) {
94 int len = strlen(buffer);
96 buffer[len-1]=0; /* zap \n */
98 MY *buf = (MY *)malloc(sizeof(MY));
99 memset(buf, 0, sizeof(MY));
100 buf->buf = bstrdup(buffer);
101 res = (MY *)lst->insert(buf, my_cmp);
113 /* buffer used for 4k io operations */
122 memset(_buf, 0, sizeof(_buf));
124 void *get_elt(int nb);
127 void update_elt(int nb);
139 void AccurateBuffer::init()
141 _fp = fopen("testfile", "w+");
149 void AccurateBuffer::destroy()
157 void *AccurateBuffer::get_elt(int nb)
159 int page=nb*sizeof(AccurateElt)/sizeof(_buf);
165 if (page != _nb) { /* not the same page */
166 if (_dirty) { /* have to sync on disk */
167 // printf("put dirty page on disk %i\n", _nb);
168 if (fseek(_fp, _nb*sizeof(_buf), SEEK_SET) == -1) {
172 if (fwrite(_buf, sizeof(_buf), 1, _fp) != 1) {
173 perror("writing...");
178 if (page <= _max_nb) { /* we read it only if the page exists */
179 // printf("read page from disk %i <= %i\n", page, _max_nb);
180 fseek(_fp, page*sizeof(_buf), SEEK_SET);
181 if (fread(_buf, sizeof(_buf), 1, _fp) != 1) {
182 // printf("memset to zero\n");
183 memset(_buf, 0, sizeof(_buf));
186 memset(_buf, 0, sizeof(_buf));
189 _max_nb = MAX(_max_nb, page);
192 /* compute addr of the element in _buf */
193 int addr=(nb%(sizeof(_buf)/sizeof(AccurateElt)))*sizeof(AccurateElt);
194 // printf("addr=%i\n", addr);
195 return (void *) (_buf + addr);
198 void AccurateBuffer::update_elt(int nb)
212 * Given a full filename, split it into its path
213 * and filename parts. They are returned in pool memory
214 * in the mdb structure.
216 void split_path_and_file(B_DBx *mdb, const char *fname)
220 /* Find path without the filename.
221 * I.e. everything after the last / is a "filename".
222 * OK, maybe it is a directory name, but we treat it like
223 * a filename. If we don't find a / then the whole name
224 * must be a path name (e.g. c:).
226 for (p=f=fname; *p; p++) {
227 if (IsPathSeparator(*p)) {
228 f = p; /* set pos of last slash */
231 if (IsPathSeparator(*f)) { /* did we find a slash? */
232 f++; /* yes, point to filename */
233 } else { /* no, whole thing must be path name */
237 /* If filename doesn't exist (i.e. root directory), we
238 * simply create a blank name consisting of a single
239 * space. This makes handling zero length filenames
244 mdb->fname = check_pool_memory_size(mdb->fname, mdb->fnl+1);
245 memcpy(mdb->fname, f, mdb->fnl); /* copy filename */
246 mdb->fname[mdb->fnl] = 0;
252 mdb->pnl = f - fname;
254 mdb->path = check_pool_memory_size(mdb->path, mdb->pnl+1);
255 memcpy(mdb->path, fname, mdb->pnl);
256 mdb->path[mdb->pnl] = 0;
262 Dmsg2(500, "split path=%s file=%s\n", mdb->path, mdb->fname);
269 char *start_heap = (char *)sbrk(0);
274 fp = fopen("lst", "r");
278 rblist *index, *rb_file, *rb_path;
279 index = New(rblist(idx, &idx->link)); /* (pathid, filenameid) => index */
280 rb_path = New(rblist(myp, &myp->link)); /* pathid => path */
281 rb_file = New(rblist(myp, &myp->link)); /* filenameid => filename */
284 mdb.fname = get_pool_memory(PM_FNAME);
285 mdb.path = get_pool_memory(PM_FNAME);
287 AccurateBuffer *accbuf = new AccurateBuffer;
290 printf("loading...\n");
291 while (fgets(buf, sizeof(buf), fp)) {
292 int len = strlen(buf);
294 buf[len-1]=0; /* zap \n */
296 split_path_and_file(&mdb, buf);
298 MY_INDEX *idx = (MY_INDEX *)malloc(sizeof(MY_INDEX));
299 memset(idx, 0, sizeof(MY_INDEX));
301 myp = (MY *)malloc(sizeof(MY));
302 memset(myp, 0, sizeof(MY));
303 myp->buf = bstrdup(mdb.path);
304 res = (MY *)rb_path->insert(myp, my_cmp);
310 idx->path = res->buf;
312 myp = (MY *)malloc(sizeof(MY));
313 memset(myp, 0, sizeof(MY));
314 myp->buf = bstrdup(mdb.fname);
315 res = (MY *)rb_file->insert(myp, my_cmp);
321 idx->fname = res->buf;
324 index->insert(idx, my_index_cmp);
326 elt = (AccurateElt *)accbuf->get_elt(count);
328 elt->path = idx->path;
329 elt->fname = idx->fname;
330 accbuf->update_elt(count);
335 printf("fetch and update...\n");
336 fprintf(stderr, "count;%lli\n", count);
337 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
339 fseek(fp, 0, SEEK_SET);
341 while (fgets(buf, sizeof(buf), fp)) {
342 int len = strlen(buf);
344 buf[len-1]=0; /* zap \n */
346 split_path_and_file(&mdb, buf);
349 search.buf = mdb.fname;
350 MY *f = (MY *)rb_file->search(&search, my_cmp);
353 continue; /* not found, skip */
356 search.buf = mdb.path;
357 MY *p = (MY *)rb_path->search(&search, my_cmp);
359 continue; /* not found, skip */
365 MY_INDEX *res = (MY_INDEX *)index->search(&idx, my_index_cmp);
368 continue; /* not found skip */
371 AccurateElt *elt = (AccurateElt *)accbuf->get_elt(res->index);
373 accbuf->update_elt(res->index);
374 toggle = (toggle+1)%100000;
377 for(int j=0;j<=count; j++) {
378 AccurateElt *elt = (AccurateElt *)accbuf->get_elt(j);
379 if (elt->path && elt->fname) {
381 printf("%s%s\n", elt->path, elt->fname);
393 char *start_heap = (char *)sbrk(0);
395 fd = open("testfile", O_CREAT | O_RDWR, 0600);
397 perror("E: Can't open testfile ");
401 memset(&elt, 0, sizeof(elt));
404 for (i=0; i<NB_ELT; i++) {
405 write(fd, &elt, sizeof(elt));
408 lseek(fd, 0, SEEK_SET); /* rewind */
410 /* 2) load and update */
411 for (i=0; i<NB_ELT; i++) {
412 lseek(fd, i*sizeof(AccurateElt), SEEK_SET);
413 read(fd, &elt, sizeof(elt));
414 lseek(fd, i*sizeof(AccurateElt), SEEK_SET);
415 write(fd, &elt, sizeof(elt));
418 lseek(fd, 0, SEEK_SET); /* rewind */
420 /* 3) Fetch all of them */
421 for (i=0; i<NB_ELT; i++) {
422 read(fd, &elt, sizeof(elt));
427 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
431 #else /* _TEST_OPEN */
435 char *start_heap = (char *)sbrk(0);
439 AccurateBuffer *buf = new AccurateBuffer;
442 printf("Loading...\n");
443 for (i=0; i<NB_ELT; i++) {
444 elt = (AccurateElt *) buf->get_elt(i);
449 /* 2) load and update */
450 printf("Load and update...\n");
451 for (i=0; i<NB_ELT; i++) {
452 elt = (AccurateElt *) buf->get_elt(i);
453 if (elt->mtime != i) {
454 printf("Something is wrong with elt %i mtime=%lli\n", i, elt->mtime);
461 /* 3) Fetch all of them */
462 printf("Fetch them...\n");
463 for (i=0; i<NB_ELT; i++) {
464 elt = (AccurateElt *) buf->get_elt(i);
465 if (elt->seen != i || elt->mtime != i) {
466 printf("Something is wrong with elt %i mtime=%lli seen=%i\n", i, elt->mtime, elt->seen);
470 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
482 fd = fopen("testfile", "w+");
484 perror("E: Can't open testfile ");
488 memset(&elt, 0, sizeof(elt));
491 printf("Loading...\n");
492 for (i=0; i<NB_ELT; i++) {
493 fwrite(&elt, sizeof(elt), 1, fd);
496 fseek(fd, 0, SEEK_SET); /* rewind */
498 /* 2) load and update */
499 printf("Load and update...\n");
500 for (i=0; i<NB_ELT; i++) {
501 fseek(fd, i*sizeof(AccurateElt), SEEK_SET);
502 fread(&elt, sizeof(elt), 1, fd);
503 fseek(fd, i*sizeof(AccurateElt), SEEK_SET);
504 fwrite(&elt, sizeof(elt), 1, fd);
507 fseek(fd, 0, SEEK_SET); /* rewind */
509 /* 3) Fetch all of them */
510 printf("Fetch them...\n");
511 for (i=0; i<NB_ELT; i++) {
512 fread(&elt, sizeof(elt), 1, fd);
518 #endif /* _TEST_BUF */
519 #endif /* _TEST_OPEN */
520 #endif /* _TEST_TREE */