[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Fix bug determining the mode of the io space.
From: |
John Darrington |
Subject: |
[PATCH] Fix bug determining the mode of the io space. |
Date: |
Sun, 22 Dec 2019 20:12:11 +0100 |
---
ChangeLog | 10 ++++++++++
src/ios-dev-file.c | 33 ++++++++++++++++++++++-----------
src/ios-dev.h | 4 ++++
src/ios.c | 3 +--
src/ios.h | 1 +
testsuite/poke.cmd/file-mode.pk | 8 ++++++++
6 files changed, 46 insertions(+), 13 deletions(-)
create mode 100644 testsuite/poke.cmd/file-mode.pk
diff --git a/ChangeLog b/ChangeLog
index ac2e02a..1cb70f5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2019-12-22 John Darrington <address@hidden>
+
+ * src/ios-dev-file.c (ios_dev_file_open): Use a more reliable
+ method to determine the file's mode.
+ (ios_dev_file_get_mode): New function.
+ * src/ios-dev.h (struct ios_dev_if) [get_mode]: New member.
+ * src/ios.c (ios_mode): Use method from implementation.
+ * src/ios.h (IOS_M_RDONLY): New macro.
+ * testsuite/poke.cmd/file-mode.pk: New file.
+
2019-12-21 John Darrington <address@hidden>
* src/pk-file.c (pk_cmd_file): Fix memory leaks and
diff --git a/src/ios-dev-file.c b/src/ios-dev-file.c
index fd8a206..8dc366c 100644
--- a/src/ios-dev-file.c
+++ b/src/ios-dev-file.c
@@ -29,6 +29,7 @@
#include <xalloc.h>
#include <string.h>
+#include "ios.h"
#include "ios-dev.h"
/* State associated with a file device. */
@@ -37,7 +38,7 @@ struct ios_dev_file
{
FILE *file;
char *filename;
- char *mode;
+ int mode;
};
static int
@@ -50,22 +51,23 @@ ios_dev_file_handler_p (const char *handler)
static void *
ios_dev_file_open (const char *handler)
{
- const char *mode;
struct ios_dev_file *fio;
FILE *f;
+ int mode = IOS_M_RDWR;
/* Skip the file:// part in the handler, if needed. */
if (strlen (handler) >= 7
&& strncmp (handler, "file://", 7) == 0)
handler += 7;
- /* Open the requested file. The open mode is read-write if
- possible. Otherwise read-only. */
-
- mode =
- access (handler, R_OK | W_OK) != 0 ? "rb" : "r+b";
-
- f = fopen (handler, mode);
+ /* Open the requested file. Try read-write initially.
+ If that fails, then try read-only. */
+ f = fopen (handler, "r+b");
+ if (!f)
+ {
+ f = fopen (handler, "rb");
+ mode = IOS_M_RDONLY;
+ }
if (!f)
{
perror (handler);
@@ -75,7 +77,7 @@ ios_dev_file_open (const char *handler)
fio = xmalloc (sizeof (struct ios_dev_file));
fio->file = f;
fio->filename = xstrdup (handler);
- fio->mode = xstrdup (mode);
+ fio->mode = mode;
return fio;
}
@@ -88,12 +90,20 @@ ios_dev_file_close (void *iod)
if (fclose (fio->file) != 0)
perror (fio->filename);
free (fio->filename);
- free (fio->mode);
return 1;
}
static int
+ios_dev_file_get_mode (void *iod)
+{
+ struct ios_dev_file *fio = iod;
+
+ return fio->mode;
+}
+
+
+static int
ios_dev_file_getc (void *iod)
{
struct ios_dev_file *fio = iod;
@@ -145,4 +155,5 @@ struct ios_dev_if ios_dev_file =
.seek = ios_dev_file_seek,
.get_c = ios_dev_file_getc,
.put_c = ios_dev_file_putc,
+ .get_mode = ios_dev_file_get_mode,
};
diff --git a/src/ios-dev.h b/src/ios-dev.h
index 7f8579d..432a219 100644
--- a/src/ios-dev.h
+++ b/src/ios-dev.h
@@ -81,4 +81,8 @@ struct ios_dev_if
the character written as an int, or IOD_EOF on error. */
int (*put_c) (void *dev, int c);
+
+ /* Return the mode of the device, as it was opened. */
+
+ int (*get_mode) (void *dev);
};
diff --git a/src/ios.c b/src/ios.c
index 757ada3..c3dd1e1 100644
--- a/src/ios.c
+++ b/src/ios.c
@@ -113,7 +113,6 @@ struct ios
char *handler;
void *dev;
struct ios_dev_if *dev_if;
- int mode;
struct ios *next;
};
@@ -230,7 +229,7 @@ ios_close (ios io)
int
ios_mode (ios io)
{
- return io->mode;
+ return io->dev_if->get_mode (io->dev);
}
ios_off
diff --git a/src/ios.h b/src/ios.h
index b0647f6..85055b7 100644
--- a/src/ios.h
+++ b/src/ios.h
@@ -125,6 +125,7 @@ void ios_close (ios io);
are summarized in the IOS_M_* constants, also defined below. */
#define IOS_M_RDWR 1
+#define IOS_M_RDONLY 2
int ios_mode (ios io);
diff --git a/testsuite/poke.cmd/file-mode.pk b/testsuite/poke.cmd/file-mode.pk
new file mode 100644
index 0000000..bb5402c
--- /dev/null
+++ b/testsuite/poke.cmd/file-mode.pk
@@ -0,0 +1,8 @@
+/* { dg-do run } */
+
+/* { dg-command { .file /etc/passwd } } */
+/* { dg-command { .file /dev/null } } */
+/* { dg-command { .info files } } */
+/* { dg-output " Id Mode Position Filename\n
+* #0 rw 0x00000000#b file:///dev/null\n
+ #1 r 0x00000000#b file:///etc/passwd\n" } */
--
2.11.0
- [PATCH] Fix bug determining the mode of the io space.,
John Darrington <=