poke-devel
[Top][All Lists]
Advanced

[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: Tue, 26 Nov 2019 14:00:25 +0100

        * src/pk-file (pk_file_readable): New function.
        (pk_cmd_load_file): Use it to check files before
        opening.
---
 ChangeLog      |  7 +++++++
 bootstrap.conf |  1 +
 src/pk-file.c  | 53 +++++++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 55 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a4e8eef..fc5d2d2 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-26  Jose E. Marchesi  <address@hidden>
 
        * src/pvm-alloc.c (pvm_realloc): New function.
@@ -17,6 +23,7 @@
        * src/ios-dev-file.c (ios_dev_file_getc): Do not rely on EOF ==
        IOD_EOF.
 
+
 2019-11-22  Jose E. Marchesi  <address@hidden>
 
        * src/pkl-ast.c (pkl_ast_type_is_complete): Handle function types.
diff --git a/bootstrap.conf b/bootstrap.conf
index e668fd4..020de9d 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -20,6 +20,7 @@
 gnulib_modules="
   array-list
   byteswap
+  fstat
   gcd
   gendocs
   getopt-gnu
diff --git a/src/pk-file.c b/src/pk-file.c
index 385968b..d01f91c 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,46 @@ 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 (S_ISDIR (statbuf.st_mode))
+    {
+      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 +252,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 +266,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 +280,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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]