Fix reading directories not failing with EISDIR.

This commit is contained in:
Jonas 'Sortie' Termansen 2021-07-28 22:21:41 +02:00
parent 16bdb2ba84
commit 63ce55e7e9
3 changed files with 14 additions and 18 deletions

View File

@ -155,7 +155,11 @@ off_t AbstractInode::lseek(ioctx_t* /*ctx*/, off_t /*offset*/, int /*whence*/)
ssize_t AbstractInode::read(ioctx_t* ctx, uint8_t* buf, size_t count)
{
if ( !supports_iovec )
{
if ( inode_type == INODE_TYPE_DIR )
return errno = EISDIR, -1;
return errno = EBADF, -1;
}
struct iovec iov;
memset(&iov, 0, sizeof(iov));
iov.iov_base = (void*) buf;
@ -166,7 +170,11 @@ ssize_t AbstractInode::read(ioctx_t* ctx, uint8_t* buf, size_t count)
ssize_t AbstractInode::readv(ioctx_t* ctx, const struct iovec* iov, int iovcnt)
{
if ( supports_iovec )
{
if ( inode_type == INODE_TYPE_DIR )
return errno = EISDIR, -1;
return errno = EBADF, -1;
}
ssize_t sofar = 0;
for ( int i = 0; i < iovcnt && sofar < SSIZE_MAX; i++ )
{
@ -198,6 +206,8 @@ ssize_t AbstractInode::pread(ioctx_t* ctx, uint8_t* buf, size_t count,
{
if ( inode_type == INODE_TYPE_STREAM || inode_type == INODE_TYPE_TTY )
return errno = ESPIPE, -1;
if ( inode_type == INODE_TYPE_DIR )
return errno = EISDIR, -1;
return errno = EBADF, -1;
}
struct iovec iov;
@ -214,6 +224,8 @@ ssize_t AbstractInode::preadv(ioctx_t* ctx, const struct iovec* iov, int iovcnt,
{
if ( inode_type == INODE_TYPE_STREAM || inode_type == INODE_TYPE_TTY )
return errno = ESPIPE, -1;
if ( inode_type == INODE_TYPE_DIR )
return errno = EISDIR, -1;
return errno = EBADF, -1;
}
ssize_t sofar = 0;
@ -364,7 +376,7 @@ ssize_t AbstractInode::readdirents(ioctx_t* /*ctx*/,
off_t /*start*/)
{
if ( inode_type == INODE_TYPE_DIR )
return errno = EBADF, -1;
return errno = ENOTDIR, -1;
return errno = ENOTDIR, -1;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 Jonas 'Sortie' Termansen.
* Copyright (c) 2013, 2021 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -31,13 +31,6 @@
static bool cat_fd(int fd, const char* path)
{
struct stat st;
if ( fstat(fd, &st) == 0 )
{
if ( S_ISDIR(st.st_mode) )
return error(0, EISDIR, "`%s'", path), false;
}
const size_t BUFFER_SIZE = 16 * 1024;
uint8_t buffer[BUFFER_SIZE];

View File

@ -165,15 +165,6 @@ static void compact_arguments(int* argc, char*** argv)
bool word_count_file(FILE* fp, const char* path, int flags,
struct word_count* total)
{
struct stat st;
if ( fstat(fileno(fp), &st) == 0 && S_ISDIR(st.st_mode) )
{
struct word_count word_count;
memset(&word_count, 0, sizeof(word_count));
error(0, EISDIR, "`%s'", path);
print_stats(word_count, stdout, flags, path);
return false;
}
struct word_count word_count = count_words(fp);
// TODO: Possible overflow here!
if ( total )