⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tapeio.c

📁 开源备份软件源码 AMANDA, the Advanced Maryland Automatic Network Disk Archiver, is a backup system that a
💻 C
📖 第 1 页 / 共 3 页
字号:
    vslot = name2slot(filename, &tname);    return vtable[vslot].xxx_tape_access(tname, mode);}inttape_stat(    char *filename,    struct stat *buf){    char *tname;    int vslot;    vslot = name2slot(filename, &tname);    return vtable[vslot].xxx_tape_stat(tname, buf);}inttape_open(    char *filename,    int mode, ...){    char *tname;    int vslot;    int fd;    mode_t mask;    va_list ap;    va_start(ap, mode);    mask = (mode_t)va_arg(ap, int);    va_end(ap);    vslot = name2slot(filename, &tname);    if((fd = vtable[vslot].xxx_tape_open(tname, mode, mask)) >= 0) {	amtable_alloc((void **)tape_info_p,		      &tape_info_count,		      SIZEOF(*tape_info),		      (size_t)(fd + 1),		      10,		      tape_info_init);	/*	 * It is possible to recurse in the above open call and come	 * back here twice for the same file descriptor.  Set the vtape	 * index only if it is not already set, i.e. the first call wins.	 */	if(tape_info[fd].vtape_index < 0) {	    tape_info[fd].vtape_index = vslot;	}    }    return fd;}inttapefd_close(    int fd){    int	res;    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || ((vslot = tape_info[fd].vtape_index) < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    if((res = vtable[vslot].xxx_tapefd_close(fd)) == 0) {	amfree(tape_info[fd].host);	amfree(tape_info[fd].disk);	amfree(tape_info[fd].datestamp);	amfree(tape_info[fd].tapetype);	memset(tape_info + fd, 0, SIZEOF(*tape_info));        tape_info_init((void *)(tape_info + fd));    }    return res;}inttapefd_can_fork(    int fd){    int	vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    return vtable[vslot].xxx_tapefd_can_fork(fd);}inttapefd_fsf(    int fd,    off_t count){    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    return vtable[vslot].xxx_tapefd_fsf(fd, count);}inttapefd_rewind(    int fd){    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    return vtable[vslot].xxx_tapefd_rewind(fd);}voidtapefd_resetofs(    int fd){    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;				/* not that it matters */	return;    }    vslot = tape_info[fd].vtape_index;    vtable[vslot].xxx_tapefd_resetofs(fd);}inttapefd_unload(    int fd){    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    return vtable[vslot].xxx_tapefd_unload(fd);}inttapefd_status(    int fd,    struct am_mt_status *stat){    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    return vtable[vslot].xxx_tapefd_status(fd, stat);}inttapefd_weof(    int fd,    off_t count){    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    return vtable[vslot].xxx_tapefd_weof(fd, count);} ssize_ttapefd_read(    int fd,    void *buffer,    size_t count){    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    return vtable[vslot].xxx_tapefd_read(fd, buffer, count);}ssize_ttapefd_write(    int fd,    const void *buffer,    size_t count){    int vslot;    if ((fd < 0) || ((size_t)fd >= tape_info_count)       || (tape_info[fd].vtape_index < 0)) {	errno = EBADF;	return -1;    }    vslot = tape_info[fd].vtape_index;    return vtable[vslot].xxx_tapefd_write(fd, buffer, count);}char *tape_rewind(    char *devname){    int fd;    char *r = NULL;    if((fd = tape_open(devname, O_RDONLY)) < 0) {	r = errstr = newvstrallocf(errstr,				  _("tape_rewind: tape open: %s: %s"),				  devname, strerror(errno));    } else if(tapefd_rewind(fd) == -1) {	r = errstr = newvstrallocf(errstr,				  _("tape_rewind: rewinding tape: %s: %s"),				  devname,				  strerror(errno));    }    if(fd >= 0) {	tapefd_close(fd);    }    return r;}char *tape_unload(    char *devname){    int fd;    char *r = NULL;    if((fd = tape_open(devname, O_RDONLY)) < 0) {	r = errstr = newvstrallocf(errstr,				  _("tape_unload: tape open: %s: %s"),				  devname,				  strerror(errno));    } else if(tapefd_unload(fd) == -1) {	r = errstr = newvstrallocf(errstr,				  _("tape_unload: unloading tape: %s: %s"),				  devname,				  strerror(errno));    }    if(fd >= 0) {	tapefd_close(fd);    }    return r;}char *tape_fsf(    char *devname,    off_t count){    int fd;    char *r = NULL;    if((fd = tape_open(devname, O_RDONLY)) < 0) {	r = errstr = newvstrallocf(errstr,				  _("tape_fsf: tape open: %s: %s"),				  devname,				  strerror(errno));    } else if(tapefd_fsf(fd, count) == -1) {	r = errstr = newvstrallocf(errstr,			          plural(_("tape_fsf: fsf %lld file: %s"),			                 _("tape_fsf: fsf %lld files: %s"),				         count),				  (long long)count, strerror(errno));    }    if(fd >= 0) {	tapefd_close(fd);    }    return r;}/* Reads the tape label, like you expect. If failure, returns an error   string. If the tape might not be an Amanda tape, the returned   string will start with NOT_AMANDA_TAPE_MSG. */char *tapefd_rdlabel(    int fd,    char **datestamp,    char **label){    ssize_t rc;    size_t buflen;    char *buffer = NULL;    dumpfile_t file;    char *r = NULL;    amfree(*datestamp);    amfree(*label);    buflen = getconf_readblocksize() * 1024;    buffer = alloc(buflen + 1);    if(tapefd_getinfo_fake_label(fd)) {	*datestamp = stralloc("X");	*label = stralloc(FAKE_LABEL);    } else if(tapefd_rewind(fd) == -1) {	r = vstrallocf(_("rewinding tape: %s"), strerror(errno));    } else if((rc = tapefd_read(fd, buffer, buflen)) == -1) {	r = vstrallocf(_(NOT_AMANDA_TAPE_MSG "(%s)"), strerror(errno));    } else if (rc == 0) {        r = vstrallocf(_(NOT_AMANDA_TAPE_MSG " (Read 0 bytes)"));    } else {	/* make sure buffer is null-terminated */	buffer[rc] = '\0';	parse_file_header(buffer, &file, (size_t)rc);	if(file.type != F_TAPESTART) {	    r = vstrallocf(NOT_AMANDA_TAPE_MSG);	} else {	    *datestamp = stralloc(file.datestamp);	    *label = stralloc(file.name);	}    }    amfree(buffer);    if (r)	errstr = newvstrallocf(errstr, "%s", r);    return r;}char *tape_rdlabel(    char *devname,    char **datestamp,    char **label){    int fd;    char *r = NULL;    if((fd = tape_open(devname, O_RDONLY)) < 0) {	r = vstrallocf(_("tape_rdlabel: tape open: %s: %s"),                      devname,                      strerror(errno));    } else        r = tapefd_rdlabel(fd, datestamp, label);    if(fd >= 0) {        tapefd_close(fd);    }    if (r)	errstr = newvstrallocf(errstr, "%s", r);    return r;}char *tapefd_wrlabel(    int fd,    char *datestamp,    char *label,    size_t size){    ssize_t rc;    char *buffer = NULL;    dumpfile_t file;    char *r = NULL;    if(tapefd_rewind(fd) == -1) {	r = errstr = newvstrallocf(errstr, _("rewinding tape: %s"),			strerror(errno));    } else {	fh_init(&file);	file.type = F_TAPESTART;	strncpy(file.datestamp, datestamp, SIZEOF(file.datestamp) - 1);	file.datestamp[SIZEOF(file.datestamp) - 1] = '\0';	strncpy(file.name, label, SIZEOF(file.name) - 1);	file.name[SIZEOF(file.name) - 1] = '\0';	file.blocksize = size;        buffer = build_header(&file, size);	tapefd_setinfo_host(fd, NULL);	tapefd_setinfo_disk(fd, label);	tapefd_setinfo_level(fd, -1);	if((rc = tapefd_write(fd, buffer, size)) != (ssize_t)size) {	    if (rc != 1) {		r = errstr = newvstrallocf(errstr,				_("writing label: short write"));	    } else {		r = errstr = newvstrallocf(errstr,				_("writing label: %s"), strerror(errno));	    }	}	amfree(buffer);    }    return r;}char *tape_wrlabel(    char *devname,    char *datestamp,    char *label,    size_t size){    int fd;    char *r = NULL;    if((fd = tape_open(devname, O_WRONLY)) < 0) {	if (errno == EACCES) {	    r = errstr = newvstrallocf(errstr,				  _("writing label: tape is write-protected"));	} else {	    r = errstr = newvstrallocf(errstr,				  _("writing label: %s"), strerror(errno));	}    } else if(tapefd_wrlabel(fd, datestamp, label, size) != NULL) {	r = errstr;    }    if(fd >= 0) {	tapefd_close(fd);    }    return r;}char *tapefd_wrendmark(    int fd,    char *datestamp,    size_t size){    ssize_t rc;    char *buffer = NULL;    dumpfile_t file;    char *r = NULL;    fh_init(&file);    file.type = F_TAPEEND;    strncpy(file.datestamp, datestamp, SIZEOF(file.datestamp) - 1);    file.datestamp[SIZEOF(file.datestamp) - 1] = '\0';    file.blocksize = size;    buffer = build_header(&file, size);    tapefd_setinfo_host(fd, NULL);    tapefd_setinfo_disk(fd, "TAPEEND");    tapefd_setinfo_level(fd, -1);    if((rc = tapefd_write(fd, buffer, size)) != (ssize_t)size) {	if (rc != 1) {	    r = errstr = newvstrallocf(errstr,				_("writing endmark: short write"));	} else {	    r = errstr = newvstrallocf(errstr,				_("writing endmark: %s"), strerror(errno));	}    }    amfree(buffer);    return r;}char *tape_wrendmark(    char *devname,    char *datestamp,    size_t size){    int fd;    char *r = NULL;    if((fd = tape_open(devname, O_WRONLY)) < 0) {	if (errno == EACCES) {	    r = errstr = newvstrallocf(errstr,				  _("writing endmark: tape is write-protected"));	} else {	    r = errstr = newvstrallocf(errstr,				  _("writing endmark: %s"), strerror(errno));	}    } else if(tapefd_wrendmark(fd, datestamp, size) != NULL) {	r = errstr;    }    if(fd >= 0) {	tapefd_close(fd);    }    return r;}char *tape_writable(    char *devname){    int fd = -1;    char *r = NULL;    /* first, make sure the file exists and the permissions are right */    if(tape_access(devname, R_OK|W_OK) == -1) {	r = errstr = newvstrallocf(errstr, "%s", strerror(errno));    } else if((fd = tape_open(devname, O_WRONLY)) < 0) {	if (errno == EACCES) {	    r = errstr = newvstrallocf(errstr, _("tape is write-protected"));	} else {	    r = errstr = newvstrallocf(errstr, "%s", strerror(errno));	}    }    if(fd >= 0) {	tapefd_close(fd);    }    return r;}ssize_tgetconf_readblocksize(void){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -