Nuke dumb error reporting code, people can just use disk::d_error. Unify the

DEBUG and d_error initialisation into an ERROR macro, which can both trace and
set the d_error field.  Much a more meaningful thing, I should say.
This commit is contained in:
Juli Mallett 2003-01-18 04:22:14 +00:00
parent cbd866f411
commit 49b2a6863b
7 changed files with 45 additions and 104 deletions

View File

@ -1,7 +1,7 @@
# $FreeBSD$
LIB= ufs
SRCS= block.c error.c inode.c sblock.c type.c
SRCS= block.c inode.c sblock.c type.c
INCS= libufs.h
CFLAGS+= -I${.CURDIR} -D_LIBUFS
.if defined(LIBUFS_DEBUG)

View File

@ -50,7 +50,7 @@ bread(struct uufsd *disk, ufs2_daddr_t blockno, void *data, size_t size)
char *buf;
ssize_t cnt;
DEBUG(NULL);
ERROR(disk, NULL);
/*
* For when we need to work with the data as a buffer.
@ -62,8 +62,7 @@ bread(struct uufsd *disk, ufs2_daddr_t blockno, void *data, size_t size)
* In case of failure, zero data, which must be fs_bsize.
*/
if (cnt != size) {
DEBUG("short read");
disk->d_error = "short read from block device";
ERROR(disk, "short read from block device");
for (cnt = 0; cnt < disk->d_fs.fs_bsize; cnt++)
buf[cnt] = 0;
return -1;
@ -76,12 +75,11 @@ bwrite(struct uufsd *disk, ufs2_daddr_t blockno, const void *data, size_t size)
{
ssize_t cnt;
DEBUG(NULL);
ERROR(disk, NULL);
cnt = pwrite(disk->d_fd, data, size, (off_t)(blockno * disk->d_bsize));
if (cnt != size) {
DEBUG("short write");
disk->d_error = "short write to block device";
ERROR(disk, "short write to block device");
return -1;
}

View File

@ -1,62 +0,0 @@
/*
* Copyright (c) 2002 Juli Mallett. All rights reserved.
*
* This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
* FreeBSD project. Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/disklabel.h>
#include <sys/stat.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>
#include <errno.h>
#include <stdio.h>
#include <libufs.h>
void
libufs_printerror(struct uufsd *disk)
{
if (disk == NULL) {
fprintf(stderr, "no disk\n");
return;
}
if (disk->d_error != NULL) {
fprintf(stderr, "disk error: %s", disk->d_error);
/*
* XXX
* Should there be a per-disk errno?
*/
if (errno)
fprintf(stderr, ": %s", strerror(errno));
fprintf(stderr, "\n");
}
}

View File

@ -55,7 +55,7 @@ getino(struct uufsd *disk, void **dino, ino_t inode, int *mode)
struct ufs2_dinode *dp2;
struct fs *fs;
DEBUG(NULL);
ERROR(disk, NULL);
fs = &disk->d_fs;
inoblock = disk->d_inoblock;
@ -65,7 +65,7 @@ getino(struct uufsd *disk, void **dino, ino_t inode, int *mode)
if (inoblock == NULL) {
inoblock = malloc(fs->fs_bsize);
if (inoblock == NULL) {
DEBUG(NULL);
ERROR(disk, "unable to allocate inode block");
return -1;
}
disk->d_inoblock = inoblock;
@ -90,6 +90,6 @@ gotit: switch (disk->d_ufs) {
default:
break;
}
DEBUG("unknown UFS filesystem");
ERROR(disk, "unknown UFS filesystem type");
return -1;
}

View File

