static int make_rev_sig(const char *dst, const char *sig, const char *endfile, int compression, struct cntr *cntr) { FILE *dstfp=NULL; gzFile dstzp=NULL; FILE *sigp=NULL; rs_result result; //logp("make rev sig: %s %s\n", dst, sig); if(dpth_is_compressed(compression, dst)) dstzp=gzopen_file(dst, "rb"); else dstfp=open_file(dst, "rb"); if((!dstzp && !dstfp) || !(sigp=open_file(sig, "wb"))) { gzclose_fp(&dstzp); close_fp(&dstfp); return -1; } result=rs_sig_gzfile(dstfp, dstzp, sigp, get_librsync_block_len(endfile), RS_DEFAULT_STRONG_LEN, NULL, cntr); gzclose_fp(&dstzp); close_fp(&dstfp); close_fp(&sigp); //logp("end of make rev sig\n"); return result; }
static void ncp_put_super(struct super_block *sb) { struct ncp_server *server = NCP_SBP(sb); lock_super(sb); ncp_lock_server(server); ncp_disconnect(server); ncp_unlock_server(server); close_fp(server->ncp_filp); ncp_dont_catch_watchdog(server); close_fp(server->wdog_filp); close_fp(server->msg_filp); ncp_free_all_inodes(server); ncp_kfree_s(server->packet, server->packet_size); sb->s_dev = 0; ncp_kfree_s(NCP_SBP(sb), sizeof(struct ncp_server)); NCP_SBP(sb) = NULL; unlock_super(sb); MOD_DEC_USE_COUNT; }
static int make_rev_sig(const char *dst, const char *sig, const char *endfile, int compression, struct conf *conf) { int ret=-1; FILE *dstfp=NULL; gzFile dstzp=NULL; FILE *sigp=NULL; //logp("make rev sig: %s %s\n", dst, sig); if(dpthl_is_compressed(compression, dst)) dstzp=gzopen_file(dst, "rb"); else dstfp=open_file(dst, "rb"); if((!dstzp && !dstfp) || !(sigp=open_file(sig, "wb")) || rs_sig_gzfile(NULL, dstfp, dstzp, sigp, get_librsync_block_len(endfile), RS_DEFAULT_STRONG_LEN, NULL, conf->cntr)!=RS_DONE) goto end; ret=0; end: //logp("end of make rev sig\n"); gzclose_fp(&dstzp); close_fp(&dstfp); if(close_fp(&sigp)) { logp("error closing %s in %s\n", sig, __func__); return -1; } return ret; }
static int compress(const char *src, const char *dst, struct config *cconf) { int res; int got; FILE *mp=NULL; gzFile zp=NULL; char buf[ZCHUNK]; if(!(mp=open_file(src, "rb")) || !(zp=gzopen_file(dst, comp_level(cconf)))) { close_fp(&mp); gzclose_fp(&zp); return -1; } while((got=fread(buf, 1, sizeof(buf), mp))>0) { res=gzwrite(zp, buf, got); if(res!=got) { logp("compressing manifest - read %d but wrote %d\n", got, res); close_fp(&mp); gzclose_fp(&zp); return -1; } } close_fp(&mp); return gzclose_fp(&zp); // this can give an error when out of space }
/* Rewrite the config file with the ssl_peer_cn value changed to what the server told us it should be. */ static int rewrite_client_conf(struct config *conf) { int ret=-1; char p[32]=""; FILE *dp=NULL; FILE *sp=NULL; char *tmp=NULL; char buf[4096]=""; logp("Rewriting config file: %s\n", conf->configfile); snprintf(p, sizeof(p), ".%d", getpid()); if(!(tmp=prepend(conf->configfile, p, strlen(p), ""))) goto end; if(!(sp=open_file(conf->configfile, "rb")) || !(dp=open_file(tmp, "wb"))) goto end; while(fgets(buf, sizeof(buf), sp)) { char *copy=NULL; char *field=NULL; char *value=NULL; if(!(copy=strdup(buf))) { logp("out of memory\n"); goto end; } if(config_get_pair(buf, &field, &value) || !field || !value || strcmp(field, "ssl_peer_cn")) { fprintf(dp, "%s", copy); free(copy); continue; } free(copy); fprintf(dp, "ssl_peer_cn = %s\n", conf->ssl_peer_cn); } close_fp(&sp); close_fp(&dp); #ifdef HAVE_WIN32 // Need to delete the destination, or Windows gets upset. unlink(conf->configfile); #endif if(do_rename(tmp, conf->configfile)) goto end; ret=0; end: close_fp(&sp); close_fp(&dp); if(ret) { logp("Rewrite failed\n"); unlink(tmp); } if(tmp) free(tmp); return ret; }
static int inflate_or_link_oldfile(const char *oldpath, const char *infpath) { int ret=0; struct stat statp; if(lstat(oldpath, &statp)) { logp("could not lstat %s\n", oldpath); return -1; } if(dpth_is_compressed(oldpath)) { FILE *source=NULL; FILE *dest=NULL; //logp("inflating...\n"); if(!(dest=open_file(infpath, "wb"))) { close_fp(&dest); return -1; } if(!statp.st_size) { // Empty file - cannot inflate. // just close the destination and we have duplicated a // zero length file. logp("asked to inflate zero length file: %s\n", oldpath); close_fp(&dest); return 0; } if(!(source=open_file(oldpath, "rb"))) { close_fp(&dest); return -1; } if((ret=zlib_inflate(source, dest))!=Z_OK) logp("zlib_inflate returned: %d\n", ret); close_fp(&source); close_fp(&dest); } else { // Not compressed - just hard link it. if(link(oldpath, infpath)) { logp("hardlink %s to %s failed: %s\n", infpath, oldpath, strerror(errno)); ret=-1; } } return ret; }
static int make_rev_delta(const char *src, const char *sig, const char *del, int compression, struct conf *cconf) { int ret=-1; FILE *srcfp=NULL; FILE *delfp=NULL; FILE *sigp=NULL; gzFile srczp=NULL; gzFile delzp=NULL; rs_signature_t *sumset=NULL; //logp("make rev delta: %s %s %s\n", src, sig, del); if(!(sigp=open_file(sig, "rb"))) goto end; if(rs_loadsig_file(sigp, &sumset, NULL)!=RS_DONE || rs_build_hash_table(sumset)!=RS_DONE) goto end; //logp("make rev deltb: %s %s %s\n", src, sig, del); if(dpthl_is_compressed(compression, src)) srczp=gzopen_file(src, "rb"); else srcfp=open_file(src, "rb"); if(!srczp && !srcfp) goto end; if(cconf->compression) delzp=gzopen_file(del, comp_level(cconf)); else delfp=open_file(del, "wb"); if(!delzp && !delfp) goto end; if(rs_delta_gzfile(NULL, sumset, srcfp, srczp, delfp, delzp, NULL, cconf->cntr)!=RS_DONE) goto end; ret=0; end: if(sumset) rs_free_sumset(sumset); gzclose_fp(&srczp); close_fp(&srcfp); close_fp(&sigp); if(gzclose_fp(&delzp)) { logp("error closing zp %s in %s\n", del, __func__); ret=-1; } if(close_fp(&delfp)) { logp("error closing fp %s in %s\n", del, __func__); ret=-1; } return ret; }
// Also used by backup_phase4_server.c int do_patch(const char *dst, const char *del, const char *upd, bool gzupd, struct cntr *cntr, struct config *cconf) { FILE *dstp=NULL; FILE *delfp=NULL; gzFile delzp=NULL; gzFile updp=NULL; FILE *updfp=NULL; rs_result result; //logp("patching...\n"); if(!(dstp=fopen(dst, "rb"))) { logp("could not open %s for reading\n", dst); return -1; } if(dpth_is_compressed(del)) delzp=gzopen(del, "rb"); else delfp=fopen(del, "rb"); if(!delzp && !delfp) { logp("could not open %s for reading\n", del); close_fp(&dstp); return -1; } if(gzupd) updp=gzopen(upd, comp_level(cconf)); else updfp=fopen(upd, "wb"); if(!updp && !updfp) { logp("could not open %s for writing\n", upd); close_fp(&dstp); gzclose_fp(&delzp); close_fp(&delfp); return -1; } result=rs_patch_gzfile(dstp, delfp, delzp, updfp, updp, NULL, cntr); fclose(dstp); gzclose_fp(&delzp); close_fp(&delfp); if(updp) gzclose_fp(&updp); if(updfp) fclose(updfp); return result; }
int manio_read_fcount(struct manio *manio) { int ret=-1; size_t s; FILE *fp=NULL; char *path=NULL; char buf[16]=""; if(!(path=get_fcount_path(manio)) || !(fp=open_file(path, "rb"))) goto end; if(!fgets(buf, sizeof(buf), fp)) { logp("fgets on %s failed\n", path); goto end; } s=strlen(buf); if(s!=9) { logp("data in %s is not the right length (%s!=9)\n", s); goto end; } manio->offset.fcount=strtoul(buf, NULL, 16); ret=0; end: close_fp(&fp); free_w(&path); return ret; }
static int incexc_matches(const char *fullrealwork, const char *incexc) { int ret=0; int got=0; FILE *fp=NULL; char buf[4096]=""; const char *inc=NULL; char *old_incexc_path=NULL; if(!(old_incexc_path=prepend_s(fullrealwork, "incexc"))) return -1; if(!(fp=open_file(old_incexc_path, "rb"))) { // Assume that no incexc file could be found because the client // was on an old version. Assume resume is OK and return 1. ret=1; goto end; } inc=incexc; while((got=fread(buf, 1, sizeof(buf), fp))>0) { if(strlen(inc)<(size_t)got) break; if(strncmp(buf, inc, got)) break; inc+=got; } if(inc && strlen(inc)) ret=0; else ret=1; end: close_fp(&fp); free(old_incexc_path); return ret; }
int zlib_inflate(struct asfd *asfd, const char *source, const char *dest, struct conf *conf) { int ret=-1; size_t b=0; FILE *fp=NULL; gzFile zp=NULL; unsigned char in[ZCHUNK]; if(!(zp=gzopen_file(source, "rb"))) { logw(asfd, conf, "could not open %s in %s\n", source, __func__); goto end; } if(!(fp=open_file(dest, "wb"))) { logw(asfd, conf, "could not open %s in %s: %s\n", dest, __func__, strerror(errno)); goto end; } while((b=gzread(zp, in, ZCHUNK))>0) { if(fwrite(in, 1, b, fp)!=b) { logw(asfd, conf, "error when writing to %s\n", dest); goto end; } } if(!gzeof(zp)) { logw(asfd, conf, "error while gzreading %s in %s\n", source, __func__); goto end; } if(close_fp(&fp)) { logw(asfd, conf, "error when closing %s in %s: %s\n", dest, __func__, strerror(errno)); goto end; } ret=0; end: gzclose_fp(&zp); close_fp(&fp); return ret; }
// Also used by restore.c. // FIX THIS: This stuff is very similar to make_rev_delta, can maybe share // some code. int do_patch(struct asfd *asfd, const char *dst, const char *del, const char *upd, bool gzupd, int compression, struct conf *cconf) { FILE *dstp=NULL; FILE *delfp=NULL; gzFile delzp=NULL; gzFile updp=NULL; FILE *updfp=NULL; rs_result result=RS_IO_ERROR; //logp("patching...\n"); if(!(dstp=open_file(dst, "rb"))) goto end; if(dpthl_is_compressed(compression, del)) delzp=gzopen_file(del, "rb"); else delfp=open_file(del, "rb"); if(!delzp && !delfp) goto end; if(gzupd) updp=gzopen(upd, comp_level(cconf)); else updfp=fopen(upd, "wb"); if(!updp && !updfp) goto end; result=rs_patch_gzfile(asfd, dstp, delfp, delzp, updfp, updp, NULL, cconf->cntr); end: close_fp(&dstp); gzclose_fp(&delzp); close_fp(&delfp); if(close_fp(&updfp)) { logp("error closing %s in %s\n", upd, __func__); result=RS_IO_ERROR; } if(gzclose_fp(&updp)) { logp("error gzclosing %s in %s\n", upd, __func__); result=RS_IO_ERROR; } return result; }
int close_file_for_send(BFILE *bfd, FILE **fp) { if(fp) return close_fp(fp); #ifdef HAVE_WIN32 if(bfd) return bclose(bfd); #endif return -1; }
void nfs_put_super(struct super_block *sb) { /* No locks should be open on this, so 0 should be safe as a fd. */ close_fp(sb->u.nfs_sb.s_server.file, 0); lock_super(sb); sb->s_dev = 0; unlock_super(sb); }
int dpth_release_all(struct dpth *dpth) { int ret=0; if(!dpth) return 0; if(dpth->fp && close_fp(&dpth->fp)) ret=-1; while(dpth->head) if(release_and_move_to_next_in_list(dpth)) ret=-1; return ret; }
void nfs_put_super(struct super_block *sb) { close_fp(sb->u.nfs_sb.s_server.file); rpc_closesock(sb->u.nfs_sb.s_server.rsock); lock_super(sb); sb->s_dev = 0; unlock_super(sb); MOD_DEC_USE_COUNT; }
static void unix_fd_free(struct sock *sk, struct file **fp, int num) { int i; for(i=0;i<num;i++) { close_fp(fp[i]); unix_notinflight(fp[i]); } }
static int copy_path_to_File(const char *src, FILE *dp) { size_t b=0; size_t w=0; unsigned char in[ZCHUNK]; FILE *sp=NULL; if(!(sp=open_file(src, "rb"))) return -1; if(copy_File_to_File(sp, dp)) { close_fp(&sp); return -1; } close_fp(&sp); return 0; }
int cntr_stats_to_file(struct cntr *cntr, const char *directory, enum action act) { int x=0; int ret=-1; FILE *fp; char *path; time_t now; const char *fname=NULL; if(act==ACTION_BACKUP || act==ACTION_BACKUP_TIMED) fname="backup_stats"; else if(act==ACTION_RESTORE) fname="restore_stats"; else if(act==ACTION_VERIFY) fname="verify_stats"; else return 0; now=time(NULL); if(!(path=prepend_s(directory, fname)) || !(fp=open_file(path, "wb"))) goto end; fprintf(fp, "client:%s\n", cntr->cname); fprintf(fp, "time_start:%lu\n", cntr->start); fprintf(fp, "time_end:%lu\n", now); fprintf(fp, "time_taken:%lu\n", now-cntr->start); for(x=0; x<cntr->colen; x++) quint_print_to_file(fp, cntr->ent[(uint8_t)cntr->cmd_order[x]], act); bottom_part_to_file(cntr, fp, act); if(close_fp(&fp)) goto end; ret=0; end: free(path); close_fp(&fp); return ret; }
void _close_allfiles(void) { register struct file *filp; register char *pi = 0; do { if ((filp = current->files.fd[(int) pi])) { current->files.fd[(int) pi] = NULL; close_fp(filp); } } while (((int)(++pi)) < NR_OPEN); }
static int deal_with_receive_end_file(struct asfd *asfd, struct sdirs *sdirs, struct sbuf *rb, FILE *p2fp, struct conf *cconf, char **last_requested) { static char *cp=NULL; static struct iobuf *rbuf; rbuf=asfd->rbuf; // Finished the file. // Write it to the phase2 file, and free the buffers. if(close_fp(&(rb->burp1->fp))) { logp("error closing delta for %s in receive\n", rb->path); goto error; } if(gzclose_fp(&(rb->burp1->zp))) { logp("error gzclosing delta for %s in receive\n", rb->path); goto error; } iobuf_copy(&rb->burp1->endfile, rbuf); rbuf->buf=NULL; if(rb->flags & SBUFL_RECV_DELTA && finish_delta(sdirs, rb)) goto error; if(sbufl_to_manifest(rb, p2fp, NULL)) goto error; if(rb->flags & SBUFL_RECV_DELTA) cntr_add_changed(cconf->cntr, rb->path.cmd); else cntr_add(cconf->cntr, rb->path.cmd, 0); if(*last_requested && !strcmp(rb->path.buf, *last_requested)) { free(*last_requested); *last_requested=NULL; } cp=strchr(rb->burp1->endfile.buf, ':'); if(rb->burp1->endfile.buf) cntr_add_bytes(cconf->cntr, strtoull(rb->burp1->endfile.buf, NULL, 10)); if(cp) { // checksum stuff goes here } sbuf_free_content(rb); return 0; error: sbuf_free_content(rb); return -1; }
static int copy_File_to_path(FILE *sp, const char *dst) { FILE *dp=NULL; if(!(dp=open_file(dst, "wb"))) return -1; if(copy_File_to_File(sp, dp)) { close_fp(&dp); return -1; } if(close_fp(&dp)) { logp("failed close_fp in %s\n", __FUNCTION__); return -1; } return 0; }
/* These receive_a_file() and send_file() functions are for use by extra_comms and the CA stuff, rather than backups/restores. */ int receive_a_file(const char *path, struct cntr *p1cntr) { int c=0; int ret=0; #ifdef HAVE_WIN32 BFILE bfd; #else FILE *fp=NULL; #endif unsigned long long rcvdbytes=0; unsigned long long sentbytes=0; #ifdef HAVE_WIN32 binit(&bfd, 0); bfd.use_backup_api=0; //set_win32_backup(&bfd); if(bopen(&bfd, path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR, 0)<=0) { berrno be; logp("Could not open for writing %s: %s\n", path, be.bstrerror(errno)); ret=-1; goto end; } #else if(!(fp=open_file(path, "wb"))) { ret=-1; goto end; } #endif #ifdef HAVE_WIN32 ret=transfer_gzfile_in(NULL, path, &bfd, NULL, &rcvdbytes, &sentbytes, NULL, 0, p1cntr, NULL); c=bclose(&bfd); #else ret=transfer_gzfile_in(NULL, path, NULL, fp, &rcvdbytes, &sentbytes, NULL, 0, p1cntr, NULL); c=close_fp(&fp); #endif end: if(c) { logp("error closing %s in receive_a_file\n", path); ret=-1; } if(!ret) logp("Received: %s\n", path); return ret; }
int sys_close(unsigned int fd) { register struct file *filp; register struct file_struct *cfiles = ¤t->files; if (fd < NR_OPEN) { (void) clear_bit(fd, &cfiles->close_on_exec); if ((filp = cfiles->fd[fd])) { cfiles->fd[fd] = NULL; return close_fp(filp); } } return -EBADF; }
static void unix_detach_fds(struct sk_buff *skb, struct cmsghdr *cmsg) { int i; /* count of space in parent for fds */ int cmnum; struct file **fp; int *cmfptr; int fdnum; cmfptr = NULL; cmnum = 0; if (cmsg) { cmnum = (cmsg->cmsg_len-sizeof(struct cmsghdr)) / sizeof(int); cmfptr = (int *)&cmsg->cmsg_data; } fdnum = *(int *)skb->h.filp; fp = (struct file **)(skb->h.filp+sizeof(long)); if (cmnum > fdnum) cmnum = fdnum; /* * Copy those that fit */ for (i = 0 ; i < cmnum ; i++) { int new_fd = get_unused_fd(); if (new_fd < 0) break; current->files->fd[new_fd]=fp[i]; *cmfptr++ = new_fd; unix_notinflight(fp[i]); } /* * Dump those that don't */ for( ; i < fdnum ; i++) { close_fp(fp[i]); unix_notinflight(fp[i]); } kfree(skb->h.filp); skb->h.filp=NULL; /* no need to use destructor */ skb->destructor = NULL; }
asmlinkage int sys_close(unsigned int fd) { int error; struct file * filp; struct files_struct * files; files = current->files; error = -EBADF; if (fd < NR_OPEN && (filp = files->fd[fd]) != NULL) { put_unused_fd(fd); FD_CLR(fd, &files->close_on_exec); files->fd[fd] = NULL; error = close_fp(filp); } return error; }
/* Windows will use this function, when sending a certificate signing request. It is not using the Windows API stuff because it needs to arrive on the server side without any junk in it. */ int send_a_file(struct asfd *asfd, const char *path, struct conf **confs) { int ret=0; FILE *fp=NULL; unsigned long long bytes=0; if(!(fp=open_file(path, "rb")) || send_whole_file_gz(asfd, path, "datapth", 0, &bytes, confs, 9 /*compression*/, fp)) { ret=-1; goto end; } logp("Sent %s\n", path); end: close_fp(&fp); return ret; }
static int release_and_move_to_next_in_list(struct dpth *dpth) { int ret=0; struct dpth_lock *next=NULL; // Try to release (and unlink) the lock even if close_fp failed, just // to be tidy. if(close_fp(&dpth->fp)) ret=-1; if(lock_release(dpth->head->lock)) ret=-1; lock_free(&dpth->head->lock); next=dpth->head->next; if(dpth->head==dpth->tail) dpth->tail=next; dpth_lock_free(dpth->head); dpth->head=next; return ret; }
int fzp_close(struct fzp **fzp) { int ret=-1; if(!fzp || !*fzp) return 0; switch((*fzp)->type) { case FZP_FILE: ret=close_fp(&((*fzp)->fp)); break; case FZP_COMPRESSED: ret=close_zp(&((*fzp)->zp)); break; default: unknown_type((*fzp)->type, __func__); break; } fzp_free(fzp); return ret; }
static inline void __exit_files(struct task_struct *tsk) { struct files_struct * files = tsk->files; if (files) { tsk->files = NULL; if (!--files->count) { int i; for (i=0 ; i<NR_OPEN ; i++) { struct file * filp = files->fd[i]; if (!filp) continue; files->fd[i] = NULL; close_fp(filp); } kfree(files); } } }