END_TEST START_TEST(test_check_browsedir_alloc_error) { char *path; size_t bdlen; struct sbuf *mb; const char *browsedir; char *last_bd_match=NULL; fail_unless((mb=sbuf_alloc(PROTO_1))!=NULL); browsedir=""; bdlen=0; fail_unless((path=strdup_w("aaa", __func__))!=NULL); iobuf_from_str(&mb->path, CMD_FILE, path); alloc_errors=1; fail_unless(check_browsedir( browsedir, mb, bdlen, &last_bd_match) ==-1); sbuf_free(&mb); free_w(&last_bd_match); alloc_check(); }
END_TEST static void run_check_browsedir(const char *browsedir, struct sbuf *mb, enum cmd cmd, const char *path, char **last_bd_match, const char *expected_last_bd_match, int expected_ret, int expected_isdir) { char *mbpath; size_t bdlen=0; if(browsedir) bdlen=strlen(browsedir); fail_unless((mbpath=strdup_w(path, __func__))!=NULL); iobuf_from_str(&mb->path, cmd, mbpath); fail_unless(check_browsedir( browsedir, mb, bdlen, last_bd_match) ==expected_ret); if(expected_last_bd_match) { fail_unless(*last_bd_match!=NULL); ck_assert_str_eq(expected_last_bd_match, *last_bd_match); } else fail_unless(*last_bd_match==NULL); fail_unless(expected_isdir==S_ISDIR(mb->statp.st_mode)); sbuf_free_content(mb); }
static int do_browse_manifest(struct asfd *srfd, struct manio *manio, struct sbuf *sb, const char *browse) { int ret=-1; int ars=0; //char ls[1024]=""; //struct cntr cntr; size_t blen=0; char *last_bd_match=NULL; if(browse) blen=strlen(browse); while(1) { int r; sbuf_free_content(sb); if((ars=manio_read(manio, sb))) { if(ars<0) goto end; // ars==1 means it ended ok. break; } if(manio->protocol==PROTO_2 && sb->endfile.buf) continue; if(sb->path.cmd!=CMD_DIRECTORY && sb->path.cmd!=CMD_FILE && sb->path.cmd!=CMD_ENC_FILE && sb->path.cmd!=CMD_EFS_FILE && sb->path.cmd!=CMD_SPECIAL && !cmd_is_link(sb->path.cmd)) continue; if((r=check_browsedir(browse, sb, blen, &last_bd_match))<0) goto end; if(!r) continue; if(json_from_statp(sb->path.buf, &sb->statp)) goto end; } ret=0; end: free_w(&last_bd_match); return ret; }
static int list_manifest(struct asfd *asfd, const char *fullpath, regex_t *regex, const char *browsedir, struct conf *conf) { int ars=0; int ret=0; struct sbuf *sb=NULL; struct manio *manio=NULL; char *manifest_dir=NULL; char *last_bd_match=NULL; size_t bdlen=0; if(!(manifest_dir=prepend_s(fullpath, conf->protocol==PROTO_BURP1?"manifest.gz":"manifest")) || !(manio=manio_alloc()) || manio_init_read(manio, manifest_dir) || !(sb=sbuf_alloc(conf))) { log_and_send_oom(asfd, __func__); goto error; } manio_set_protocol(manio, conf->protocol); if(browsedir) bdlen=strlen(browsedir); while(1) { int show=0; if((ars=manio_sbuf_fill(manio, asfd, sb, NULL, NULL, conf))<0) goto error; else if(ars>0) goto end; // Finished OK. if(write_status(STATUS_LISTING, sb->path.buf, conf)) goto error; if(browsedir) { int r; if((r=check_browsedir(browsedir, &sb->path.buf, bdlen, &last_bd_match))<0) goto error; if(!r) continue; show++; } else { if(check_regex(regex, sb->path.buf)) show++; } if(show) { if(write_wrapper(asfd, &sb->attr) || write_wrapper(asfd, &sb->path)) goto error; if(sbuf_is_link(sb) && write_wrapper(asfd, &sb->link)) goto error; } sbuf_free_content(sb); } error: ret=-1; end: sbuf_free(&sb); free_w(&manifest_dir); manio_free(&manio); free_w(&last_bd_match); return ret; }
static int list_manifest(const char *fullpath, regex_t *regex, const char *browsedir, const char *client, struct cntr *p1cntr, struct cntr *cntr) { int ars=0; int ret=0; int quit=0; gzFile zp=NULL; struct sbuf mb; char *manifest=NULL; size_t bdlen=0; init_sbuf(&mb); if(!(manifest=prepend_s(fullpath, "manifest.gz", strlen("manifest.gz")))) { log_and_send_oom(__FUNCTION__); return -1; } if(!(zp=gzopen_file(manifest, "rb"))) { log_and_send("could not open manifest"); free(manifest); return -1; } free(manifest); if(browsedir) bdlen=strlen(browsedir); while(!quit) { int show=0; //logp("list manifest loop\n"); // Need to parse while sending, to take note of the regex. free_sbuf(&mb); if((ars=sbuf_fill(NULL, zp, &mb, cntr))) { if(ars<0) ret=-1; // ars==1 means it ended ok. break; } if(mb.cmd!=CMD_DIRECTORY && mb.cmd!=CMD_FILE && mb.cmd!=CMD_ENC_FILE && mb.cmd!=CMD_EFS_FILE && mb.cmd!=CMD_SPECIAL && !cmd_is_link(mb.cmd)) continue; //if(mb.path[mb.plen]=='\n') mb.path[mb.plen]='\0'; write_status(client, STATUS_LISTING, mb.path, p1cntr, cntr); if(browsedir) { int r; if((r=check_browsedir(browsedir, &(mb.path), bdlen))<0) { quit++; ret=-1; } if(!r) continue; show++; } else { if(check_regex(regex, mb.path)) show++; } if(show) { if(async_write(CMD_STAT, mb.statbuf, mb.slen) || async_write(mb.cmd, mb.path, mb.plen)) { quit++; ret=-1; } else if(sbuf_is_link(&mb) && async_write(mb.cmd, mb.linkto, mb.llen)) { quit++; ret=-1; } } } gzclose_fp(&zp); free_sbuf(&mb); return ret; }
static int list_manifest(const char *fullpath) { int ret=0; struct sbuf *sb=NULL; struct manio *manio=NULL; char *manifest_dir=NULL; char *last_bd_match=NULL; size_t bdlen=0; if(!(manifest_dir=prepend_s(fullpath, protocol==PROTO_1?"manifest.gz":"manifest")) || !(manio=manio_open(manifest_dir, "rb", protocol)) || !(sb=sbuf_alloc(protocol))) { log_and_send_oom(asfd); goto error; } if(browsedir) bdlen=strlen(browsedir); while(1) { sbuf_free_content(sb); switch(manio_read(manio, sb)) { case 0: break; case 1: if(browsedir && *browsedir && !last_bd_match) asfd_write_wrapper_str(asfd, CMD_ERROR, "directory not found"); goto end; // Finished OK. default: goto error; } if(protocol==PROTO_2 && sb->endfile.buf) continue; if(sbuf_is_metadata(sb)) continue; if(write_status(CNTR_STATUS_LISTING, sb->path.buf, cntr)) goto error; if(browsedir) { int r; if((r=check_browsedir(browsedir, sb, bdlen, &last_bd_match))<0) goto error; if(!r) continue; } if(regex && !regex_check(regex, sb->path.buf)) continue; if(asfd_write_wrapper(asfd, &sb->attr) || asfd_write_wrapper(asfd, &sb->path)) goto error; if(sbuf_is_link(sb) && asfd_write_wrapper(asfd, &sb->link)) goto error; } error: ret=-1; end: sbuf_free(&sb); free_w(&manifest_dir); manio_close(&manio); free_w(&last_bd_match); return ret; }