Commit d27af7e8 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 6249dcd1
...@@ -27,6 +27,9 @@ ...@@ -27,6 +27,9 @@
#include <memory> #include <memory>
static error _pathError(const char *op, const string &path, int syserr);
// os:: // os::
namespace os { namespace os {
...@@ -63,13 +66,39 @@ error File::stat(struct stat *st) { ...@@ -63,13 +66,39 @@ error File::stat(struct stat *st) {
// _errno returns error corresponding to file->op and errno. // _errno returns error corresponding to file->op and errno.
error File::_errno(const char *op) { error File::_errno(const char *op) {
File *f = this; File *f = this;
char ebuf[128]; return _pathError(op, f->_path, errno);
char *estr = strerror_r(errno, ebuf, sizeof(ebuf));
return fmt::errorf("%s %s: %s", op, f->_path, estr);
} }
} // os:: } // os::
// _pathError returns os.PathError-like for op/path and system error
// indicated by syserr.
static error _pathError(const char *op, const string &path, int syserr) {
char ebuf[128];
char *estr = strerror_r(syserr, ebuf, sizeof(ebuf));
return fmt::errorf("%s %s: %s", op, path.c_str(), estr);
}
// mm::
namespace mm {
// map_into memory-maps f.fd[offset +size) as into [addr +size).
// prot is PROT_* from mmap(2).
// flags is MAP_* from mmap(2); MAP_FIXED is added to flags.
//
// XXX split from File?
error map_into(void *addr, size_t size, int prot, int flags, const File &f, off_t offset) {
void *addr2;
addr2 = mmap(addr, size, PROT_READ, MAP_FIXED | MAP_SHARED, f->fd(), offset);
if (addr2 == MAP_FAILED)
return _pathError("mmap", f.name(), errno);
if (addr2 != addr)
panic("mmap(addr, MAP_FIXED): returned !addr");
return nil;
}
} // mm::
// fmt:: // fmt::
namespace fmt { namespace fmt {
......
...@@ -80,6 +80,13 @@ tuple<File, error> open(const string &path, int flags = O_RDONLY, ...@@ -80,6 +80,13 @@ tuple<File, error> open(const string &path, int flags = O_RDONLY,
} // os:: } // os::
// mm::
namespace mm {
error map_into(void *addr, size_t, int prot, int flags, const os::File &f, off_t offset);
// XXX unmap
} // mm::
// fmt:: // fmt::
namespace fmt { namespace fmt {
......
...@@ -38,6 +38,7 @@ using namespace golang; ...@@ -38,6 +38,7 @@ using namespace golang;
#include <vector> #include <vector>
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <stdint.h> #include <stdint.h>
...@@ -278,14 +279,8 @@ error _Mapping::_remmapblk(int64_t blk, Tid at) { ...@@ -278,14 +279,8 @@ error _Mapping::_remmapblk(int64_t blk, Tid at) {
} }
// block is inside file - mmap file data // block is inside file - mmap file data
else { else {
mm.map_into_ro(blkmem, fsfile.fd(), blk*f->blksize); // XXX err err = mm::map_into(blkmem, f->blksize, PROT_READ, MAP_SHARED, fsfile, blk*f->blksize);
// XXX err handle
err = mmap_into_ro(blkmem, f->blksize, fsfile, blk*f->blksize);
err = fsfile.mmapfix(blkmem, f->blksize, PROT_READ, MAP_SHARED, blk*f->blksize);
mmap(blkmem, f->blksize, PROT_READ, MAP_FIXED | MAP_SHARED,
fsfile.fd(), blk*f->blksize);
} }
return nil; return nil;
...@@ -314,14 +309,3 @@ tuple<os::File, error> WCFS::_open(const string &path, int flags) { ...@@ -314,14 +309,3 @@ tuple<os::File, error> WCFS::_open(const string &path, int flags) {
string h(uint64_t v) { string h(uint64_t v) {
return fmt::sprintf("%016x", v); return fmt::sprintf("%016x", v);
} }
// map_into_ro memory-maps f.fd[offset +size) as read-only into [addr +size).
// The mapping is created with MAP_SHARED.
error mmap_into_ro(void *addr, size_t size, const File &f. off_t offset) {
void *addr2;
addr2 = mmap(blkmem, size, PROT_READ, MAP_FIXED | MAP_SHARED, f.fd(), offset);
// XXX -> err
// XXX assert addr2 == addr
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment