[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] Allow .load only on suitable file system entries
From: |
Jose E. Marchesi |
Subject: |
Re: [PATCH] Allow .load only on suitable file system entries |
Date: |
Fri, 29 Nov 2019 09:05:57 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) |
+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;
+ }
The stat and check for S_ISDIR can be replaced with:
if (fstat (fileno (stream), &st) == -1 || !S_ISREG (st.st_mode))
sprintf (errmsg, 4096, _("%s: not a regular file"));
No it can't; Because we don't have a stream.
I'm unsure if we want to insist on file being a "regular" file. For
example I can envisage a situation where somebody might want to use poke
to examine (or write!) a filesystem or swapspace on /dev/sd??
Also, if stat fails for any reason (eg the file doesn't exist), then the
above
would give a misleading error message.
The check for readability can be done portably using another gnulib
function, freadable:
if (!freadable (stream))
sprintf (errmsg, 4096, _("%s: file cannot be read"));
Again, we don't have a stream at this point, so that isn't going to
work.
Hm ok. Let's use stat and access instead.
But then, we don't need any additional gnulib module..