Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 9 Feb 2003 07:47:36 -0800 (PST)
From:      Juli Mallett <jmallett@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 24901 for review
Message-ID:  <200302091547.h19FlabB084754@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=24901

Change 24901 by jmallett@jmallett_dalek on 2003/02/09 07:47:07

	Abstractions and framework to work with the files herein, right now,
	just provide facility to Get them out.  Seems to work for getting the
	sash images out of an inst CD.

Affected files ...

.. //depot/projects/mips/sbin/fxlabel/fxlabel.c#3 edit

Differences ...

==== //depot/projects/mips/sbin/fxlabel/fxlabel.c#3 (text+ko) ====

@@ -39,19 +39,27 @@
 #include <unistd.h>
 
 struct volhdr	label;
+struct volpart	*vhp = &label.vh_part[FX_VOLHDRPART];
 static int	needswap;
+static int	bsize = 512;
 
 /*
- * Boolean returns here indicate true on error.
+ * Boolean returns here indicate true on error.  Some just error to prevent
+ * programmers from shooting off feet.
  */
+static void	addfile(struct volhdr *, int, const char *);
+static void	defrag(struct volhdr *, int);
+static void	deletefile(struct volhdr *, int, const char *);
 static int	dkopen(const char *, int);
 static void	dirlist(struct volhdr *);
+static void	getfile(struct volhdr *, int, const char *);
 static bool	goodlabel(struct volhdr *, const char **);
 static void	newlabel(struct volhdr *);
 static void	printlabel(struct volhdr *);
 static bool	readlabel(int);
 static void	sumlabel(struct volhdr *);
 static void	swaplabel(struct volhdr *);
+static int	startdisk(const char *, int);
 static bool	writelabel(int);
 
 static void	usage(void);
@@ -66,16 +74,26 @@
 int
 main(int argc, char *argv[])
 {
-	const char *disk, *error;
+	const char *aflag, *dflag, *disk, *gflag;
 	bool pflag, wflag;
 	int ch;
 	int fd;
 
+	aflag = dflag = gflag = NULL;
 	pflag = wflag = false;
 	needswap = be16toh(0x1234) != 0x1234;
 
-	while ((ch = getopt(argc, argv, "pw")) != -1) {
+	while ((ch = getopt(argc, argv, "a:d:g:pw")) != -1) {
 		switch (ch) {
+		case 'a':
+			aflag = optarg;
+			break;
+		case 'd':
+			dflag = optarg;
+			break;
+		case 'g':
+			gflag = optarg;
+			break;
 		case 'p':
 			if (wflag)
 				usage();
@@ -94,19 +112,40 @@
 	argc -= optind;
 	argv += optind;
 
-	if (!pflag && !wflag)
+	if (aflag == NULL && dflag == NULL && gflag == NULL && !pflag && !wflag)
 		pflag = true;
 
 	while ((disk = *argv++) != NULL) {
+		if (aflag != NULL) {
+			fd = startdisk(disk, O_RDWR);
+			defrag(&label, fd);
+			addfile(&label, fd, aflag);
+			sumlabel(&label);
+			swaplabel(&label);
+			if (writelabel(fd))
+				err(1, "%s (label write)", disk);
+			close(fd);
+			continue;
+		}
+		if (dflag != NULL) {
+			fd = startdisk(disk, O_RDWR);
+			deletefile(&label, fd, dflag);
+			defrag(&label, fd);
+			sumlabel(&label);
+			swaplabel(&label);
+			if (writelabel(fd))
+				err(1, "%s (label write)", disk);
+			close(fd);
+			continue;
+		}
+		if (gflag != NULL) {
+			fd = startdisk(disk, O_RDONLY);
+			getfile(&label, fd, gflag);
+			close(fd);
+			continue;
+		}
 		if (pflag) {
-			fd = dkopen(disk, O_RDONLY);
-			if (fd == -1)
-				err(1, "%s (open)", disk);
-			if (readlabel(fd))
-				err(1, "%s (label read)", disk);
-			if (goodlabel(&label, &error))
-				errx(1, "%s (label check): %s", disk, error);
-			swaplabel(&label);
+			fd = startdisk(disk, O_RDONLY);
 			printlabel(&label);
 			close(fd);
 			continue;
@@ -128,6 +167,24 @@
 	return (0);
 }
 
+static void
+addfile(struct volhdr *vp, int fd, const char *file)
+{
+	errx(1, "adding of files is unimplemented");
+}
+
+static void
+defrag(struct volhdr *vp, int fd)
+{
+	errx(1, "defragmenting of the free space is unimplemented");
+}
+
+static void
+deletefile(struct volhdr *vp, int fd, const char *file)
+{
+	errx(1, "deletion of files is unimplemented");
+}
+
 static int
 dkopen(const char *disk, int flags)
 {
@@ -160,6 +217,39 @@
 	}
 }
 
+static void
+getfile(struct volhdr *vp, int fd, const char *file)
+{
+	struct voldir *dp;
+	ssize_t cnt;
+	char *buf;
+	int ofd;
+	int i;
+
+	for (i = 0; i < FX_DIRSIZE; i++) {
+		dp = &vp->vh_dir[i];
+		if (!dp->vd_size)
+			continue;
+		if (strncmp(file, dp->vd_name, FX_NAMELEN) != 0)
+			continue;
+		ofd = open(file, O_CREAT | O_WRONLY, 0644);
+		if (ofd == -1)
+			err(1, "%s (file get)", file);
+		buf = malloc(dp->vd_size);
+		if (buf == NULL)
+			errx(1, "malloc failed");
+		cnt = pread(fd, buf, dp->vd_size, dp->vd_addr * bsize);
+		if (cnt != dp->vd_size)
+			err(1, "%s (file read)", file);
+		cnt = write(ofd, buf, dp->vd_size);
+		if (cnt != dp->vd_size)
+			err(1, "%s (file write)", file);
+		close(ofd);
+		return;
+	}
+	errx(1, "could not find %s", file);
+}
+
 static bool
 goodlabel(struct volhdr *vp, const char **errorp)
 {
@@ -240,9 +330,24 @@
 	}
 }
 
-/*
- * This takes a new label which is already converted to big endian.
- */
+static int
+startdisk(const char *disk, int flags)
+{
+	const char *error;
+	int fd;
+
+	fd = dkopen(disk, flags);
+	if (fd == -1)
+		err(1, "%s (open)", disk);
+	if (readlabel(fd))
+		err(1, "%s (label read)", disk);
+	if (goodlabel(&label, &error))
+		errx(1, "%s (label check): %s", disk, error);
+	swaplabel(&label);
+
+	return (fd);
+}
+
 static void
 sumlabel(struct volhdr *vp)
 {
@@ -310,7 +415,10 @@
 usage(void)
 {
 	fprintf(stderr,
-"usage: fxlabel [-p] disk [...]\n"
-"usage: fxlabel [-w] disk [...]\n");
+"usage: fxlabel [-a file] disk [...]\n"
+"       fxlabel [-d file] disk [...]\n"
+"       fxlabel [-g file] disk [...]\n"
+"       fxlabel [-p] disk [...]\n"
+"       fxlabel [-w] disk [...]\n");
 	exit(-1);
 }

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe p4-projects" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200302091547.h19FlabB084754>