getfsent.c

来自「开源备份软件源码 AMANDA, the Advanced Marylan」· C语言 代码 · 共 760 行 · 第 1/2 页

C
760
字号
        && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1) {       dp = typebuf;       ep = fsent->fstype = malloc(strlen(typebuf)+2);       while (*dp)            *ep++ = tolower(*dp++);       *ep=0;    }    if ( mnt.mt_ro_flg == MNT_READONLY ) {	fsent->mntopts = "ro";    } else {	fsent->mntopts = "rw";    }    fsent->freq = 0;    fsent->passno = 0;    return 1;}#      else /* } { */#define GETFSENT_TYPE "undefined"#      endif#    endif#  endif#endif#endif /* } *//* *===================================================================== * Convert either a block or character device name to a character (raw) * device name. * * static char *dev2rdev(const char *name); * * entry:	name - device name to convert * exit:	matching character device name if found, *		otherwise returns the input * * The input must be an absolute path. * * The exit string area is always an alloc-d area that the caller is * responsible for releasing. *===================================================================== */static char *dev2rdev(    char *	name){  char *fname = NULL;  struct stat st;  char *s;  int ch;  if(stat(name, &st) == 0 && !S_ISBLK(st.st_mode)) {    /*     * If the input is already a character device, just return it.     */    return stralloc(name);  }  s = name;  ch = *s++;  if(ch == '\0' || ch != '/') return stralloc(name);  ch = *s++;					/* start after first '/' */  /*   * Break the input path at each '/' and create a new name with an   * 'r' before the right part.  For instance:   *   *   /dev/sd0a -> /dev/rsd0a   *   /dev/dsk/c0t0d0s0 -> /dev/rdsk/c0t0d0s0 -> /dev/dsk/rc0t0d0s0   */  while(ch) {    if (ch == '/') {      s[-1] = '\0';      fname = newvstralloc(fname, name, "/r", s, NULL);      s[-1] = (char)ch;      if(stat(fname, &st) == 0 && S_ISCHR(st.st_mode)) return fname;    }    ch = *s++;  }  amfree(fname);  return stralloc(name);			/* no match */}#ifndef IGNORE_FSTABstatic int samefile(struct stat[3], struct stat *);static intsamefile(    struct stat stats[3],    struct stat *estat){  int i;  for(i = 0; i < 3; ++i) {    if (stats[i].st_dev == estat->st_dev &&	stats[i].st_ino == estat->st_ino)      return 1;  }  return 0;}#endif /* !IGNORE_FSTAB */intsearch_fstab(     char *		name,     generic_fsent_t *	fsent,     int		check_dev){#ifdef IGNORE_FSTAB  /* There is no real mount table so this will always fail and   * we are using GNU tar so we can just return here.   */  (void)name;		/* Quiet unused parameter warning */  (void)fsent;		/* Quiet unused parameter warning */  (void)check_dev;	/* Quiet unused parameter warning */  return 0;#else  struct stat stats[3];  char *fullname = NULL;  char *rdev = NULL;  int rc;  if (!name)    return 0;  memset(stats, 0, SIZEOF(stats));  stats[0].st_dev = stats[1].st_dev = stats[2].st_dev = (dev_t)-1;  if (stat(name, &stats[0]) == -1)    stats[0].st_dev = (dev_t)-1;  if (name[0] != '/') {    fullname = stralloc2(DEV_PREFIX, name);    if (stat(fullname, &stats[1]) == -1)      stats[1].st_dev = (dev_t)-1;    fullname = newstralloc2(fullname, RDEV_PREFIX, name);    if (stat(fullname, &stats[2]) == -1)      stats[2].st_dev = (dev_t)-1;    amfree(fullname);  }  else if (stat((rdev = dev2rdev(name)), &stats[1]) == -1)    stats[1].st_dev = (dev_t)-1;  amfree(rdev);  if (!open_fstab())    return 0;  rc = 0;  while(get_fstab_nextentry(fsent)) {    struct stat mntstat;    struct stat fsstat;    struct stat fsrstat;    int smnt = -1, sfs = -1, sfsr = -1;    amfree(rdev);    if(fsent->mntdir != NULL)       smnt = stat(fsent->mntdir, &mntstat);    if(fsent->fsname != NULL) {      sfs = stat(fsent->fsname, &fsstat);      sfsr = stat((rdev = dev2rdev(fsent->fsname)), &fsrstat);      if(check_dev == 1 && sfs == -1 && sfsr == -1)	continue;    }    if((fsent->mntdir != NULL &&	smnt != -1 &&        samefile(stats, &mntstat)) ||        (fsent->fsname != NULL &&	sfs != -1 &&        samefile(stats, &fsstat)) ||       (fsent->fsname != NULL &&	sfsr != -1 &&        samefile(stats, &fsrstat))) {      rc = 1;      break;    }  }  amfree(rdev);  close_fstab();  return rc;#endif /* !IGNORE_FSTAB */}intis_local_fstype(    generic_fsent_t *	fsent){    if(fsent->fstype == NULL)	/* unknown, assume local */	return 1;    /* just eliminate fstypes known to be remote or unsavable */    return strcmp(fsent->fstype, "nfs") != 0 && /* NFS */	   strcmp(fsent->fstype, "afs") != 0 &&	/* Andrew Filesystem */	   strcmp(fsent->fstype, "swap") != 0 && /* Swap */	   strcmp(fsent->fstype, "iso9660") != 0 && /* CDROM */	   strcmp(fsent->fstype, "hs") != 0 && /* CDROM */	   strcmp(fsent->fstype, "piofs") != 0;	/* an AIX printer thing? */}char *amname_to_devname(    char *	str){    generic_fsent_t fsent;    if(search_fstab(str, &fsent, 1) && fsent.fsname != NULL)	str = fsent.fsname;    else if(search_fstab(str, &fsent, 0) && fsent.fsname != NULL)	str = fsent.fsname;    return dev2rdev(str);}char *amname_to_dirname(    char *	str){    generic_fsent_t fsent;    if(search_fstab(str, &fsent, 1) && fsent.mntdir != NULL)	str = fsent.mntdir;    else if(search_fstab(str, &fsent, 0) && fsent.mntdir != NULL)	str = fsent.mntdir;    return stralloc(str);}char *amname_to_fstype(    char *	str){    generic_fsent_t fsent;    if (!search_fstab(str, &fsent, 1) && !search_fstab(str, &fsent, 0))      return stralloc("");    return stralloc(fsent.fstype);}#ifdef TESTvoid print_entry(generic_fsent_t *fsent);voidprint_entry(    generic_fsent_t *	fsent){#define nchk(s)	((s)? (s) : "<NULL>")    g_printf("%-20.20s %-14.14s %-7.7s %4d %5d %s\n",	   nchk(fsent->fsname), nchk(fsent->mntdir), nchk(fsent->fstype),	   fsent->freq, fsent->passno, nchk(fsent->mntopts));}intmain(    int		argc,    char **	argv){    generic_fsent_t fsent;    char *s;    char *name = NULL;    /*     * Configure program for internationalization:     *   1) Only set the message locale for now.     *   2) Set textdomain for all amanda related programs to "amanda"     *      We don't want to be forced to support dozens of message catalogs.     */      setlocale(LC_MESSAGES, "C");    textdomain("amanda");     safe_fd(-1, 0);    set_pname("getfsent");    dbopen(NULL);    /* Don't die when child closes pipe */    signal(SIGPIPE, SIG_IGN);    if(!open_fstab()) {	g_fprintf(stderr, _("getfsent_test: could not open fstab\n"));	return 1;    }    g_printf("getfsent (%s)\n",GETFSENT_TYPE);    g_printf("l/r fsname               mntdir         fstype  freq pass# mntopts\n");    while(get_fstab_nextentry(&fsent)) {	g_printf("%c  ",is_local_fstype(&fsent)? 'l' : 'r');	print_entry(&fsent);    }    g_printf("--------\n");    close_fstab();    name = newstralloc(name, "/usr");    if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {	g_printf(_("Found %s mount for %s:\n"),	       is_local_fstype(&fsent)? _("local") : _("remote"), name);	print_entry(&fsent);    }    else 	g_printf(_("Mount for %s not found\n"), name);    name = newstralloc(name, "/");    if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {	g_printf(_("Found %s mount for %s:\n"),	       is_local_fstype(&fsent)? _("local") : _("remote"), name);	print_entry(&fsent);    }    else 	g_printf(_("Mount for %s not found\n"), name);    name = newstralloc(name, "/");    s = amname_to_fstype(name);    g_printf(_("fstype of `%s': %s\n"), name, s);    amfree(s);    name = newstralloc(name, "/dev/root");    s = amname_to_fstype(name);    g_printf(_("fstype of `%s': %s\n"), name, s);    amfree(s);    name = newstralloc(name, "/usr");    s = amname_to_fstype(name);    g_printf(_("fstype of `%s': %s\n"), name, s);    amfree(s);    name = newstralloc(name, "c0t3d0s0");    s = amname_to_fstype(name);    g_printf(_("fstype of `%s': %s\n"), name, s);    amfree(s);    name = newstralloc(name, "/tmp/foo");    s = amname_to_devname(name);    g_printf(_("device of `%s': %s\n"), name, s);    amfree(s);    s = amname_to_dirname(name);    g_printf(_("dirname of `%s': %s\n"), name, s);    amfree(s);    s = amname_to_fstype(name);    g_printf(_("fstype of `%s': %s\n"), name, s);    amfree(s);    name = newstralloc(name, "./foo");    s = amname_to_devname(name);    g_printf(_("device of `%s': %s\n"), name, s);    amfree(s);    s = amname_to_dirname(name);    g_printf(_("dirname of `%s': %s\n"), name, s);    amfree(s);    s = amname_to_fstype(name);    g_printf(_("fstype of `%s': %s\n"), name, s);    amfree(s);    while (--argc > 0) {	name = newstralloc(name, *++argv);	s = amname_to_devname(name);	g_printf(_("device of `%s': %s\n"), name, s);	amfree(s);	s = amname_to_dirname(name);	g_printf(_("dirname of `%s': %s\n"), name, s);	amfree(s);	s = amname_to_fstype(name);	g_printf(_("fstype of `%s': %s\n"), name, s);	amfree(s);    }    amfree(name);    return 0;}#endif

⌨️ 快捷键说明

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