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

📄 tapetype.c

📁 开源备份软件源码 AMANDA, the Advanced Maryland Automatic Network Disk Archiver, is a backup system that a
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Amanda, The Advanced Maryland Automatic Network Disk Archiver * Copyright (c) 1991-1998 University of Maryland at College Park * All Rights Reserved. * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name of U.M. not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission.  U.M. makes no representations about the * suitability of this software for any purpose.  It is provided "as is" * without express or implied warranty. * * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Author: James da Silva, Systems Design and Analysis Group *			   Computer Science Department *			   University of Maryland at College Park *//* * $Id: tapetype.c,v 1.27 2006/08/24 01:57:16 paddy_s Exp $ * * tests a tape in a given tape unit and prints a tapetype entry for * it.  */#include "amanda.h"#include "conffile.h"#include "tapeio.h"#define NBLOCKS 32			/* number of random blocks */extern int optind;static char *sProgName;static char *tapedev;static int fd;static size_t blockkb = 32;static size_t blocksize;static char *randombytes = (char *) NULL;#if USE_RAND/* If the C library does not define random(), try to use rand() by   defining USE_RAND, but then make sure you are not using hardware   compression, because the low-order bits of rand() may not be that   random... :-( */#define random() rand()#define srandom(seed) srand(seed)#endifstatic char *getrandombytes(void);static int writeblock(int fd);static off_t writeblocks(int fd, off_t nblks);static void allocrandombytes(void);static void do_pass0(off_t size, time_t *seconds, int dorewind);static void do_pass(off_t size, off_t *blocks, off_t *files, time_t *seconds);static void help(void);static void initnotrandombytes(void);static void initrandombytes(void);static void show_progress(off_t *blocks, off_t *files);static void usage(void);int main(int argc, char **argv);static voidallocrandombytes(void){  size_t i;  size_t j;  size_t page_size;  char   *p;  if (randombytes == (char *)NULL) {#if defined(HAVE_GETPAGESIZE)    page_size = (size_t)getpagesize();#else    page_size = (size_t)1024;#endif    j = (NBLOCKS * blocksize) + page_size;	/* buffer space plus one page */    j = am_round(j, page_size);			/* even number of pages */    p = (char *)alloc(j);    i = (size_t)(p - (char *)0) & (page_size - 1);/* page boundary offset */    if(i != 0) {      randombytes = p + (page_size - i);	/* round up to page boundary */    } else {      randombytes = p;				/* alloc already on boundary */    }  }}static voidinitnotrandombytes(void){  unsigned long i;  unsigned long j;  char *p;  allocrandombytes();  j = NBLOCKS * blocksize;  p = randombytes;  for(i=0; i < j; ++i) {    *p++ = (char) (i % 256);  }}static voidinitrandombytes(void){  unsigned long i, j;  char *p;  allocrandombytes();  j = NBLOCKS * blocksize;  p = randombytes;  for(i=0; i < j; ++i) {    *p++ = (char)random();  }}static char *getrandombytes(void){  static unsigned long counter = 0;  return randombytes + ((counter++ % NBLOCKS) * blocksize);}static int short_write;static intwriteblock(    int		fd){  ssize_t w;  if ((w = tapefd_write(fd, getrandombytes(), blocksize)) == (ssize_t)blocksize) {    return 1;  }  if (w >= 0) {    short_write = 1;  } else {    short_write = 0;  }  return 0;}/* returns number of blocks actually written */static off_twriteblocks(    int		fd,    off_t	nblks){  off_t blks = (off_t)0;  while (blks < nblks) {    if (! writeblock(fd)) {      return (off_t)0;    }    blks += (off_t)1;  }  return blks;}static voidusage(void){  fputs(_("usage: "), stderr);  fputs(sProgName, stderr);  fputs(_(" [-h]"), stderr);  fputs(_(" [-c]"), stderr);  fputs(_(" [-o]"), stderr);  fputs(_(" [-b blocksize]"), stderr);  fputs(_(" -e estsize"), stderr);  fputs(_(" [-f tapedev]"), stderr);  fputs(_(" [-t typename]"), stderr);  fputc('\n', stderr);}static voidhelp(void){  usage();  fputs(_("-h			display this message\n"  	  "-c			run hardware compression detection test only\n"  	  "-o			overwrite amanda tape\n"  	  "-b blocksize		record block size (default: 32k)\n"  	  "-e estsize		estimated tape size (No default!)\n"  	  "-f tapedev		tape device name (default: $TAPE)\n"  	  "-t typename		tapetype name (default: unknown-tapetype)\n"  	  "\n"	  "Note: disable hardware compression when running this program.\n"),	stderr);}int do_tty;static voidshow_progress(    off_t *	blocks,    off_t *	files){    g_fprintf(stderr,	    plural(_("wrote %lld %zu Kb block"),		   _("wrote %lld %zu Kb blocks"),		   *blocks),	    (long long)*blocks,	    blockkb);    g_fprintf(stderr,	    plural(_(" in %lld file"),		   _(" in %lld files"),		   *files),	    (long long)*files);}static voiddo_pass(    off_t	size,    off_t *	blocks,    off_t *	files,    time_t *	seconds){  off_t blks;  time_t start, end;  int save_errno;  if (tapefd_rewind(fd) == -1) {    g_fprintf(stderr, _("%s: could not rewind %s: %s\n"),	    sProgName, tapedev, strerror(errno));    exit(1);  }  if (((-1 == tapefd_close(fd)) ||       (-1 == (fd = tape_open(tapedev, O_RDWR))))) {    g_fprintf(stderr, "%s: could not re-open %s: %s\n",	    sProgName, tapedev, strerror(errno));    exit(1);  }  time(&start);  while(1) {    if ((blks = writeblocks(fd, size)) <= (off_t)0 || tapefd_weof(fd, (off_t)1) != 0)      break;    *blocks += blks;    *files += (off_t)1;    if(do_tty) {      putc('\r', stderr);      show_progress(blocks, files);    }  }  save_errno = errno;  time(&end);  if (*blocks == (off_t)0) {    g_fprintf(stderr, _("%s: could not write any data in this pass: %s\n"),	    sProgName, short_write ? _("short write") : strerror(save_errno));    exit(1);  }  if(end <= start) {    /*     * Just in case time warped backward or the device is really, really     * fast (e.g. /dev/null testing).     */    *seconds = 1;  } else {    *seconds = end - start;  }  if(do_tty) {    putc('\r', stderr);  }  show_progress(blocks, files);  g_fprintf(stderr,	  plural(_(" in %jd second (%s)\n"),		 _(" in %jd seconds (%s)\n"),	  	 *seconds),	  (intmax_t)*seconds,	  short_write ? _("short write") : strerror(save_errno));}static voiddo_pass0(    off_t	size,    time_t *	seconds,    int		dorewind){  off_t blks;  time_t start, end;  int save_errno;  if (dorewind  &&  tapefd_rewind(fd) == -1) {    g_fprintf(stderr, _("%s: could not rewind %s: %s\n"),	    sProgName, tapedev, strerror(errno));    exit(1);  }  if (dorewind &&      ((-1 == tapefd_close(fd)) ||       (-1 == (fd = tape_open(tapedev, O_RDWR))))) {    g_fprintf(stderr, "%s: could not re-open %s: %s\n",	    sProgName, tapedev, strerror(errno));    exit(1);  }  time(&start);  blks = writeblocks(fd, size);  tapefd_weof(fd, (off_t)1);  save_errno = errno;  time(&end);  if (blks <= (off_t)0) {    g_fprintf(stderr, _("%s: could not write any data in this pass: %s\n"),	    sProgName, short_write ? _("short write") : strerror(save_errno));    exit(1);  }  if(end <= start) {    /*     * Just in case time warped backward or the device is really, really     * fast (e.g. /dev/null testing).     */    *seconds = 1;  } else {    *seconds = end - start;  }}intmain(    int		argc,    char **	argv){  off_t pass1blocks = (off_t)0;  off_t pass2blocks = (off_t)0;  time_t pass1time;  time_t pass2time;  time_t timediff;  off_t pass1files = (off_t)0;  off_t pass2files = (off_t)0;  off_t estsize;  off_t pass0size;  off_t pass1size;  off_t pass2size;

⌨️ 快捷键说明

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