[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Allow .load only on suitable file system entries
From: |
John Darrington |
Subject: |
[PATCH] Allow .load only on suitable file system entries |
Date: |
Sun, 24 Nov 2019 08:31:35 +0100 |
* src/pk-file (pk_file_readable): New function.
(pk_cmd_load_file): Use it to check files before
opening.
---
ChangeLog | 6 ++++++
src/pk-file.c | 51 +++++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 51 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 9cb3328..bb9b781 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2019-11-24 John Darrington <address@hidden>
+
+ * src/pk-file (pk_file_readable): New function.
+ (pk_cmd_load_file): Use it to check files before
+ opening.
+
2019-11-22 Jose E. Marchesi <address@hidden>
* src/pkl-ast.c (pkl_ast_type_is_complete): Handle function types.
diff --git a/src/pk-file.c b/src/pk-file.c
index 385968b..2cfc532 100644
--- a/src/pk-file.c
+++ b/src/pk-file.c
@@ -18,6 +18,8 @@
#include <config.h>
#include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <gettext.h>
#define _(str) dgettext (PACKAGE, str)
@@ -199,6 +201,44 @@ pk_cmd_info_files (int argc, struct pk_cmd_arg argv[],
uint64_t uflags)
return 1;
}
+/* Returns zero iff FILENAME is the name
+ of an entry in the filesystem which :
+ * is not a directory;
+ * is readable; AND
+ * exists.
+ If it satisfies the above, the function returns NULL.
+ Otherwise, returns a pointer to a statically allocated
+ error message describing how the file doesn't satisfy
+ the conditions. */
+static char *
+pk_file_readable (const char *filename)
+{
+ static char errmsg[4096];
+ struct stat statbuf;
+ if (0 != stat (filename, &statbuf))
+ {
+ char *why = strerror (errno);
+ snprintf (errmsg, 4096, _("Cannot stat %s: %s\n"), filename, why);
+ return errmsg;
+ }
+
+ if (statbuf.st_mode & S_IFDIR)
+ {
+ snprintf (errmsg, 4096, _("%s is a directory\n"), filename);
+ return errmsg;
+ }
+
+ if (access (filename, R_OK) != 0)
+ {
+ char *why = strerror (errno);
+ snprintf (errmsg, 4096, _("%s: file cannot be read: %s\n"), filename,
why);
+ return errmsg;
+ }
+
+ return 0;
+}
+
+
static int
pk_cmd_load_file (int argc, struct pk_cmd_arg argv[], uint64_t uflags)
{
@@ -210,7 +250,9 @@ pk_cmd_load_file (int argc, struct pk_cmd_arg argv[],
uint64_t uflags)
assert (argc == 1);
arg = PK_CMD_ARG_STR (argv[0]);
- if (access (arg, R_OK) == 0)
+ char *emsg = NULL;
+
+ if ((emsg = pk_file_readable (arg)) == NULL)
filename = xstrdup (arg);
else if (arg[0] != '/')
{
@@ -222,7 +264,7 @@ pk_cmd_load_file (int argc, struct pk_cmd_arg argv[],
uint64_t uflags)
strcat (filename, "/");
strcat (filename, arg);
- if (access (filename, R_OK) != 0)
+ if ((emsg = pk_file_readable (arg)) == NULL)
goto no_file;
}
else
@@ -236,10 +278,7 @@ pk_cmd_load_file (int argc, struct pk_cmd_arg argv[],
uint64_t uflags)
return 1;
no_file:
- {
- char *why = strerror (errno);
- pk_printf (_("%s: file cannot be read: %s\n"), arg, why);
- }
+ pk_puts (emsg);
error:
free (filename);
return 0;
--
2.11.0
- [PATCH] Allow .load only on suitable file system entries,
John Darrington <=