diff options
Diffstat (limited to 'platform/default/src/mbgl/storage/local_file_request.cpp')
-rw-r--r-- | platform/default/src/mbgl/storage/local_file_request.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/platform/default/src/mbgl/storage/local_file_request.cpp b/platform/default/src/mbgl/storage/local_file_request.cpp new file mode 100644 index 0000000000..084e6ff1b0 --- /dev/null +++ b/platform/default/src/mbgl/storage/local_file_request.cpp @@ -0,0 +1,37 @@ +#include <mbgl/storage/file_source_request.hpp> +#include <mbgl/storage/response.hpp> +#include <mbgl/util/io.hpp> + +#include <sys/types.h> +#include <sys/stat.h> + +#if defined(_WINDOWS) && !defined(S_ISDIR) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#endif + +namespace mbgl { + +void requestLocalFile(const std::string& path, ActorRef<FileSourceRequest> req) { + Response response; + struct stat buf; + int result = stat(path.c_str(), &buf); + + if (result == 0 && (S_IFDIR & buf.st_mode)) { + response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound); + } else if (result == -1 && errno == ENOENT) { + response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound); + } else { + auto data = util::readFile(path); + if (!data) { + response.error = std::make_unique<Response::Error>( + Response::Error::Reason::Other, + std::string("Cannot read file ") + path); + } else { + response.data = std::make_shared<std::string>(std::move(*data)); + } + } + + req.invoke(&FileSourceRequest::setResponse, response); +} + +} // namespace mbgl |