@ -38,7 +38,7 @@
/*
* Trace steps through libufs, to be used at entry and erroneous return.
*/
#define DEBUG(str) \
#define ERROR(uufsd, str) \
do { \
fprintf(stderr, "libufs in %s", __func__); \
if (str != NULL) \
@ -46,9 +46,15 @@ do { \
if (errno) \
fprintf(stderr, ": %s", strerror(errno)); \
fprintf(stderr, "\n"); \
if ((uufsd) != NULL) \
(uufsd)->d_error = str; \
} while (0)
#else /* _LIBUFS_DEBUGGING */
#define DEBUG(str) /* nil */
#define DEBUG(uufsd, str) \
do { \
if ((uufsd) != NULL) \
(uufsd)->d_error = str; \
} while (0)
#endif /* _LIBUFS_DEBUGGING */
#endif /* _LIBUFS */
@ -90,11 +96,6 @@ __BEGIN_DECLS
ssize_t bread(struct uufsd *, ufs2_daddr_t, void *, size_t);
ssize_t bwrite(struct uufsd *, ufs2_daddr_t, const void *, size_t);
/*
* error.c
*/
void libufs_printerror(struct uufsd *);
/*
* inode.c
*/

View File

@ -53,15 +53,14 @@ sbread(struct uufsd *disk)
struct fs *fs;
int sb, superblock;
DEBUG(NULL);
ERROR(disk, NULL);
fs = &disk->d_fs;
superblock = superblocks[0];
for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
if (bread(disk, superblock, disk->d_sb, SBLOCKSIZE) == -1) {
disk->d_error = "non-existent or truncated superblock";
DEBUG(NULL);
ERROR(disk, "non-existent or truncated superblock");
return -1;
}
if (fs->fs_magic == FS_UFS1_MAGIC)
@ -82,8 +81,7 @@ sbread(struct uufsd *disk)
* must set it to indicate no superblock could be found with
* which to associate this disk/filesystem.
*/
DEBUG("no superblock found");
disk->d_error = "no superblock found";
ERROR(disk, "no usable known superblock found");
errno = ENOENT;
return -1;
}
@ -98,28 +96,25 @@ sbwrite(struct uufsd *disk, int all)
struct fs *fs;
int i, rofd;
DEBUG(NULL);
ERROR(disk, NULL);
fs = &disk->d_fs;
rofd = disk->d_fd;
disk->d_fd = open(disk->d_name, O_WRONLY);
if (disk->d_fd < 0) {
DEBUG("open");
disk->d_error = "failed to open disk";
ERROR(disk, "failed to open disk");
return -1;
}
if (bwrite(disk, disk->d_sblock, fs, SBLOCKSIZE) == -1) {
DEBUG(NULL);
disk->d_error = "failed to write superblock";
ERROR(disk, "failed to write superblock");
return -1;
}
if (all) {
for (i = 0; i < fs->fs_ncg; i++)
if (bwrite(disk, fsbtodb(fs, cgsblock(fs, i)),
fs, SBLOCKSIZE) == -1) {
DEBUG(NULL);
disk->d_error = "failed to update a superblock";
ERROR(disk, "failed to update a superblock");
return -1;
}
}

View File

@ -51,16 +51,18 @@ ufs_disk_ctor(const char *name)
{
struct uufsd *new;
DEBUG(NULL);
new = NULL;
ERROR(new, NULL);
new = malloc(sizeof(*new));
if (new == NULL) {
DEBUG(NULL);
ERROR(new, "unable to allocate memory for disk");
return NULL;
}
if (ufs_disk_fillout(new, name) == -1) {
DEBUG(NULL);
ERROR(new, "could not fill out disk");
free(new);
return NULL;
}
@ -69,18 +71,26 @@ ufs_disk_ctor(const char *name)
}
void
ufs_disk_dtor(struct uufsd **disk)
ufs_disk_dtor(struct uufsd **diskp)
{
DEBUG(NULL);
ufs_disk_close(*disk);
free(*disk);
*disk = NULL;
struct uufsd *disk;
if (diskp != NULL)
disk = *diskp;
else
return;
ERROR(disk, NULL);
ufs_disk_close(disk);
free(disk);
*diskp = NULL;
}
int
ufs_disk_close(struct uufsd *disk)
{
DEBUG(NULL);
ERROR(disk, NULL);
close(disk->d_fd);
if (disk->d_inoblock != NULL) {
free(disk->d_inoblock);
@ -94,12 +104,11 @@ ufs_disk_fillout(struct uufsd *disk, const char *name)
{
int fd;
DEBUG(NULL);
ERROR(disk, NULL);
fd = open(name, O_RDONLY);
if (fd == -1) {
DEBUG("open");
disk->d_error = "failed to open disk for reading";
ERROR(disk, "failed to open disk for reading");
return -1;
}
@ -113,7 +122,7 @@ ufs_disk_fillout(struct uufsd *disk, const char *name)
disk->d_error = NULL;
if (sbread(disk) == -1) {
DEBUG(NULL);
ERROR(disk, "could not read superblock to fill out disk");
return -1;
}