poke-devel
[Top][All Lists]
Advanced

[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: Tue, 26 Nov 2019 14:13:34 +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"));

The check for readability can be done portably using another gnulib
function, freadable:

  if (!freadable (stream))
    sprintf (errmsg, 4096, _("%s: file cannot be read"));



reply via email to

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