summaryrefslogtreecommitdiff
path: root/ucoo
diff options
context:
space:
mode:
authorNicolas Schodet2015-10-09 11:39:53 +0200
committerNicolas Schodet2019-10-07 00:44:50 +0200
commit381c4d1886ec72364e0e62ad0f08a2511a0c8304 (patch)
treee5fd1155c800d92477f090ce7dabf175ebe4bfbf /ucoo
parent0f38f554b462a516d66649f919faa7336a9601d2 (diff)
ucoo/{intf,arch}: add file system interface
Diffstat (limited to 'ucoo')
-rw-r--r--ucoo/arch/syscalls.newlib.cc185
-rw-r--r--ucoo/arch/syscalls.newlib.hh8
-rw-r--r--ucoo/intf/Module2
-rw-r--r--ucoo/intf/file_system.cc58
-rw-r--r--ucoo/intf/file_system.hh83
5 files changed, 289 insertions, 47 deletions
diff --git a/ucoo/arch/syscalls.newlib.cc b/ucoo/arch/syscalls.newlib.cc
index 5b0b777..00d1f06 100644
--- a/ucoo/arch/syscalls.newlib.cc
+++ b/ucoo/arch/syscalls.newlib.cc
@@ -26,11 +26,15 @@
#include <reent.h>
#include <sys/stat.h>
+#include <sys/types.h>
#include <sys/times.h>
#include <time.h>
+#include <fcntl.h>
#include <errno.h>
-ucoo::Stream *ucoo::syscalls_streams[3];
+ucoo::Stream *ucoo::syscalls_streams[8];
+
+ucoo::FileSystem *ucoo::syscalls_file_system;
/** This is needed by C++ ABI, this simple definition will do. See:
* http://lists.debian.org/debian-gcc/2003/07/msg00057.html */
@@ -74,21 +78,6 @@ _exit (int n)
ucoo::halt ();
}
-/** Close a file, unimplemented. */
-extern "C" int
-_close_r (struct _reent *ptr, int fd)
-{
- return -1;
-}
-
-/** Status of open file, consider all files as character devices. */
-extern "C" int
-_fstat_r (struct _reent *ptr, int fd, struct stat *st)
-{
- st->st_mode = S_IFCHR;
- return 0;
-}
-
/** Get PID, minimal implementation. */
extern "C" int
_getpid_r (struct _reent *ptr)
@@ -96,11 +85,19 @@ _getpid_r (struct _reent *ptr)
return 1;
}
-/** Whether file is a terminal, consider this is always true. */
+/** Whether file is a terminal, consider this is always true for stdin, stdout
+ * and stderr. */
extern "C" int
_isatty_r (struct _reent *ptr, int fd)
{
- return 1;
+ if (fd < ucoo::lengthof (ucoo::syscalls_streams)
+ && ucoo::syscalls_streams[fd])
+ return fd < 3;
+ else
+ {
+ ptr->_errno = EBADF;
+ return -1;
+ }
}
/** Send a signal, no process, no signal. */
@@ -111,46 +108,137 @@ _kill_r (struct _reent *ptr, int pid, int sig)
return -1;
}
-/** Set position in a file, no-op. */
+/** Set position in a file, unimplemented. */
extern "C" off_t
_lseek_r (struct _reent *ptr, int fd, off_t pos, int whence)
{
- return 0;
+ ptr->_errno = ENOSYS;
+ return -1;
}
-/** Open a file, unimplemented. */
+/** Open a file. */
extern "C" int
_open_r (struct _reent *ptr, const char *file, int flags, int mode)
{
- return -1;
+ if (ucoo::syscalls_file_system)
+ {
+ int i;
+ for (i = 0; i < ucoo::lengthof (ucoo::syscalls_streams)
+ && ucoo::syscalls_streams[i]; i++)
+ ;
+ if (i == ucoo::lengthof (ucoo::syscalls_streams))
+ {
+ ptr->_errno = ENFILE;
+ return -1;
+ }
+ else
+ {
+ ucoo::FileSystem::Mode mode;
+#ifdef O_BINARY
+ flags &= ~O_BINARY;
+#endif
+#ifdef O_TEXT
+ flags &= ~O_TEXT;
+#endif
+ if (flags == O_RDONLY)
+ mode = ucoo::FileSystem::Mode::READ;
+ else if (flags == (O_WRONLY | O_CREAT | O_TRUNC))
+ mode = ucoo::FileSystem::Mode::WRITE;
+ else
+ {
+ ptr->_errno = ENOSYS;
+ return -1;
+ }
+ ucoo::FileSystem::Error error;
+ ucoo::syscalls_streams[i] =
+ ucoo::syscalls_file_system->open (file, mode, error);
+ if (!ucoo::syscalls_streams[i])
+ {
+ if (error == ucoo::FileSystem::Error::ACCESS_DENIED)
+ ptr->_errno = EACCES;
+ else if (error == ucoo::FileSystem::Error::TOO_MANY_OPEN_FILES)
+ ptr->_errno = ENFILE;
+ else if (error == ucoo::FileSystem::Error::NAME_TOO_LONG)
+ ptr->_errno = ENAMETOOLONG;
+ else if (error == ucoo::FileSystem::Error::NO_SUCH_FILE)
+ ptr->_errno = ENOENT;
+ else if (error == ucoo::FileSystem::Error::NO_SPACE_LEFT)
+ ptr->_errno = ENOSPC;
+ else if (error == ucoo::FileSystem::Error::READ_ONLY)
+ ptr->_errno = EROFS;
+ else
+ ucoo::assert_unreachable ();
+ return -1;
+ }
+ else
+ return i;
+ }
+ }
+ else
+ {
+ ptr->_errno = ENOSYS;
+ return -1;
+ }
+}
+
+/** Close a file. */
+extern "C" int
+_close_r (struct _reent *ptr, int fd)
+{
+ if (ucoo::syscalls_file_system
+ && fd < ucoo::lengthof (ucoo::syscalls_streams)
+ && ucoo::syscalls_streams[fd])
+ {
+ ucoo::syscalls_file_system->close (ucoo::syscalls_streams[fd]);
+ ucoo::syscalls_streams[fd] = nullptr;
+ return 0;
+ }
+ else
+ {
+ ptr->_errno = EBADF;
+ return -1;
+ }
+}
+
+/** Status of open file, consider all files as character devices. */
+extern "C" int
+_fstat_r (struct _reent *ptr, int fd, struct stat *st)
+{
+ if (fd < ucoo::lengthof (ucoo::syscalls_streams)
+ && ucoo::syscalls_streams[fd])
+ {
+ st->st_mode = S_IFCHR;
+ return 0;
+ }
+ else
+ {
+ ptr->_errno = EBADF;
+ return -1;
+ }
}
/** Read from file. */
extern "C" int
_read_r (struct _reent *ptr, int fd, void *buf, size_t cnt)
{
- if (fd == 0)
+ if (fd < ucoo::lengthof (ucoo::syscalls_streams)
+ && ucoo::syscalls_streams[fd])
{
- if (ucoo::syscalls_streams[0])
+ ucoo::Stream &s = *ucoo::syscalls_streams[fd];
+ int r = s.read (reinterpret_cast<char *> (buf), cnt);
+ switch (r)
{
- ucoo::Stream &s = *ucoo::syscalls_streams[0];
- int r = s.read (reinterpret_cast<char *> (buf), cnt);
- switch (r)
- {
- case -2:
- return 0;
- case -1:
- ptr->_errno = EIO;
- return -1;
- case 0:
- ptr->_errno = EAGAIN;
- return -1;
- default:
- return r;
- }
- }
- else
+ case -2:
return 0;
+ case -1:
+ ptr->_errno = EIO;
+ return -1;
+ case 0:
+ ptr->_errno = EAGAIN;
+ return -1;
+ default:
+ return r;
+ }
}
else
{
@@ -163,7 +251,8 @@ _read_r (struct _reent *ptr, int fd, void *buf, size_t cnt)
extern "C" int
_write_r (struct _reent *ptr, int fd, const void *buf, size_t cnt)
{
- if ((fd == 1 || fd == 2) && ucoo::syscalls_streams[fd])
+ if (fd < ucoo::lengthof (ucoo::syscalls_streams)
+ && ucoo::syscalls_streams[fd])
{
ucoo::Stream &s = *ucoo::syscalls_streams[fd];
int r = s.write (reinterpret_cast<const char *> (buf), cnt);
@@ -189,8 +278,16 @@ _write_r (struct _reent *ptr, int fd, const void *buf, size_t cnt)
extern "C" int
_unlink_r (struct _reent *ptr, const char *pathname)
{
- ptr->_errno = ENOSYS;
- return -1;
+ if (ucoo::syscalls_file_system)
+ {
+ ucoo::syscalls_file_system->unlink (pathname);
+ return 0;
+ }
+ else
+ {
+ ptr->_errno = ENOSYS;
+ return -1;
+ }
}
extern "C" int
diff --git a/ucoo/arch/syscalls.newlib.hh b/ucoo/arch/syscalls.newlib.hh
index af4ff98..5d362ec 100644
--- a/ucoo/arch/syscalls.newlib.hh
+++ b/ucoo/arch/syscalls.newlib.hh
@@ -24,11 +24,15 @@
//
// }}}
#include "ucoo/intf/stream.hh"
+#include "ucoo/intf/file_system.hh"
namespace ucoo {
-// Streams used as stdin, stdout and stderr.
-extern Stream *syscalls_streams[3];
+/// Streams used as stdin, stdout, stderr and open files.
+extern Stream *syscalls_streams[8];
+
+/// File system, if any.
+extern FileSystem *syscalls_file_system;
} // namespace ucoo
diff --git a/ucoo/intf/Module b/ucoo/intf/Module
index db0f820..9d0f337 100644
--- a/ucoo/intf/Module
+++ b/ucoo/intf/Module
@@ -1 +1 @@
-ucoo_intf_SOURCES = stream.cc spi_master.cc lcd.cc
+ucoo_intf_SOURCES = stream.cc spi_master.cc lcd.cc file_system.cc
diff --git a/ucoo/intf/file_system.cc b/ucoo/intf/file_system.cc
new file mode 100644
index 0000000..b171861
--- /dev/null
+++ b/ucoo/intf/file_system.cc
@@ -0,0 +1,58 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2015 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/intf/file_system.hh"
+#ifdef TARGET_newlib
+# include "ucoo/arch/syscalls.newlib.hh"
+# include "ucoo/common.hh"
+#endif
+
+namespace ucoo {
+
+void
+FileSystem::enable (bool root)
+{
+ disable ();
+ enabled_ = true;
+ root_ = root;
+#ifdef TARGET_newlib
+ if (root_)
+ {
+ assert (!syscalls_file_system);
+ syscalls_file_system = this;
+ }
+#endif
+}
+
+void
+FileSystem::disable ()
+{
+#ifdef TARGET_newlib
+ if (enabled_ && root_)
+ syscalls_file_system = nullptr;
+#endif
+ enabled_ = false;
+ root_ = false;
+}
+
+} // namespace ucoo
diff --git a/ucoo/intf/file_system.hh b/ucoo/intf/file_system.hh
new file mode 100644
index 0000000..ad43e98
--- /dev/null
+++ b/ucoo/intf/file_system.hh
@@ -0,0 +1,83 @@
+#ifndef ucoo_intf_file_system_hh
+#define ucoo_intf_file_system_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2015 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/intf/stream.hh"
+
+namespace ucoo {
+
+/// Interface to a simple file system, can be bound to newlib syscalls.
+class FileSystem
+{
+ public:
+ /// Open mode.
+ enum class Mode {
+ /// Read only.
+ READ,
+ /// Write only. If file exists, it is truncated, else it is created.
+ WRITE,
+ };
+ /// Error code.
+ enum class Error {
+ /// Access not allowed.
+ ACCESS_DENIED,
+ /// The maximum number of open files is exceeded.
+ TOO_MANY_OPEN_FILES,
+ /// File name is too long.
+ NAME_TOO_LONG,
+ /// File does not exists.
+ NO_SUCH_FILE,
+ /// No space left on device.
+ NO_SPACE_LEFT,
+ /// Read only file system.
+ READ_ONLY,
+ };
+ public:
+ /// Enable (and register to newlib syscalls).
+ virtual void enable (bool root = true);
+ /// Disable (and unregister from newlib syscalls).
+ virtual void disable ();
+ /// Open file and return an open stream or nullptr on failure.
+ virtual Stream *open (const char *filename, Mode mode, Error &error) = 0;
+ /// Open file and return an open stream or nullptr on failure, no error
+ /// code.
+ Stream *open (const char *filename, Mode mode = Mode::READ)
+ {
+ Error error;
+ return open (filename, mode, error);
+ }
+ /// Close a previously open file.
+ virtual void close (Stream *file) = 0;
+ /// Remove a file, does not complain if the file does not exists.
+ virtual void unlink (const char *filename) = 0;
+ private:
+ /// Is it enabled?
+ bool enabled_;
+ /// Is this the root file system?
+ bool root_;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_intf_file_system_hh