/* vi:set cindent tabstop=2 shiftwidth=2: */ /* * libvxfs - library for reading Veritas Journaled FileSystem (VxFS) * Copyright (c) 1999 Martin Hinner * * block.c: VxFS block routines and UnixWare vtoc support * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id$ */ #include #include #include #include #include int vxfd; int vxbsize; long vxpartoffset; int vxopendev (char *device) { vxbsize = 1024; vxfd = open (device, O_RDONLY); if (vxfd < 0) { perror (device); return -1; } return 0; } void vxclosedev (void) { if (vxfd >= 0) { close (vxfd); vxfd = -1; vxbsize = 0; } } int vxsetpart (int part) { struct uw_vtoc *vtoc; if (part >= V_NUMPAR) { if (vxverbose) fprintf (stderr, "vxsetpart: Invalid partition %u\n", part); return -1; } vtoc = vxreaduwvtoc (); if (vtoc == NULL) { /* No partition specified, silently ignore */ if (part == 0) return 0; if (vxverbose) fprintf (stderr, "vxsetpart: vxreaduwvtoc returned NULL\n"); return -1; } if (vtoc->part[part].tag == V_UNUSED) { if (vxverbose) fprintf (stderr, "vxsetpart: Attemp to mount unused partition\n"); return -1; } vxpartoffset = vtoc->part[part].start; free (vtoc); return 0; } struct uw_vtoc * vxreaduwvtoc (void) { struct uw_vtoc *vtoc; struct uw_disklabel *disklabel; struct uw_dospt *dospt; int i,npart; dospt = (struct uw_dospt *) malloc(VTOC_BSIZE); if (!dospt) { if (vxverbose) fprintf(stderr,"vxreaduwvtoc: Not enough memory!\n"); return 0; } if (lseek (vxfd, 0 , SEEK_SET) < 0) { if (vxverbose) perror ("vxreaduwvtoc: lseek"); return 0; } if (read (vxfd, dospt, VTOC_BSIZE) != VTOC_BSIZE) { if (vxverbose) perror ("vxreaduwvtoc: read"); return 0; } npart = -1; for (i=0;i<4;i++) { if (dospt->part[i].sys_ind == VTOC_DOSTYPE) npart = i; } if (npart == -1) { if (vxverbose) fprintf(stderr,"vxreaduwvtoc: No UnixWare partition found!\n"); free(dospt); return 0; } disklabel = (struct uw_disklabel *) malloc (VTOC_BSIZE); if (!disklabel) { if (vxverbose) fprintf (stderr, "vxreaduwvtoc: Not enough memory!\n"); return 0; } if (lseek (vxfd, VTOC_BSIZE * (dospt->part[npart].start_sect + VTOC_SECTOR), SEEK_SET) < 0) { if (vxverbose) perror ("vxreaduwvtoc: lseek"); return 0; } if (read (vxfd, disklabel, VTOC_BSIZE) != VTOC_BSIZE) { if (vxverbose) perror ("vxreaduwvtoc: read"); return 0; } if (disklabel->vtoc.sanity != VTOC_SANE) { free (disklabel); return 0; } vtoc = (struct uw_vtoc*)malloc(sizeof(struct uw_vtoc)); if (vtoc == NULL) { if (vxverbose) fprintf (stderr, "vxreaduwvtoc: Not enough memory!\n"); return 0; } memcpy(vtoc, &disklabel->vtoc, sizeof(struct uw_vtoc)); free(disklabel); return vtoc; } void * vxread (int blknum, int count) { void *blk; blk = malloc (vxbsize * count); if (!blk) { if (vxverbose) fprintf (stderr, "vxread(%u): Not enough memory!\n", blknum); return 0; } if (lseek (vxfd, (VTOC_BSIZE * vxpartoffset) + (blknum * vxbsize), SEEK_SET) < 0) { if (vxverbose) perror ("vxread: lseek"); return 0; } if (read (vxfd, blk, vxbsize * count) != vxbsize * count) { if (vxverbose) perror ("vxread: read"); return 0; } return blk; }