int main(int argc, char **argv) { char *x, *y; sbuf_t *inpa, *inpb; int inp_counter=0; char ch; int i, j, k, n, m; int score; inpa = sbuf_alloc(16); inpb = sbuf_alloc(16); while ((ch=fgetc(stdin))!=EOF) { if (ch=='\n') { inp_counter++; if (inp_counter==2) { break; }; continue; } if (inp_counter==0) { sbuf_add(inpa, ch); } else { sbuf_add(inpb, ch); } } x = inpa->s; y = inpb->s; if (strlen(x)!=strlen(y)) { fprintf(stderr, "length mismatch\n"); exit(-1); } n = strlen(x); score = 0; for (i=0; i<n; i++) { if (x[i]=='-') { if (y[i]=='-') { fprintf(stderr, "invalid alignment string ('-' aligned with '-' at %d)\n", i); exit(-1); } score += GAP; } else if (y[i]=='-') { if (x[i]=='-') { fprintf(stderr, "invalid alignment string ('-' aligned with '-' at %d)\n", i); exit(-1); } score += GAP; } else if (x[i]!=y[i]) { score+=MISMATCH; } } printf("%d\n", score); }
END_TEST static void do_test_check_browsedir_windows_blank(enum protocol protocol) { struct sbuf *mb; const char *browsedir; char *last_bd_match=NULL; fail_unless((mb=sbuf_alloc(protocol))!=NULL); browsedir=""; run_check_browsedir(browsedir, mb, CMD_FILE, "A:/aaa", &last_bd_match, "A:", 1, 1); run_check_browsedir(browsedir, mb, CMD_DIRECTORY, "C:/aaa", &last_bd_match, "C:", 1, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "C:/aaa/file", &last_bd_match, "C:", 0, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "C:/aaa/filx", &last_bd_match, "C:", 0, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "D:/adf", &last_bd_match, "D:", 1, 1); sbuf_free(&mb); free_w(&last_bd_match); alloc_check(); }
static struct sbuf *setup_sbuf(const char *path, const char *datapth, const char *endfile, int compression) { struct sbuf *sb; fail_unless((sb=sbuf_alloc(PROTO_1))!=NULL); if(path) { fail_unless((path=strdup_w(path, __func__))!=NULL); iobuf_from_str(&sb->path, CMD_FILE, (char *)path); } if(datapth) { fail_unless((datapth=strdup_w("/datapth", __func__))!=NULL); iobuf_from_str(&sb->protocol1->datapth, CMD_FILE, (char *)datapth); } if(endfile) { fail_unless((endfile=strdup_w(endfile, __func__))!=NULL); iobuf_from_str(&sb->endfile, CMD_END_FILE, (char *)endfile); } sb->compression=compression; return sb; }
// Used on resume, this just reads the phase1 file and sets up cntr. static int read_phase1(struct manio *p1manio, struct conf **cconfs) { int ret=-1; struct sbuf *p1b; enum protocol protocol=get_protocol(cconfs); struct cntr *cntr=get_cntr(cconfs); if(!(p1b=sbuf_alloc(protocol))) return -1; while(1) { sbuf_free_content(p1b); switch(manio_read(p1manio, p1b)) { case 0: break; case 1: ret=0; default: goto end; } cntr_add_phase1(cntr, p1b->path.cmd, 0); if(sbuf_is_estimatable(p1b)) cntr_add_val(cntr, CMD_BYTES_ESTIMATED, (uint64_t)p1b->statp.st_size); } end: sbuf_free(&p1b); return ret; }
static int do_backup_phase2_client(struct asfd *asfd, struct conf *conf, int resume) { int ret=-1; // For efficiency, open Windows files for the VSS data, and do not // close them until another time around the loop, when the actual // data is read. BFILE bfd; // Windows VSS headers tell us how much file // data to expect. size_t datalen=0; #ifdef HAVE_WIN32 binit(&bfd, 0, conf); #endif struct sbuf *sb=NULL; struct iobuf *rbuf=asfd->rbuf; if(!(sb=sbuf_alloc(conf))) goto end; if(!resume) { // Only do this bit if the server did not tell us to resume. if(asfd->write_str(asfd, CMD_GEN, "backupphase2") || asfd->read_expect(asfd, CMD_GEN, "ok")) goto end; } else if(conf->send_client_cntr) { // On resume, the server might update the client with cntr. if(cntr_recv(asfd, conf)) goto end; } while(1) { iobuf_free_content(rbuf); if(asfd->read(asfd)) goto end; else if(!rbuf->buf) continue; if(rbuf->cmd==CMD_GEN && !strcmp(rbuf->buf, "backupphase2end")) { if(asfd->write_str(asfd, CMD_GEN, "okbackupphase2end")) goto end; ret=0; break; } if(parse_rbuf(asfd, sb, &bfd, &datalen, conf)) goto end; } end: #ifdef HAVE_WIN32 // It is possible for a bfd to still be open. close_file_for_sendl(&bfd, NULL, asfd); #endif iobuf_free_content(rbuf); sbuf_free(sb); return ret; }
void wav(const char* filename) { int fd; rt_size_t block_size; block_size = sbuf_get_size(); block_size = (block_size / 512) * 512; fd = open(filename, O_RDONLY, 0); if (fd >= 0) { rt_uint8_t* buf; rt_size_t len; rt_device_t device; /* open audio device and set tx done call back */ device = rt_device_find("snd"); rt_device_set_tx_complete(device, wav_tx_done); rt_device_open(device, RT_DEVICE_OFLAG_WRONLY); do { buf = sbuf_alloc(); len = read(fd, (char*)buf, block_size); if (len > 0) rt_device_write(device, 0, buf, len); else sbuf_release(buf); } while (len != 0); /* close device and file */ rt_device_close(device); close(fd); } }
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 do_test_check_browsedir_null_or_blank(enum protocol protocol, const char *browsedir) { struct sbuf *mb; char *last_bd_match=NULL; fail_unless((mb=sbuf_alloc(protocol))!=NULL); run_check_browsedir(browsedir, mb, CMD_FILE, "aaa", &last_bd_match, "aaa", 1, 0); run_check_browsedir(browsedir, mb, CMD_DIRECTORY, "/", &last_bd_match, "/", 1, 1); run_check_browsedir(browsedir, mb, CMD_DIRECTORY, "/asdf", &last_bd_match, "/", 0, 1); run_check_browsedir(browsedir, mb, CMD_DIRECTORY, "/asdf/blah", &last_bd_match, "/", 0, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "zzz", &last_bd_match, "zzz", 1, 0); run_check_browsedir(browsedir, mb, CMD_FILE, "zzzz//", &last_bd_match, "zzzz", 1, 1); sbuf_free(&mb); free_w(&last_bd_match); alloc_check(); }
static int restore_remaining_dirs(struct asfd *asfd, struct bu *bu, struct slist *slist, enum action act, struct sdirs *sdirs, enum cntr_status cntr_status, struct conf **cconfs) { int ret=-1; struct sbuf *sb; struct sbuf *need_data=NULL; if(!(need_data=sbuf_alloc(get_protocol(cconfs)))) goto end; // Restore any directories that are left in the list. for(sb=slist->head; sb; sb=sb->next) { if(get_protocol(cconfs)==PROTO_1) { if(restore_sbuf_protocol1(asfd, sb, bu, act, sdirs, cntr_status, cconfs)) goto end; } else { if(restore_sbuf_protocol2(asfd, sb, act, cntr_status, get_cntr(cconfs), need_data)) goto end; } } ret=0; end: sbuf_free(&need_data); return ret; }
void _trace_list(list_s *l) { sbuf_s *s = sbuf_alloc(); char *v; list_foreach(l, first, next, cur) { v = (char *)cur->value; sbuf_appendf(s, "'%s', ", v); }
static #endif int forward_past_entry(struct manio *manio, struct iobuf *target, enum protocol protocol, man_off_t **pos) { struct sbuf *sb=NULL; if(!(sb=sbuf_alloc(protocol))) goto error; man_off_t_free(pos); if(!(*pos=manio_tell(manio))) { logp("Could not manio_tell first pos in %s(): %s\n", __func__, strerror(errno)); goto error; } while(1) { sbuf_free_content(sb); switch(manio_read(manio, sb)) { case 0: break; case 1: logp("End of file in %s()\n", __func__); goto error; default: logp("Error in %s()\n", __func__); // Treat error in unchanged manio as not OK. goto error; } if(target->cmd==sb->path.cmd && !pathcmp(target->buf, sb->path.buf)) { man_off_t_free(pos); if(!(*pos=manio_tell(manio))) { logp("Could not get pos in %s(): %s\n", __func__, strerror(errno)); goto error; } sbuf_free(&sb); return 0; } } error: sbuf_free(&sb); man_off_t_free(pos); return -1; }
// When a backup is ongoing, use this to add newly complete candidates. int candidate_add_fresh(const char *path, struct conf *conf) { int ars; int ret=-1; gzFile zp=NULL; const char *cp=NULL; struct sbuf *sb=NULL; struct candidate *candidate=NULL; struct blk *blk=NULL; if(!(candidate=candidates_add_new())) goto end; cp=path+strlen(conf->directory); while(cp && *cp=='/') cp++; if(!(candidate->path=strdup(cp))) { log_out_of_memory(__func__); goto end; } if(!(sb=sbuf_alloc(conf)) || !(blk=blk_alloc()) || !(zp=gzopen_file(path, "rb"))) goto end; while(zp) { if((ars=sbuf_fill_from_gzfile(sb, NULL /* struct async */, zp, blk, NULL, conf))<0) goto end; else if(ars>0) { // Reached the end. break; } if(!*(blk->weak)) continue; if(is_hook(blk->weak)) { if(sparse_add_candidate(blk->weak, candidate)) goto end; } *blk->weak='\0'; } if(scores_grow(scores, candidates_len)) goto end; candidates_set_score_pointers(candidates, candidates_len, scores); scores_reset(scores); //printf("HERE: %d candidates\n", (int)candidates_len); ret=0; end: gzclose_fp(&zp); sbuf_free(sb); blk_free(blk); return ret; }
static int my_send_file(struct asfd *asfd, struct FF_PKT *ff, struct conf **confs) { static struct sbuf *sb=NULL; struct cntr *cntr=get_cntr(confs); if(!sb && !(sb=sbuf_alloc(get_protocol(confs)))) return -1; #ifdef HAVE_WIN32 if(ff->winattr & FILE_ATTRIBUTE_ENCRYPTED) { if(ff->type==FT_REG || ff->type==FT_DIR) return to_server(asfd, confs, ff, sb, CMD_EFS_FILE); return logw(asfd, cntr, "EFS type %d not yet supported: %s\n", ff->type, ff->fname); } #endif switch(ff->type) { case FT_REG: case FT_RAW: case FT_FIFO: return do_to_server(asfd, confs, ff, sb, filesymbol, in_exclude_comp(get_strlist(confs[OPT_EXCOM]), ff->fname, get_int(confs[OPT_COMPRESSION]))); case FT_DIR: case FT_REPARSE: case FT_JUNCTION: return to_server(asfd, confs, ff, sb, dirsymbol); case FT_LNK_S: return to_server(asfd, confs, ff, sb, CMD_SOFT_LINK); case FT_LNK_H: return to_server(asfd, confs, ff, sb, CMD_HARD_LINK); case FT_SPEC: return to_server(asfd, confs, ff, sb, CMD_SPECIAL); case FT_NOFSCHG: return ft_err(asfd, confs, ff, "Will not descend: " "file system change not allowed"); case FT_NOFOLLOW: return ft_err(asfd, confs, ff, "Could not follow link"); case FT_NOSTAT: return ft_err(asfd, confs, ff, "Could not stat"); case FT_NOOPEN: return ft_err(asfd, confs, ff, "Could not open directory"); default: return logw(asfd, cntr, "Err: Unknown file type %d: %s\n", ff->type, ff->fname); } }
int send_file(struct asfd *asfd, FF_PKT *ff, bool top_level, struct conf *conf) { static struct sbuf *sb=NULL; if(!sb && !(sb=sbuf_alloc(conf))) return -1; if(!file_is_included(conf, ff->fname, top_level)) return 0; #ifdef HAVE_WIN32 if(ff->winattr & FILE_ATTRIBUTE_ENCRYPTED) { if(ff->type==FT_REG || ff->type==FT_DIR) return to_server(asfd, conf, ff, sb, CMD_EFS_FILE); return logw(asfd, conf, "EFS type %d not yet supported: %s", ff->type, ff->fname); } #endif switch(ff->type) { case FT_REG: case FT_RAW: case FT_FIFO: return do_to_server(asfd, conf, ff, sb, filesymbol, in_exclude_comp(conf->excom, ff->fname, conf->compression)); case FT_DIR: case FT_REPARSE: case FT_JUNCTION: return to_server(asfd, conf, ff, sb, dirsymbol); case FT_LNK_S: return to_server(asfd, conf, ff, sb, CMD_SOFT_LINK); case FT_LNK_H: return to_server(asfd, conf, ff, sb, CMD_HARD_LINK); case FT_SPEC: return to_server(asfd, conf, ff, sb, CMD_SPECIAL); case FT_NOFSCHG: return logw(asfd, conf, "Dir: %s [will not descend: " "file system change not allowed]\n", ff->fname); case FT_NOFOLLOW: return ft_err(asfd, conf, ff, "Could not follow link"); case FT_NOSTAT: return ft_err(asfd, conf, ff, "Could not stat"); case FT_NOOPEN: return ft_err(asfd, conf, ff, "Could not open directory"); default: return logw(asfd, conf, _("Err: Unknown file type %d: %s"), ff->type, ff->fname); } }
static int add_to_file_requests(struct slist *slist, struct iobuf *rbuf) { static uint64_t file_no=1; struct sbuf *sb; if(!(sb=sbuf_alloc(PROTO_2))) return -1; iobuf_move(&sb->path, rbuf); // Give it a number to simplify tracking. sb->protocol2->index=file_no++; slist_add_sbuf(slist, sb); return 0; }
static int add_to_file_requests(struct slist *slist, struct iobuf *rbuf, struct conf *conf) { static uint64_t file_no=1; struct sbuf *sb; if(!(sb=sbuf_alloc(conf))) return -1; iobuf_copy(&sb->path, rbuf); rbuf->buf=NULL; // Give it a number to simplify tracking. sb->burp2->index=file_no++; slist_add_sbuf(slist, sb); return 0; }
static int setup_cntr(struct asfd *asfd, const char *manifest, regex_t *regex, int srestore, enum action act, char status, struct conf **cconfs) { int ars=0; int ret=-1; struct fzp *fzp=NULL; struct sbuf *sb=NULL; struct cntr *cntr=get_cntr(cconfs); // FIX THIS: this is only trying to work for protocol1. if(get_protocol(cconfs)!=PROTO_1) return 0; if(!(sb=sbuf_alloc(PROTO_1))) goto end; if(!(fzp=fzp_gzopen(manifest, "rb"))) { log_and_send(asfd, "could not open manifest"); goto end; } while(1) { if((ars=sbuf_fill_from_file(sb, fzp, NULL, NULL))) { if(ars<0) goto end; // ars==1 means end ok break; } else { if(want_to_restore(srestore, sb, regex, cconfs)) { cntr_add_phase1(cntr, sb->path.cmd, 0); if(sb->endfile.buf) cntr_add_val(cntr, CMD_BYTES_ESTIMATED, strtoull(sb->endfile.buf, NULL, 10), 0); } } sbuf_free_content(sb); } ret=0; end: sbuf_free(&sb); fzp_close(&fzp); return ret; }
static int maybe_add_from_scan(struct manios *manios, struct slist *slist, struct asfd *chfd, struct sbuf **csb) { int ret=-1; struct sbuf *snew=NULL; while(1) { sbuf_free(&snew); if(!manios->phase1) return 0; // Limit the amount loaded into memory at any one time. if(slist && slist->head) { if(slist->head->protocol2->index - slist->tail->protocol2->index>4096) return 0; } if(!(snew=sbuf_alloc(PROTO_2))) goto end; switch(manio_read(manios->phase1, snew)) { case 0: break; case 1: manio_close(&manios->phase1); ret=0; // Finished. default: goto end; } switch(entry_changed(snew, manios, chfd, csb)) { case 0: continue; // No change. case 1: break; default: goto end; // Error. } if(data_needed(snew)) snew->flags|=SBUF_NEED_DATA; slist_add_sbuf(slist, snew); snew=NULL; } return 0; end: sbuf_free(&snew); return ret; }
static int maybe_add_from_scan(struct asfd *asfd, struct manio *p1manio, struct manio *cmanio, struct manio *unmanio, struct slist *slist, struct conf *conf) { int ret=-1; static int ars; static int ec=0; struct sbuf *snew=NULL; while(1) { if(manio_closed(p1manio)) return 0; // Limit the amount loaded into memory at any one time. if(slist && slist->head) { if(slist->head->burp2->index - slist->tail->burp2->index>4096) return 0; } if(!(snew=sbuf_alloc(conf))) goto end; if((ars=manio_sbuf_fill(p1manio, asfd, snew, NULL, NULL, conf))<0) goto end; else if(ars>0) return 0; // Finished. if(!(ec=entry_changed(asfd, snew, cmanio, unmanio, conf))) { // No change, no need to add to slist. continue; } else if(ec<0) goto end; // Error. if(data_needed(snew)) snew->flags|=SBUF_NEED_DATA; slist_add_sbuf(slist, snew); } return 0; end: sbuf_free(&snew); return ret; }
static void download_pipe_read(void *aux) { download_t *dn = aux; ssize_t len; char *s, *p; if (dn->pipe_fd < 0 || dn->pipe_pid == 0) return; while (1) { if (dn->pipe_sbuf.sb_ptr > 50*1024*1024) { errno = EMSGSIZE; goto failed; } sbuf_alloc(&dn->pipe_sbuf, 2048); len = sbuf_read(&dn->pipe_sbuf, dn->pipe_fd); if (len == 0) { s = dn->url ? strdupa(dn->url) : strdupa(""); p = strchr(s, ' '); if (p) *p = '\0'; p = strrchr(s, '/'); if (p) p++; sbuf_append(&dn->pipe_sbuf, "", 1); dn->process(dn->aux, p, NULL, (char *)dn->pipe_sbuf.sb_data, (size_t)dn->pipe_sbuf.sb_ptr); download_pipe_close(dn); return; } else if (len < 0) { if (ERRNO_AGAIN(errno)) break; failed: tvherror(dn->log, "pipe: read failed: %d", errno); download_pipe_close(dn); return; } } gtimer_arm_ms(&dn->pipe_read_timer, download_pipe_read, dn, 250); }
static void do_test_check_browsedir(enum protocol protocol) { struct sbuf *mb; const char *browsedir; char *last_bd_match=NULL; fail_unless((mb=sbuf_alloc(protocol))!=NULL); browsedir="/path"; run_check_browsedir(browsedir, mb, CMD_FILE, "/aaaa/path/file", &last_bd_match, NULL, 0, 0); run_check_browsedir(browsedir, mb, CMD_FILE, "/pat/aaa/file", &last_bd_match, NULL, 0, 0); run_check_browsedir(browsedir, mb, CMD_DIRECTORY, "/path", &last_bd_match, ".", 1, 1); run_check_browsedir(browsedir, mb, CMD_DIRECTORY, "/path/", &last_bd_match, ".", 0, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "/path/aa/file", &last_bd_match, "aa", 1, 1); // Get a bit more coverage by setting the statp mode to S_IFDIR. mb->statp.st_mode &= ~(S_IFMT); mb->statp.st_mode |= S_IFDIR; run_check_browsedir(browsedir, mb, CMD_FILE, "/path/to/file", &last_bd_match, "to", 1, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "/path/to/gggg", &last_bd_match, "to", 0, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "/path/to/gggg/zzz", &last_bd_match, "to", 0, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "/path/to/hhhh", &last_bd_match, "to", 0, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "/path/so/hhhh", &last_bd_match, "so", 1, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "/patha/aaa/file", &last_bd_match, "so", 0, 0); run_check_browsedir(browsedir, mb, CMD_FILE, "path/aaa/file", &last_bd_match, "so", 0, 0); sbuf_free(&mb); free_w(&last_bd_match); alloc_check(); }
static int browse_manifest_start(struct asfd *srfd, struct cstat *cstat, struct bu *bu, const char *browse, struct conf **confs) { int ret=-1; char *manifest=NULL; struct sbuf *sb=NULL; struct manio *manio=NULL; if(!(manifest=prepend_s(bu->path, cstat->protocol==PROTO_1?"manifest.gz":"manifest")) || !(manio=manio_open(manifest, "rb", cstat->protocol)) || !(sb=sbuf_alloc(cstat->protocol))) goto end; if(get_int(confs[OPT_MONITOR_BROWSE_CACHE])) ret=cache_load(srfd, manio, sb, cstat, bu); else ret=do_browse_manifest(srfd, manio, sb, browse); end: free_w(&manifest); manio_close(&manio); sbuf_free(&sb); return ret; }
END_TEST static void do_test_check_browsedir_root(enum protocol protocol) { struct sbuf *mb; const char *browsedir; char *last_bd_match=NULL; fail_unless((mb=sbuf_alloc(protocol))!=NULL); browsedir="/"; run_check_browsedir(browsedir, mb, CMD_FILE, "aaa", &last_bd_match, NULL, 0, 0); run_check_browsedir(browsedir, mb, CMD_DIRECTORY, "/", &last_bd_match, ".", 1, 1); run_check_browsedir(browsedir, mb, CMD_DIRECTORY, "/aa", &last_bd_match, "aa", 1, 1); run_check_browsedir(browsedir, mb, CMD_FILE, "/aa/bb", &last_bd_match, "aa", 0, 1); sbuf_free(&mb); free_w(&last_bd_match); alloc_check(); }
int do_list_client(struct asfd *asfd, struct conf *conf, enum action act, int json) { int ret=-1; char msg[512]=""; char *dpth=NULL; struct sbuf *sb=NULL; int json_started=0; struct iobuf *rbuf=asfd->rbuf; //logp("in do_list\n"); if(conf->browsedir) snprintf(msg, sizeof(msg), "listb %s:%s", conf->backup?conf->backup:"", conf->browsedir); else snprintf(msg, sizeof(msg), "list %s:%s", conf->backup?conf->backup:"", conf->regex?conf->regex:""); if(asfd->write_str(asfd, CMD_GEN, msg) || asfd->read_expect(asfd, CMD_GEN, "ok")) goto end; if(!(sb=sbuf_alloc(conf))) goto end; iobuf_init(&sb->path); iobuf_init(&sb->link); iobuf_init(&sb->attr); if(json) { open_tag(0, NULL); open_tag(1, "backups"); json_started++; } // This should probably should use the sbuf stuff. while(1) { sbuf_free_content(sb); iobuf_free_content(rbuf); if(asfd->read(asfd)) break; if(rbuf->cmd==CMD_TIMESTAMP) { // A backup timestamp, just print it. if(json) json_backup(rbuf->buf, conf); else { printf("Backup: %s\n", rbuf->buf); if(conf->browsedir) printf("Listing directory: %s\n", conf->browsedir); if(conf->regex) printf("With regex: %s\n", conf->regex); } continue; } else if(rbuf->cmd!=CMD_ATTRIBS) { iobuf_log_unexpected(rbuf, __func__); goto end; } iobuf_copy(&sb->attr, rbuf); iobuf_init(rbuf); attribs_decode(sb); if(asfd->read(asfd)) { logp("got stat without an object\n"); goto end; } iobuf_copy(&sb->path, rbuf); iobuf_init(rbuf); 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) { list_item(json, act, sb); } else if(cmd_is_link(sb->path.cmd)) // symlink or hardlink { if(asfd->read(asfd) || rbuf->cmd!=sb->path.cmd) { logp("could not get link %c:%s\n", sb->path.cmd, sb->path.buf); goto end; } iobuf_copy(&sb->link, rbuf); iobuf_init(rbuf); list_item(json, act, sb); } else { fprintf(stderr, "unlistable %c:%s\n", sb->path.cmd, sb->path.buf?sb->path.buf:""); } } ret=0; end: if(json && json_started) close_tag(0); printf("\n"); iobuf_free_content(&sb->path); iobuf_free_content(&sb->link); iobuf_free_content(&sb->attr); if(dpth) free(dpth); sbuf_free(&sb); if(!ret) logp("List finished ok\n"); return ret; }
int mp3_decoder_run(struct mp3_decoder* decoder) { int err; rt_uint16_t* buffer; rt_uint32_t delta; RT_ASSERT(decoder != RT_NULL); if ((decoder->read_ptr == RT_NULL) || decoder->bytes_left < 2*MAINBUF_SIZE) { if(mp3_decoder_fill_buffer(decoder) != 0) return -1; } // rt_kprintf("read offset: 0x%08x\n", decoder->read_ptr - decoder->read_buffer); decoder->read_offset = MP3FindSyncWord(decoder->read_ptr, decoder->bytes_left); if (decoder->read_offset < 0) { /* discard this data */ rt_kprintf("outof sync, byte left: %d\n", decoder->bytes_left); decoder->bytes_left = 0; return 0; } decoder->read_ptr += decoder->read_offset; delta = decoder->read_offset; decoder->bytes_left -= decoder->read_offset; if (decoder->bytes_left < 1024) { /* fill more data */ if(mp3_decoder_fill_buffer(decoder) != 0) return -1; } /* get a decoder buffer */ buffer = (rt_uint16_t*)sbuf_alloc(); decoder->bytes_left_before_decoding = decoder->bytes_left; err = MP3Decode(decoder->decoder, &decoder->read_ptr, (int*)&decoder->bytes_left, (short*)buffer, 0); delta += (decoder->bytes_left_before_decoding - decoder->bytes_left); current_offset += delta; decoder->frames++; if (err != ERR_MP3_NONE) { switch (err) { case ERR_MP3_INDATA_UNDERFLOW: rt_kprintf("ERR_MP3_INDATA_UNDERFLOW\n"); decoder->bytes_left = 0; if(mp3_decoder_fill_buffer(decoder) != 0) { /* release this memory block */ sbuf_release(buffer); return -1; } break; case ERR_MP3_MAINDATA_UNDERFLOW: /* do nothing - next call to decode will provide more mainData */ rt_kprintf("ERR_MP3_MAINDATA_UNDERFLOW\n"); break; default: rt_kprintf("unknown error: %d, left: %d\n", err, decoder->bytes_left); // skip this frame if (decoder->bytes_left > 0) { decoder->bytes_left --; decoder->read_ptr ++; } else { // TODO RT_ASSERT(0); } break; } /* release this memory block */ sbuf_release(buffer); } else { int outputSamps; /* no error */ MP3GetLastFrameInfo(decoder->decoder, &decoder->frame_info); /* set sample rate */ if (decoder->frame_info.samprate != current_sample_rate) { current_sample_rate = decoder->frame_info.samprate; rt_device_control(decoder->snd_device, CODEC_CMD_SAMPLERATE, ¤t_sample_rate); } /* write to sound device */ outputSamps = decoder->frame_info.outputSamps; if (outputSamps > 0) { if (decoder->frame_info.nChans == 1) { int i; for (i = outputSamps - 1; i >= 0; i--) { buffer[i * 2] = buffer[i]; buffer[i * 2 + 1] = buffer[i]; } outputSamps *= 2; } rt_device_write(decoder->snd_device, 0, buffer, outputSamps * sizeof(rt_uint16_t)); } else { /* no output */ sbuf_release(buffer); } } return 0; }
static #endif int do_backup_phase2_server_protocol2(struct async *as, struct asfd *chfd, struct sdirs *sdirs, int resume, struct conf **confs) { int ret=-1; uint8_t end_flags=0; struct slist *slist=NULL; struct iobuf wbuf; struct dpth *dpth=NULL; man_off_t *p1pos=NULL; struct manios *manios=NULL; // This is used to tell the client that a number of consecutive blocks // have been found and can be freed. uint64_t wrap_up=0; struct asfd *asfd=NULL; struct cntr *cntr=NULL; struct sbuf *csb=NULL; uint64_t file_no=1; if(!as) { logp("async not provided to %s()\n", __func__); goto end; } if(!sdirs) { logp("sdirs not provided to %s()\n", __func__); goto end; } if(!confs) { logp("confs not provided to %s()\n", __func__); goto end; } asfd=as->asfd; if(!asfd) { logp("asfd not provided to %s()\n", __func__); goto end; } if(!chfd) { logp("chfd not provided to %s()\n", __func__); goto end; } cntr=get_cntr(confs); if(get_int(confs[OPT_BREAKPOINT])>=2000 && get_int(confs[OPT_BREAKPOINT])<3000) { breaking=get_int(confs[OPT_BREAKPOINT]); breakcount=breaking-2000; } logp("Phase 2 begin (recv backup data)\n"); if(!(dpth=dpth_alloc()) || dpth_protocol2_init(dpth, sdirs->data, get_string(confs[OPT_CNAME]), sdirs->cfiles, get_int(confs[OPT_MAX_STORAGE_SUBDIRS]))) goto end; if(resume && !(p1pos=do_resume(sdirs, dpth, confs))) goto end; if(!(manios=manios_open_phase2(sdirs, p1pos, PROTO_2)) || !(slist=slist_alloc()) || !(csb=sbuf_alloc(PROTO_2))) goto end; iobuf_free_content(asfd->rbuf); memset(&wbuf, 0, sizeof(struct iobuf)); while(!(end_flags&END_BACKUP)) { if(maybe_add_from_scan(manios, slist, chfd, &csb)) goto end; if(!wbuf.len) { if(get_wbuf_from_sigs(&wbuf, slist, &end_flags)) goto end; if(!wbuf.len) { get_wbuf_from_files(&wbuf, slist, manios, &end_flags, &file_no); } } if(wbuf.len && asfd->append_all_to_write_buffer(asfd, &wbuf)==APPEND_ERROR) goto end; if(append_for_champ_chooser(chfd, slist->blist, end_flags)) goto end; if(as->read_write(as)) { logp("error from as->read_write in %s\n", __func__); goto end; } while(asfd->rbuf->buf) { if(deal_with_read(asfd->rbuf, slist, cntr, &end_flags, dpth)) goto end; // Get as much out of the readbuf as possible. if(asfd->parse_readbuf(asfd)) goto end; } while(chfd->rbuf->buf) { if(deal_with_read_from_chfd(chfd, slist->blist, &wrap_up, dpth, cntr)) goto end; // Get as much out of the readbuf as possible. if(chfd->parse_readbuf(chfd)) goto end; } if(write_to_changed_file(asfd, chfd, manios, slist, end_flags)) goto end; } // Hack: If there are some entries left after the last entry that // contains block data, it will not be written to the changed file // yet because the last entry of block data has not had // sb->protocol2->bend set. if(slist->head && slist->head->next) { struct sbuf *sb=NULL; sb=slist->head; slist->head=sb->next; sbuf_free(&sb); if(write_to_changed_file(asfd, chfd, manios, slist, end_flags)) goto end; } if(manios_close(&manios)) goto end; if(check_for_missing_work_in_slist(slist)) goto end; // Need to release the last left. There should be one at most. if(dpth->head && dpth->head->next) { logp("ERROR: More data locks remaining after: %s\n", dpth->head->save_path); goto end; } if(dpth_release_all(dpth)) goto end; ret=0; end: logp("End backup\n"); sbuf_free(&csb); slist_free(&slist); if(asfd) iobuf_free_content(asfd->rbuf); if(chfd) iobuf_free_content(chfd->rbuf); dpth_free(&dpth); manios_close(&manios); man_off_t_free(&p1pos); return ret; }
// This is basically backup_phase3_server() from protocol1. It used to merge the // unchanged and changed data into a single file. Now it splits the manifests // into several files. int backup_phase3_server_protocol2(struct sdirs *sdirs, struct conf **confs) { int ret=1; int pcmp=0; char *hooksdir=NULL; char *dindexdir=NULL; char *manifesttmp=NULL; struct sbuf *usb=NULL; struct sbuf *csb=NULL; struct blk *blk=NULL; int finished_ch=0; int finished_un=0; struct manio *newmanio=NULL; struct manio *chmanio=NULL; struct manio *unmanio=NULL; uint64_t fcount=0; logp("Start phase3\n"); if(!(manifesttmp=get_tmp_filename(sdirs->rmanifest)) || !(newmanio=manio_alloc()) || !(chmanio=manio_alloc()) || !(unmanio=manio_alloc()) || !(hooksdir=prepend_s(manifesttmp, "hooks")) || !(dindexdir=prepend_s(manifesttmp, "dindex")) || manio_init_write(newmanio, manifesttmp) || manio_init_write_hooks(newmanio, get_string(confs[OPT_DIRECTORY]), hooksdir, sdirs->rmanifest) || manio_init_write_dindex(newmanio, dindexdir) || manio_init_read(chmanio, sdirs->changed) || manio_init_read(unmanio, sdirs->unchanged) || !(usb=sbuf_alloc(confs)) || !(csb=sbuf_alloc(confs))) goto end; while(!finished_ch || !finished_un) { if(!blk && !(blk=blk_alloc())) goto end; if(!finished_un && usb && !usb->path.buf) { switch(manio_sbuf_fill(unmanio, NULL /* no async */, usb, NULL, NULL, confs)) { case -1: goto end; case 1: finished_un++; } } if(!finished_ch && csb && !csb->path.buf) { switch(manio_sbuf_fill(chmanio, NULL /* no async */, csb, NULL, NULL, confs)) { case -1: goto end; case 1: finished_ch++; } } if((usb && usb->path.buf) && (!csb || !csb->path.buf)) { switch(manio_copy_entry(NULL /* no async */, &usb, usb, &blk, unmanio, newmanio, confs)) { case -1: goto end; case 1: finished_un++; } } else if((!usb || !usb->path.buf) && (csb && csb->path.buf)) { switch(manio_copy_entry(NULL /* no async */, &csb, csb, &blk, chmanio, newmanio, confs)) { case -1: goto end; case 1: finished_ch++; } } else if((!usb || !usb->path.buf) && (!csb || !(csb->path.buf))) { continue; } else if(!(pcmp=sbuf_pathcmp(usb, csb))) { // They were the same - write one. switch(manio_copy_entry(NULL /* no async */, &csb, csb, &blk, chmanio, newmanio, confs)) { case -1: goto end; case 1: finished_ch++; } } else if(pcmp<0) { switch(manio_copy_entry(NULL /* no async */, &usb, usb, &blk, unmanio, newmanio, confs)) { case -1: goto end; case 1: finished_un++; } } else { switch(manio_copy_entry(NULL /* no async */, &csb, csb, &blk, chmanio, newmanio, confs)) { case -1: goto end; case 1: finished_ch++; } } } fcount=newmanio->fcount; // Flush to disk and set up for reading. if(manio_free(&newmanio) || !(newmanio=manio_alloc()) || manio_init_read(newmanio, sdirs->rmanifest)) goto end; // Rename race condition should be of no consequence here, as the // manifest should just get recreated automatically. if(do_rename(manifesttmp, sdirs->rmanifest)) goto end; else { recursive_delete(sdirs->changed, NULL, 1); recursive_delete(sdirs->unchanged, NULL, 1); } if(sparse_generation(newmanio, fcount, sdirs, confs)) goto end; ret=0; logp("End phase3\n"); end: manio_free(&newmanio); manio_free(&chmanio); manio_free(&unmanio); sbuf_free(&csb); sbuf_free(&usb); blk_free(&blk); free_w(&hooksdir); free_w(&dindexdir); free_w(&manifesttmp); return ret; }
// Combine the phase1 and phase2 files into a new manifest. int backup_phase3_server_all(struct sdirs *sdirs, struct conf **confs) { int ret=-1; int pcmp=0; struct blk *blk=NULL; struct sbuf *usb=NULL; struct sbuf *csb=NULL; char *manifesttmp=NULL; struct manio *newmanio=NULL; struct manio *chmanio=NULL; struct manio *unmanio=NULL; enum protocol protocol=get_protocol(confs); struct cntr *cntr=get_cntr(confs); const char *rmanifest_relative=NULL; logp("Begin phase3 (merge manifests)\n"); if(protocol==PROTO_2) rmanifest_relative=get_rmanifest_relative(sdirs, confs); if(!(manifesttmp=get_tmp_filename(sdirs->manifest)) || !(newmanio=manio_open_phase3(manifesttmp, comp_level(get_int(confs[OPT_COMPRESSION])), protocol, rmanifest_relative)) || !(chmanio=manio_open_phase2(sdirs->changed, "rb", protocol)) || !(unmanio=manio_open_phase2(sdirs->unchanged, "rb", protocol)) || !(usb=sbuf_alloc(protocol)) || !(csb=sbuf_alloc(protocol))) goto end; while(chmanio || unmanio) { if(!blk && !(blk=blk_alloc())) goto end; if(unmanio && !usb->path.buf) { switch(manio_read(unmanio, usb)) { case -1: goto end; case 1: manio_close(&unmanio); } } if(chmanio && !csb->path.buf) { switch(manio_read(chmanio, csb)) { case -1: goto end; case 1: manio_close(&chmanio); } } if(usb->path.buf && !csb->path.buf) { if(write_status(CNTR_STATUS_MERGING, usb->path.buf, cntr)) goto end; switch(manio_copy_entry( usb, usb, &blk, unmanio, newmanio)) { case -1: goto end; case 1: manio_close(&unmanio); } } else if(!usb->path.buf && csb->path.buf) { if(write_status(CNTR_STATUS_MERGING, csb->path.buf, cntr)) goto end; switch(manio_copy_entry( csb, csb, &blk, chmanio, newmanio)) { case -1: goto end; case 1: manio_close(&chmanio); } } else if(!usb->path.buf && !csb->path.buf) { continue; } else if(!(pcmp=sbuf_pathcmp(usb, csb))) { // They were the same - write one. if(write_status(CNTR_STATUS_MERGING, csb->path.buf, cntr)) goto end; switch(manio_copy_entry( csb, csb, &blk, chmanio, newmanio)) { case -1: goto end; case 1: manio_close(&chmanio); } } else if(pcmp<0) { if(write_status(CNTR_STATUS_MERGING, usb->path.buf, cntr)) goto end; switch(manio_copy_entry( usb, usb, &blk, unmanio, newmanio)) { case -1: goto end; case 1: manio_close(&unmanio); } } else { if(write_status(CNTR_STATUS_MERGING, csb->path.buf, cntr)) goto end; switch(manio_copy_entry( csb, csb, &blk, chmanio, newmanio)) { case -1: goto end; case 1: manio_close(&chmanio); } } } // Flush to disk. if(manio_close(&newmanio)) { logp("error gzclosing %s in backup_phase3_server\n", manifesttmp); goto end; } // Rename race condition should be of no consequence here, as the // manifest should just get recreated automatically. if(do_rename(manifesttmp, sdirs->manifest)) goto end; else { recursive_delete(sdirs->changed); recursive_delete(sdirs->unchanged); } logp("End phase3 (merge manifests)\n"); ret=0; end: manio_close(&newmanio); manio_close(&chmanio); manio_close(&unmanio); sbuf_free(&csb); sbuf_free(&usb); blk_free(&blk); free_w(&manifesttmp); return ret; }
/* Merge two files of sorted dindexes into each other. */ int merge_dindexes(const char *dst, const char *srca, const char *srcb) { int ret=-1; struct sbuf *asb=NULL; struct sbuf *bsb=NULL; struct fzp *azp=NULL; struct fzp *bzp=NULL; struct fzp *dzp=NULL; uint64_t *anew=NULL; uint64_t *bnew=NULL; if(!(asb=sbuf_alloc(PROTO_2)) || (srcb && !(bsb=sbuf_alloc(PROTO_2)))) goto end; if(build_path_w(dst)) goto end; if((srca && !(azp=fzp_gzopen(srca, "rb"))) || (srcb && !(bzp=fzp_gzopen(srcb, "rb"))) || !(dzp=fzp_gzopen(dst, "wb"))) goto end; while(azp || bzp || anew || bnew) { if(azp && asb && !anew) { switch(get_next_dindex(&anew, asb, azp)) { case -1: goto end; case 1: fzp_close(&azp); // Finished OK. } } if(bzp && bsb && !bnew) { switch(get_next_dindex(&bnew, bsb, bzp)) { case -1: goto end; case 1: fzp_close(&bzp); // Finished OK. } } if(anew && !bnew) { if(gzprintf_dindex(dzp, anew)) goto end; free_v((void **)&anew); } else if(!anew && bnew) { if(gzprintf_dindex(dzp, bnew)) goto end; free_v((void **)&bnew); } else if(!anew && !bnew) { continue; } else if(*anew==*bnew) { // They were the same - write the new one. if(gzprintf_dindex(dzp, bnew)) goto end; free_v((void **)&anew); free_v((void **)&bnew); } else if(*anew<*bnew) { if(gzprintf_dindex(dzp, anew)) goto end; free_v((void **)&anew); } else { if(gzprintf_dindex(dzp, bnew)) goto end; free_v((void **)&bnew); } } if(fzp_close(&dzp)) { logp("Error closing %s in %s\n", tmpfile, __func__); goto end; } ret=0; end: fzp_close(&azp); fzp_close(&bzp); fzp_close(&dzp); sbuf_free(&asb); sbuf_free(&bsb); free_v((void **)&anew); free_v((void **)&bnew); return ret; }
static #endif int merge_sparse_indexes(const char *dst, const char *srca, const char *srcb) { int fcmp; int ret=-1; struct sbuf *asb=NULL; struct sbuf *bsb=NULL; uint64_t *afingerprints=NULL; uint64_t *bfingerprints=NULL; size_t aflen=0; size_t bflen=0; struct fzp *azp=NULL; struct fzp *bzp=NULL; struct fzp *dzp=NULL; struct hooks *anew=NULL; struct hooks *bnew=NULL; char *apath=NULL; char *bpath=NULL; if(!(asb=sbuf_alloc(PROTO_2)) || (srcb && !(bsb=sbuf_alloc(PROTO_2)))) goto end; if(build_path_w(dst)) goto end; if((srca && !(azp=fzp_gzopen(srca, "rb"))) || (srcb && !(bzp=fzp_gzopen(srcb, "rb"))) || !(dzp=fzp_gzopen(dst, "wb"))) goto end; while(azp || bzp || anew || bnew) { if(azp && asb && !anew) { switch(get_next_set_of_hooks(&anew, asb, azp, &apath, &afingerprints, &aflen)) { case -1: goto end; case 1: fzp_close(&azp); // Finished OK. } } if(bzp && bsb && !bnew) { switch(get_next_set_of_hooks(&bnew, bsb, bzp, &bpath, &bfingerprints, &bflen)) { case -1: goto end; case 1: fzp_close(&bzp); // Finished OK. } } if(anew && !bnew) { if(gzprintf_hooks(dzp, anew)) goto end; hooks_free(&anew); } else if(!anew && bnew) { if(gzprintf_hooks(dzp, bnew)) goto end; hooks_free(&bnew); } else if(!anew && !bnew) { continue; } else if(!(fcmp=hookscmp(anew, bnew))) { // They were the same - write the new one. if(gzprintf_hooks(dzp, bnew)) goto end; hooks_free(&anew); hooks_free(&bnew); } else if(fcmp<0) { if(gzprintf_hooks(dzp, anew)) goto end; hooks_free(&anew); } else { if(gzprintf_hooks(dzp, bnew)) goto end; hooks_free(&bnew); } } if(fzp_close(&dzp)) { logp("Error closing %s in %s\n", tmpfile, __func__); goto end; } ret=0; end: fzp_close(&azp); fzp_close(&bzp); fzp_close(&dzp); sbuf_free(&asb); sbuf_free(&bsb); hooks_free(&anew); hooks_free(&bnew); free_v((void **)&afingerprints); free_v((void **)&bfingerprints); free_w(&apath); free_w(&bpath); return ret; }