Skip to content

Commit

Permalink
Use fstatfs to determine whether a file is a procfs file.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Aug 11, 2021
1 parent 0202daa commit 02eb6be
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
30 changes: 29 additions & 1 deletion ch.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ extern dev_t curr_dev;
extern ino_t curr_ino;
#endif

#if HAVE_STATFS
#include <sys/statfs.h>
#endif

typedef POSITION BLOCKNUM;

public int ignore_eoi;
Expand Down Expand Up @@ -684,6 +688,30 @@ ch_setbufspace(bufspace)
}
}

/*
*/
static int
trust_size(file, fsize)
int file;
POSITION fsize;
{
if (fsize != 0)
{
/* Nontrustable files always say their size is 0, so this isn't one. */
return 1;
}
#if HAVE_STATFS
/* Cannot trust procfs files. */
struct statfs st;
if (fstatfs(file, &st) == 0)
return (st.f_type != PROC_SUPER_MAGIC);
#else
(void) file;
#endif
/* If we can't tell the file type, be safe and say it's not trustable. */
return 0;
}

/*
* Flush (discard) any saved file state, including buffer contents.
*/
Expand Down Expand Up @@ -732,7 +760,7 @@ ch_flush(VOID_PARAM)
* data. They are sometimes, but not always, seekable.
* Force them to be non-seekable here.
*/
if (ch_fsize == 0)
if (!trust_size(ch_file, ch_fsize))
{
ch_fsize = NULL_POSITION;
ch_flags &= ~CH_CANSEEK;
Expand Down
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ AH_TEMPLATE([HAVE_CONST],
[Define HAVE_CONST if your compiler supports the "const" modifier.])
AH_TEMPLATE([HAVE_STAT_INO],
[Define HAVE_STAT_INO if your struct stat has st_ino and st_dev.])
AH_TEMPLATE([HAVE_STATFS],
[Define HAVE_STATFS if have have fstatfs with f_type.])
AH_TEMPLATE([HAVE_TIME_T],
[Define HAVE_TIME_T if your system supports the "time_t" type.])
AH_TEMPLATE([HAVE_STRERROR],
Expand Down Expand Up @@ -266,6 +268,10 @@ AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/stat.h>],
[struct stat s; dev_t dev = s.st_dev; ino_t ino = s.st_ino;],
[AC_MSG_RESULT(yes); AC_DEFINE(HAVE_STAT_INO)], [AC_MSG_RESULT(no)])
AC_MSG_CHECKING(for statfs)
AC_TRY_COMPILE([#include <sys/statfs.h>],
[struct statfs s; s.f_type = PROC_SUPER_MAGIC; (void) fstatfs(0,&s); ],
[AC_MSG_RESULT(yes); AC_DEFINE(HAVE_STATFS)], [AC_MSG_RESULT(no)])

# Checks for ANSI function prototypes.
AC_MSG_CHECKING(for ANSI function prototypes)
Expand Down

0 comments on commit 02eb6be

Please sign in to comment.