From 03cafb600101c4bc659521b833a1f3a8c56938be Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 1 Oct 2024 11:45:17 -0700 Subject: More fixes opening files & exclusive mode. --- src/stable/myriad.cpp | 2 +- src/stable/myriad.h | 2 +- src/tests/myriadfs.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/tests/myriadfs.cpp diff --git a/src/stable/myriad.cpp b/src/stable/myriad.cpp index f9fcd3e..03cffa9 100644 --- a/src/stable/myriad.cpp +++ b/src/stable/myriad.cpp @@ -84,7 +84,7 @@ Bu::MyriadStream Bu::Myriad::open( Bu::Myriad::StreamId iStream, Bu::MutexLocker l( mhStream ); if( (eMode&Create) ) { - if( !hStream.has( iStream ) ) + if( hStream.has( iStream ) ) { if( (eMode&Exclusive) ) { diff --git a/src/stable/myriad.h b/src/stable/myriad.h index 7f84c2a..6168aa2 100644 --- a/src/stable/myriad.h +++ b/src/stable/myriad.h @@ -45,7 +45,7 @@ namespace Bu Truncate = 0x08, ///< Truncate file if it does exist Append = 0x10, ///< Start writing at end of file //NonBlock = 0x20, ///< Open file in non-blocking mode - Exclusive = 0x44, ///< Create file, if it exists then fail + Exclusive = 0x40, ///< Create file, if it exists then fail // Helpful mixes ReadWrite = 0x03, ///< Open for reading and writing diff --git a/src/tests/myriadfs.cpp b/src/tests/myriadfs.cpp new file mode 100644 index 0000000..1266e4b --- /dev/null +++ b/src/tests/myriadfs.cpp @@ -0,0 +1,62 @@ +#include "bu/file.h" +#include "bu/membuf.h" +#include "bu/myriadfs.h" +#include "bu/myriadstream.h" +#include "bu/sio.h" + +using namespace Bu; + +int main() +{ +// Bu::MemBuf mb; + Bu::File mb("store.myr", File::Read|File::Write|File::Create ); + Bu::MyriadFs mfs( mb, 512 ); + + sio << "Creating dirs..." << sio.nl; + mfs.create("/etc", Bu::MyriadFs::typeDir|0755 ); + mfs.create("/dev", Bu::MyriadFs::typeDir|0755 ); + mfs.create("/usr", Bu::MyriadFs::typeDir|0755 ); + + mfs.create("/dev/null", Bu::MyriadFs::typeChrDev|0666, 1, 3 ); + mfs.create("/dev/zero", Bu::MyriadFs::typeChrDev|0666, 1, 5 ); + mfs.create("/dev/sda", Bu::MyriadFs::typeBlkDev|0660, 8, 0 ); + + sio << "Creating files..." << sio.nl; + { + Bu::MyriadStream ms = mfs.open("/hello", Bu::MyriadFs::Read ); + ms.write("world!"); + } + { + Bu::MyriadStream ms = mfs.open("/etc/hello", Bu::MyriadFs::Read ); + ms.write("world, again!"); + } + { + Bu::MyriadStream ms = mfs.open("/etc/trunc", Bu::MyriadFs::Write ); + ms.write("[longer text shouldn't be seen]"); + } + { + Bu::MyriadStream ms = mfs.open("/etc/trunc", Bu::MyriadFs::Write|Bu::MyriadFs::Create|Bu::MyriadFs::Truncate ); + ms.write("[short text]"); + } + + sio << "Reading files..." << sio.nl; + { + Bu::MyriadStream ms = mfs.open("/hello", Bu::MyriadFs::Read ); + char buf[512]; + buf[ms.read( buf, 512 )] = '\0'; + sio << "read: '" << buf << "'" << sio.nl; + } + { + Bu::MyriadStream ms = mfs.open("/etc/hello", Bu::MyriadFs::Read ); + char buf[512]; + buf[ms.read( buf, 512 )] = '\0'; + sio << "read: '" << buf << "'" << sio.nl; + } + { + Bu::MyriadStream ms = mfs.open("/etc/trunc", Bu::MyriadFs::Read ); + char buf[512]; + buf[ms.read( buf, 512 )] = '\0'; + sio << "read: '" << buf << "'" << sio.nl; + } +} + -- cgit v1.2.3