raw-posix: add a raw_open_common helper

raw_open and hdev_open contain the same basic logic.  Add a new
raw_open_common helper containing the guts of the open routine
and call it from raw_open and hdev_open.

We use the new open_flags field in BDRVRawState to allow passing
additional open flags to raw_open_common from both.

Signed-off-by: Christoph Hellwig <hch@lst.de>
This commit is contained in:
Christoph Hellwig 2009-06-15 13:53:38 +02:00 committed by Christoph Hellwig
parent 0e1d8f4c54
commit 90babde0ca

View File

@ -124,7 +124,8 @@ static int cd_open(BlockDriverState *bs);
static int raw_is_inserted(BlockDriverState *bs); static int raw_is_inserted(BlockDriverState *bs);
static int raw_open(BlockDriverState *bs, const char *filename, int flags) static int raw_open_common(BlockDriverState *bs, const char *filename,
int flags)
{ {
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
int fd, ret; int fd, ret;
@ -140,8 +141,6 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
s->open_flags |= O_RDONLY; s->open_flags |= O_RDONLY;
bs->read_only = 1; bs->read_only = 1;
} }
if (flags & BDRV_O_CREAT)
s->open_flags |= O_CREAT | O_TRUNC;
/* Use O_DSYNC for write-through caching, no flags for write-back caching, /* Use O_DSYNC for write-through caching, no flags for write-back caching,
* and O_DIRECT for no caching. */ * and O_DIRECT for no caching. */
@ -150,8 +149,7 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
else if (!(flags & BDRV_O_CACHE_WB)) else if (!(flags & BDRV_O_CACHE_WB))
s->open_flags |= O_DSYNC; s->open_flags |= O_DSYNC;
s->type = FTYPE_FILE; s->fd = -1;
fd = open(filename, s->open_flags, 0644); fd = open(filename, s->open_flags, 0644);
if (fd < 0) { if (fd < 0) {
ret = -errno; ret = -errno;
@ -172,6 +170,17 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
return 0; return 0;
} }
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
{
BDRVRawState *s = bs->opaque;
s->type = FTYPE_FILE;
if (flags & BDRV_O_CREAT)
s->open_flags |= O_CREAT | O_TRUNC;
return raw_open_common(bs, filename, flags);
}
/* XXX: use host sector size if necessary with: /* XXX: use host sector size if necessary with:
#ifdef DIOCGSECTORSIZE #ifdef DIOCGSECTORSIZE
{ {
@ -949,9 +958,7 @@ kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex ma
static int hdev_open(BlockDriverState *bs, const char *filename, int flags) static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
{ {
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
int fd, ret; int ret;
posix_aio_init();
#ifdef CONFIG_COCOA #ifdef CONFIG_COCOA
if (strstart(filename, "/dev/cdrom", NULL)) { if (strstart(filename, "/dev/cdrom", NULL)) {
@ -979,19 +986,6 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
IOObjectRelease( mediaIterator ); IOObjectRelease( mediaIterator );
} }
#endif #endif
s->open_flags |= O_BINARY;
if ((flags & BDRV_O_ACCESS) == O_RDWR) {
s->open_flags |= O_RDWR;
} else {
s->open_flags |= O_RDONLY;
bs->read_only = 1;
}
/* Use O_DSYNC for write-through caching, no flags for write-back caching,
* and O_DIRECT for no caching. */
if ((flags & BDRV_O_NOCACHE))
s->open_flags |= O_DIRECT;
else if (!(flags & BDRV_O_CACHE_WB))
s->open_flags |= O_DSYNC;
s->type = FTYPE_FILE; s->type = FTYPE_FILE;
#if defined(__linux__) #if defined(__linux__)
@ -1015,15 +1009,11 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
s->type = FTYPE_CD; s->type = FTYPE_CD;
} }
#endif #endif
s->fd = -1;
fd = open(filename, s->open_flags, 0644); ret = raw_open_common(bs, filename, flags);
if (fd < 0) { if (ret)
ret = -errno;
if (ret == -EROFS)
ret = -EACCES;
return ret; return ret;
}
s->fd = fd;
#if defined(__FreeBSD__) #if defined(__FreeBSD__)
/* make sure the door isnt locked at this time */ /* make sure the door isnt locked at this time */
if (s->type == FTYPE_CD) if (s->type == FTYPE_CD)