summaryrefslogtreecommitdiff
path: root/sbin/pdisk
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2016-01-18 17:57:36 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2016-01-18 17:57:36 +0000
commit1f0673e8309d3a4c43d6de1aebb4e122998706b6 (patch)
tree1d9f442c27b9c6e4e48c97ddcddb76d5f7bc1921 /sbin/pdisk
parentbadd3ba1cb98180755713addf8b0b99346ea7b74 (diff)
struct file_media had 1 field left. And one place where struct
file_media was actually stored. So move the last field (fd) into the place formerly used to store pointer to the instance. As a result we can just pass fd's around rather than pointers to a struct containing a fd. close_file_media() becomes empty but for a close(). So just use close() and nuke close_file_media().
Diffstat (limited to 'sbin/pdisk')
-rw-r--r--sbin/pdisk/dump.c10
-rw-r--r--sbin/pdisk/file_media.c43
-rw-r--r--sbin/pdisk/file_media.h13
-rw-r--r--sbin/pdisk/partition_map.c39
-rw-r--r--sbin/pdisk/partition_map.h4
-rw-r--r--sbin/pdisk/pdisk.c15
-rw-r--r--sbin/pdisk/validate.c17
7 files changed, 54 insertions, 87 deletions
diff --git a/sbin/pdisk/dump.c b/sbin/pdisk/dump.c
index 71c32cbd314..db4aa4361e9 100644
--- a/sbin/pdisk/dump.c
+++ b/sbin/pdisk/dump.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dump.c,v 1.35 2016/01/18 15:17:22 krw Exp $ */
+/* $OpenBSD: dump.c,v 1.36 2016/01/18 17:57:35 krw Exp $ */
/*
* dump.c - dumping partition maps
@@ -481,14 +481,12 @@ display_patches(struct partition_map * entry)
{
static unsigned char *patch_block;
struct patchdescriptor *q;
- struct file_media *m;
struct patchlist *p;
unsigned char *next, *s;
long long offset;
int i;
offset = entry->data->dpme_pblock_start;
- m = entry->the_map->m;
offset = ((long long) entry->data->dpme_pblock_start) *
entry->the_map->logical_block;
if (patch_block == NULL) {
@@ -498,7 +496,7 @@ display_patches(struct partition_map * entry)
return;
}
}
- if (read_file_media(m, (long long) offset, DEV_BSIZE,
+ if (read_file_media(entry->the_map->fd, (long long) offset, DEV_BSIZE,
(char *) patch_block) == 0) {
warn("Can't read patch block");
return;
@@ -516,8 +514,8 @@ display_patches(struct partition_map * entry)
while (i > 0) {
s -= DEV_BSIZE;
i -= 1;
- if (read_file_media(m, offset + i, DEV_BSIZE,
- (char *)s) == 0) {
+ if (read_file_media(entry->the_map->fd, offset + i,
+ DEV_BSIZE, (char *)s) == 0) {
warn("Can't read patch block %d", i);
return;
}
diff --git a/sbin/pdisk/file_media.c b/sbin/pdisk/file_media.c
index 8166d60ca12..1535427754b 100644
--- a/sbin/pdisk/file_media.c
+++ b/sbin/pdisk/file_media.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file_media.c,v 1.34 2016/01/18 16:41:41 krw Exp $ */
+/* $OpenBSD: file_media.c,v 1.35 2016/01/18 17:57:35 krw Exp $ */
/*
* file_media.c -
@@ -66,38 +66,26 @@ compute_block_size(int fd, char *name)
}
-struct file_media *
+int
open_file_as_media(char *file, int oflag)
{
- struct stat info;
- struct file_media *a;
int fd;
- a = 0;
fd = opendev(file, oflag, OPENDEV_PART, NULL);
- if (fd >= 0) {
- a = malloc(sizeof(struct file_media));
- if (a != 0) {
- compute_block_size(fd, file);
- a->fd = fd;
- if (fstat(fd, &info) < 0) {
- warn("can't stat file '%s'", file);
- }
- } else {
- close(fd);
- }
- }
- return (a);
+ if (fd >= 0)
+ compute_block_size(fd, file);
+
+ return (fd);
}
long
-read_file_media(struct file_media *a, long long offset, unsigned long count,
+read_file_media(int fd, long long offset, unsigned long count,
void *address)
{
ssize_t off;
- off = pread(a->fd, address, count, offset);
+ off = pread(fd, address, count, offset);
if (off == count)
return (1);
@@ -113,26 +101,15 @@ read_file_media(struct file_media *a, long long offset, unsigned long count,
long
-write_file_media(struct file_media *a, long long offset, unsigned long count,
+write_file_media(int fd, long long offset, unsigned long count,
void *address)
{
ssize_t off;
- off = pwrite(a->fd, address, count, offset);
+ off = pwrite(fd, address, count, offset);
if (off == count)
return (1);
warn("writing to file failed");
return (0);
}
-
-
-long
-close_file_media(struct file_media * a)
-{
- if (a == 0) {
- return 0;
- }
- close(a->fd);
- return 1;
-}
diff --git a/sbin/pdisk/file_media.h b/sbin/pdisk/file_media.h
index 145a930da99..f0790442ecc 100644
--- a/sbin/pdisk/file_media.h
+++ b/sbin/pdisk/file_media.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: file_media.h,v 1.12 2016/01/18 16:41:41 krw Exp $ */
+/* $OpenBSD: file_media.h,v 1.13 2016/01/18 17:57:35 krw Exp $ */
/*
* file_media.h -
@@ -31,13 +31,8 @@
#define __file_media__
-struct file_media {
- int fd;
-};
-
-struct file_media *open_file_as_media(char *, int);
-long read_file_media(struct file_media *, long long, unsigned long, void *);
-long write_file_media(struct file_media *, long long, unsigned long, void *);
-long close_file_media(struct file_media *m);
+int open_file_as_media(char *, int);
+long read_file_media(int, long long, unsigned long, void *);
+long write_file_media(int, long long, unsigned long, void *);
#endif /* __file_media__ */
diff --git a/sbin/pdisk/partition_map.c b/sbin/pdisk/partition_map.c
index dafc0483199..23089801e96 100644
--- a/sbin/pdisk/partition_map.c
+++ b/sbin/pdisk/partition_map.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: partition_map.c,v 1.39 2016/01/18 15:30:00 krw Exp $ */
+/* $OpenBSD: partition_map.c,v 1.40 2016/01/18 17:57:35 krw Exp $ */
/*
* partition_map.c - partition map routines
@@ -91,13 +91,12 @@ struct partition_map_header *
open_partition_map(char *name, int *valid_file)
{
struct partition_map_header *map;
- struct file_media *m;
- int writable;
+ int fd, writable;
- m = open_file_as_media(name, (rflag) ? O_RDONLY : O_RDWR);
- if (m == 0) {
- m = open_file_as_media(name, O_RDONLY);
- if (m == 0) {
+ fd = open_file_as_media(name, (rflag) ? O_RDONLY : O_RDWR);
+ if (fd == -1) {
+ fd = open_file_as_media(name, O_RDONLY);
+ if (fd == -1) {
warn("can't open file '%s'", name);
*valid_file = 0;
return NULL;
@@ -112,7 +111,7 @@ open_partition_map(char *name, int *valid_file)
map = malloc(sizeof(struct partition_map_header));
if (map == NULL) {
warn("can't allocate memory for open partition map");
- close_file_media(m);
+ close(fd);
return NULL;
}
map->name = name;
@@ -123,14 +122,14 @@ open_partition_map(char *name, int *valid_file)
map->base_order = NULL;
map->physical_block = DEV_BSIZE; /* preflight */
- map->m = m;
+ map->fd = fd;
map->misc = malloc(DEV_BSIZE);
if (map->misc == NULL) {
warn("can't allocate memory for block zero buffer");
- close_file_media(map->m);
+ close(map->fd);
free(map);
return NULL;
- } else if (read_file_media(map->m, (long long) 0, DEV_BSIZE,
+ } else if (read_file_media(map->fd, (long long) 0, DEV_BSIZE,
(char *) map->misc) == 0 ||
convert_block0(map->misc, 1) ||
coerce_block0(map)) {
@@ -179,7 +178,7 @@ close_partition_map(struct partition_map_header * map)
free(entry->data);
free(entry);
}
- close_file_media(map->m);
+ close(map->fd);
free(map);
}
@@ -265,11 +264,9 @@ void
write_partition_map(struct partition_map_header * map)
{
struct partition_map *entry;
- struct file_media *m;
char *block;
int i = 0, result = 0;
- m = map->m;
if (map->misc != NULL) {
convert_block0(map->misc, 0);
result = write_block(map, 0, (char *) map->misc);
@@ -357,13 +354,13 @@ struct partition_map_header *
create_partition_map(char *name, struct partition_map_header * oldmap)
{
struct partition_map_header *map;
- struct file_media *m;
struct dpme *data;
unsigned long number;
long size;
+ int fd;
- m = open_file_as_media(name, (rflag) ? O_RDONLY : O_RDWR);
- if (m == 0) {
+ fd = open_file_as_media(name, (rflag) ? O_RDONLY : O_RDWR);
+ if (fd == -1) {
warn("can't open file '%s' for %sing", name, (rflag) ?
"read" : "writ");
return NULL;
@@ -371,7 +368,7 @@ create_partition_map(char *name, struct partition_map_header * oldmap)
map = malloc(sizeof(struct partition_map_header));
if (map == NULL) {
warn("can't allocate memory for open partition map");
- close_file_media(m);
+ close(fd);
return NULL;
}
map->name = name;
@@ -385,7 +382,7 @@ create_partition_map(char *name, struct partition_map_header * oldmap)
} else {
size = DEV_BSIZE;
}
- map->m = m;
+ map->fd = fd;
if (map->physical_block > MAXIOSIZE) {
map->physical_block = MAXIOSIZE;
}
@@ -1178,7 +1175,7 @@ remove_driver(struct partition_map * entry)
int
read_block(struct partition_map_header * map, unsigned long num, char *buf)
{
- return read_file_media(map->m, ((long long) num) * map->logical_block,
+ return read_file_media(map->fd, ((long long) num) * map->logical_block,
DEV_BSIZE, (void *) buf);
}
@@ -1186,6 +1183,6 @@ read_block(struct partition_map_header * map, unsigned long num, char *buf)
int
write_block(struct partition_map_header * map, unsigned long num, char *buf)
{
- return write_file_media(map->m, ((long long) num) * map->logical_block,
+ return write_file_media(map->fd, ((long long) num) * map->logical_block,
DEV_BSIZE, (void *) buf);
}
diff --git a/sbin/pdisk/partition_map.h b/sbin/pdisk/partition_map.h
index 5706d0bd4fd..d8d86b1b6cb 100644
--- a/sbin/pdisk/partition_map.h
+++ b/sbin/pdisk/partition_map.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: partition_map.h,v 1.18 2016/01/18 00:04:36 krw Exp $ */
+/* $OpenBSD: partition_map.h,v 1.19 2016/01/18 17:57:35 krw Exp $ */
/*
* partition_map.h - partition map routines
@@ -34,11 +34,11 @@
#include "file_media.h"
struct partition_map_header {
- struct file_media *m;
char *name;
struct partition_map * disk_order;
struct partition_map * base_order;
struct block0 *misc;
+ int fd;
int writable;
int changed;
int written;
diff --git a/sbin/pdisk/pdisk.c b/sbin/pdisk/pdisk.c
index 6fd98ba2f96..7560e574d7d 100644
--- a/sbin/pdisk/pdisk.c
+++ b/sbin/pdisk/pdisk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pdisk.c,v 1.47 2016/01/18 02:16:06 krw Exp $ */
+/* $OpenBSD: pdisk.c,v 1.48 2016/01/18 17:57:35 krw Exp $ */
/*
* pdisk - an editor for Apple format partition tables
@@ -628,17 +628,16 @@ do_change_map_size(struct partition_map_header * map)
void
do_display_block(struct partition_map_header * map, char *alt_name)
{
- struct file_media *m;
char *name;
long number;
- int g;
+ int fd, g;
static unsigned char *display_block;
static long next_number = -1;
static int display_g;
if (map != NULL) {
name = 0;
- m = map->m;
+ fd = map->fd;
g = map->logical_block;
} else {
if (alt_name == 0) {
@@ -653,8 +652,8 @@ do_display_block(struct partition_map_header * map, char *alt_name)
return;
}
}
- m = open_file_as_media(name, O_RDONLY);
- if (m == 0) {
+ fd = open_file_as_media(name, O_RDONLY);
+ if (fd == -1) {
warn("can't open file '%s'", name);
free(name);
return;
@@ -677,7 +676,7 @@ do_display_block(struct partition_map_header * map, char *alt_name)
}
display_g = g;
}
- if (read_file_media(m, ((long long) number) * g, g,
+ if (read_file_media(fd, ((long long) number) * g, g,
(char *)display_block) != 0) {
printf("block %ld -", number);
dump_block((unsigned char *)display_block, g);
@@ -685,7 +684,7 @@ do_display_block(struct partition_map_header * map, char *alt_name)
}
xit:
if (name) {
- close_file_media(m);
+ close(fd);
free(name);
}
return;
diff --git a/sbin/pdisk/validate.c b/sbin/pdisk/validate.c
index ab7b4f6e362..d80964cbda8 100644
--- a/sbin/pdisk/validate.c
+++ b/sbin/pdisk/validate.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: validate.c,v 1.26 2016/01/18 02:24:02 krw Exp $ */
+/* $OpenBSD: validate.c,v 1.27 2016/01/18 17:57:35 krw Exp $ */
/*
* validate.c -
@@ -36,6 +36,7 @@
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
+#include <unistd.h>
#include "validate.h"
#include "convert.h"
@@ -61,7 +62,7 @@ static char *buffer;
static struct block0 *b0;
static struct dpme *mb;
static struct partition_map_header *the_map;
-static struct file_media *the_media;
+static int the_fd;
static int g;
int get_block_zero(void);
@@ -81,7 +82,7 @@ get_block_zero(void)
b0 = the_map->misc;
rtn_value = 1;
} else {
- if (read_file_media(the_media, (long long) 0, DEV_BSIZE,
+ if (read_file_media(the_fd, (long long) 0, DEV_BSIZE,
buffer) == 0) {
rtn_value = 0;
} else {
@@ -109,7 +110,7 @@ get_block_n(int n)
rtn_value = 0;
}
} else {
- if (read_file_media(the_media, ((long long) n) * g, DEV_BSIZE, (void *) buffer) == 0) {
+ if (read_file_media(the_fd, ((long long) n) * g, DEV_BSIZE, (void *) buffer) == 0) {
rtn_value = 0;
} else {
mb = (struct dpme *) buffer;
@@ -304,13 +305,13 @@ validate_map(struct partition_map_header * map)
uint32_t limit;
if (map == NULL) {
- the_map = 0;
+ the_map = NULL;
if (get_string_argument("Name of device: ", &name, 1) == 0) {
bad_input("Bad name");
return;
}
- the_media = open_file_as_media(name, O_RDONLY);
- if (the_media == 0) {
+ the_fd = open_file_as_media(name, O_RDONLY);
+ if (the_fd == -1) {
warn("can't open file '%s'", name);
free(name);
return;
@@ -443,7 +444,7 @@ post_processing:
done:
if (map == NULL) {
- close_file_media(the_media);
+ close(the_fd);
free(buffer);
free(name);
}