Support O_DIRECTORY open() flag

Close #3545.
This commit is contained in:
Sebastian Huber
2018-10-11 10:51:21 +02:00
parent 68799dac67
commit 4af18b34f4
2 changed files with 38 additions and 2 deletions

View File

@@ -73,6 +73,7 @@ static int do_open(
bool make = (oflag & O_CREAT) == O_CREAT;
bool exclusive = (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
bool truncate = (oflag & O_TRUNC) == O_TRUNC;
bool open_dir;
int eval_flags = RTEMS_FS_FOLLOW_LINK
| (read_access ? RTEMS_FS_PERMS_READ : 0)
| (write_access ? RTEMS_FS_PERMS_WRITE : 0)
@@ -86,14 +87,24 @@ static int do_open(
create_regular_file( &ctx, mode );
}
if ( write_access ) {
#ifdef O_DIRECTORY
open_dir = ( oflag & O_DIRECTORY ) == O_DIRECTORY;
#else
open_dir = false;
#endif
if ( write_access || open_dir ) {
const rtems_filesystem_location_info_t *currentloc =
rtems_filesystem_eval_path_get_currentloc( &ctx );
mode_t type = rtems_filesystem_location_type( currentloc );
if ( S_ISDIR( type ) ) {
if ( write_access && S_ISDIR( type ) ) {
rtems_filesystem_eval_path_error( &ctx, EISDIR );
}
if ( open_dir && !S_ISDIR( type ) ) {
rtems_filesystem_eval_path_error( &ctx, ENOTDIR );
}
}
rtems_filesystem_eval_path_extract_currentloc( &ctx, &iop->pathinfo );

View File

@@ -130,6 +130,29 @@ void stat_a_file(
}
static void test_open_directory(void)
{
static const char file[] = "somefile";
int status;
int fd;
fd = open( file, O_CREAT, S_IRWXU );
rtems_test_assert( fd >= 0 );
status = close( fd );
rtems_test_assert( status == 0 );
#ifdef O_DIRECTORY
errno = 0;
fd = open( file, O_DIRECTORY, S_IRWXU );
rtems_test_assert( fd == -1 );
rtems_test_assert( errno == ENOTDIR );
#endif
status = unlink( file );
rtems_test_assert( status == 0 );
}
/*
* Main entry point of the test
*/
@@ -161,6 +184,8 @@ int main(
TEST_BEGIN();
test_open_directory();
/*
* Grab the maximum size of an in-memory file.
*/