From 63ce55e7e9a1bca2594adb5295d6c8385df8a875 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 28 Jul 2021 22:21:41 +0200 Subject: [PATCH] Fix reading directories not failing with EISDIR. --- kernel/inode.cpp | 14 +++++++++++++- utils/cat.c | 9 +-------- utils/wc.c | 9 --------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/kernel/inode.cpp b/kernel/inode.cpp index 926c239b..38d6fe78 100644 --- a/kernel/inode.cpp +++ b/kernel/inode.cpp @@ -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; } diff --git a/utils/cat.c b/utils/cat.c index 35c2cd18..e89f02dc 100644 --- a/utils/cat.c +++ b/utils/cat.c @@ -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]; diff --git a/utils/wc.c b/utils/wc.c index 608de555..bfba3283 100644 --- a/utils/wc.c +++ b/utils/wc.c @@ -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 )