Skip to content

Commit 7d0be8d

Browse files
committed
ptio: Improve ptio_open_dev()
Instead of always passing O_RDWR to ptio_open_dev(), pass the transfer direction as the second argument and automatically decide on the open mode based on that direction, that is, open the device read-write if data is to be transfered to the device and read-only otherwise. Signed-off-by: Damien Le Moal <[email protected]>
1 parent e7a5890 commit 7d0be8d

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/ptio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int ptio_revalidate(struct ptio_dev *dev)
5959
}
6060

6161
static int ptio_exec(struct ptio_dev *dev, char *cdb_str,
62-
enum ptio_cdb_type cdb_type, int dxfer,
62+
enum ptio_cdb_type cdb_type, enum ptio_dxfer dxfer,
6363
char *buf_path, size_t bufsz)
6464
{
6565
struct ptio_cmd cmd;
@@ -147,7 +147,7 @@ int main(int argc, char **argv)
147147
struct ptio_dev dev;
148148
char *cdb_str = NULL;
149149
enum ptio_cdb_type cdb_type = PTIO_CDB_NONE;
150-
int dxfer = PTIO_DXFER_NONE;
150+
enum ptio_dxfer dxfer = PTIO_DXFER_NONE;
151151
char *buf_path = NULL;
152152
int bufsz = 0;
153153
int i, ret;
@@ -277,7 +277,7 @@ int main(int argc, char **argv)
277277
}
278278

279279
/* Open the device and printf some information about it */
280-
ret = ptio_open_dev(&dev, O_RDWR);
280+
ret = ptio_open_dev(&dev, dxfer);
281281
if (ret)
282282
return 1;
283283

src/ptio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ struct ptio_cmd {
109109
int ptio_get_sense(struct ptio_dev *dev, struct ptio_cmd *cmd);
110110

111111
/* In ptio_dev.c */
112-
int ptio_open_dev(struct ptio_dev *dev, mode_t mode);
112+
int ptio_open_dev(struct ptio_dev *dev, enum ptio_dxfer dxfer);
113113
void ptio_close_dev(struct ptio_dev *dev);
114114
int ptio_revalidate_dev(struct ptio_dev *dev);
115115
int ptio_get_dev_information(struct ptio_dev *dev);

src/ptio_dev.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,10 @@ int ptio_write_buf(char *path, uint8_t *buf, size_t bufsz)
548548
/*
549549
* Open a device.
550550
*/
551-
int ptio_open_dev(struct ptio_dev *dev, mode_t mode)
551+
int ptio_open_dev(struct ptio_dev *dev, enum ptio_dxfer dxfer)
552552
{
553553
struct stat st;
554+
mode_t mode;
554555

555556
/* Check that this is a block device */
556557
if (stat(dev->path, &st) < 0) {
@@ -566,6 +567,19 @@ int ptio_open_dev(struct ptio_dev *dev, mode_t mode)
566567
}
567568

568569
/* Open device */
570+
switch (dxfer) {
571+
case PTIO_DXFER_TO_DEV:
572+
mode = O_RDWR;
573+
break;
574+
case PTIO_DXFER_NONE:
575+
case PTIO_DXFER_FROM_DEV:
576+
mode = O_RDONLY;
577+
break;
578+
default:
579+
fprintf(stderr, "Invalid dxfer type\n");
580+
return -1;
581+
}
582+
569583
dev->fd = open(dev->path, mode | O_EXCL);
570584
if (dev->fd < 0) {
571585
fprintf(stderr, "Open %s failed %d (%s)\n",

0 commit comments

Comments
 (0)