Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Unified Diff: native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc

Issue 9956156: Add mount sources for a MountNode (inode) (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc
===================================================================
--- native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc (revision 131457)
+++ native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc (working copy)
@@ -2,290 +2,273 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
-
-#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string>
-#include <vector>
-
+#include "mount.h"
#include "mount_mem.h"
+#include "mount_node.h"
+#include "mount_node_dir.h"
+#include "mount_node_mem.h"
#include "path.h"
-#include "util/simple_auto_lock.h"
-static const size_t s_blksize = 1024;
+#include "auto_lock.h"
+#include "ref_object.h"
-class MountNodeMem {
- public:
- MountNodeMem(int ino) {
- ino_ = ino;
- ref_count_ = 0;
- data_ = NULL;
- memset(&stat_, 0, sizeof(stat_));
- Resize(s_blksize);
- };
+MountMem::MountMem(int dev) :
binji 2012/04/16 22:43:05 nit: move : to next line, indent 4
noelallen1 2012/04/21 18:09:53 Done.
+ Mount(dev),
+ root_(NULL),
+ max_ino_(0) {
+}
- size_t Size() const {
- return stat_.st_size;
- }
+bool MountMem::Init(const std::string& args) {
+ root_ = AllocatePath(_S_IREAD | _S_IWRITE);
+ return (bool) (root_ != NULL);
+}
- void Resize(size_t size) {
- size_t cap = blocks_;
- size_t req = (size + s_blksize - 1) / s_blksize;
+void MountMem::Destroy() {
+ if (root_)
+ root_->Release();
binji 2012/04/16 22:43:05 set root_ to NULL
noelallen1 2012/04/21 18:09:53 Done.
+}
- if ((req > cap) || (req < (cap / 2))) {
- char *newdata = (char *) malloc(req * s_blksize);
- memcpy(newdata, data_, size);
- free(data_);
- stat_.st_size = size;
- data_ = newdata;
- blocks_ = req;
- }
+MountNode *MountMem::AllocatePath(int mode) {
binji 2012/04/16 22:43:05 nit: swap * side
noelallen1 2012/04/21 18:09:53 Done.
+ ino_t ino = AllocateINO();
+
+ // If we fail to initialize, let the smart pointer delete it
binji 2012/04/16 22:43:05 Where is the smart pointer?
noelallen1 2012/04/21 18:09:53 Done.
+ MountNode *ptr = new MountNodeDir(this, ino, dev_);
binji 2012/04/16 22:43:05 nit: swap * side
noelallen1 2012/04/21 18:09:53 Done.
+ if (!ptr->Init(mode, 1002, 1003)) {
binji 2012/04/16 22:43:05 what are 1002, 1003?
noelallen1 2012/04/21 18:09:53 Done.
+ ptr->Release();
+ FreeINO(ino);
+ return NULL;
}
+ return ptr;
+}
- int ino_;
- int ref_count_;
- struct stat stat_;
- void *data_;
- size_t blocks_;
- pthread_mutex_t lock_;
-};
+MountNode *MountMem::AllocateData(int mode) {
binji 2012/04/16 22:43:05 nit: swap * side
noelallen1 2012/04/21 18:09:53 Done.
+ ino_t ino = AllocateINO();
-MountMem::MountMem() {}
-MountMem::~MountMem() {}
-
-int MountMem::AddDirEntry(MountMemNode* dir_node, MountMemNode* obj_node, const char *name) {
- if (strlen(name > 255)) {
- errno = EINVAL;
- return -1;
+ // If we fail to initialize, let the smart pointer delete it
+ MountNode *ptr = new MountNodeMem(this, ino, dev_);
binji 2012/04/16 22:43:05 nit: swap * side
noelallen1 2012/04/21 18:09:53 Done.
+ if (!ptr->Init(mode, 1002, 1003)) {
+ ptr->Release();
+ FreeINO(ino);
+ return NULL;
}
+ return ptr;
+}
- struct dirent* d = (struct dirent *) dir_node->data_;
- size_t cnt = dir_node->Size() / sizeof(struct dirent);
- size_t off;
+void MountMem::ReleaseNode(MountNode* node) {
+ node->Release();
+}
- // Find a free location
- for (off = 0; off < cnt; off++) {
- if (d->d_name[0] == 0) break;
- d++;
+int MountMem::AllocateINO() {
+ if (inos_.size() == 0) {
+ max_ino_ += 8;
+ for (int a=0; a < 7; a++) {
binji 2012/04/16 22:43:05 nit: spaces around =
noelallen1 2012/04/21 18:09:53 Done.
+ inos_.push_back(max_ino_ - a);
+ }
}
+ return max_ino_ - 8;
binji 2012/04/16 22:43:05 How does this work? It doesn't look like anything
noelallen1 2012/04/21 18:09:53 Whoops. Fixed.
+}
- // Otherwise regrow and take the last spot.
- if (off == cnt) {
- dir_node->Resize(dir_node->Size() + sizeof(struct dirent));
- d = &((struct dirent *) dir_node->data_)[off];
- }
+void MountMem::FreeINO(int ino) {
+ inos_.push_back(ino);
+}
- strcpy(d->d_name, name);
- d->d_ino = obj_node->ino_;
- d->d_reclen = sizeof(dirent);
- d->d_off = off * sizeof(dirent);
+MountNode *MountMem::FindNode(const Path& path, int type) {
binji 2012/04/16 22:43:05 Can we assert in debug versions that the lock is h
noelallen1 2012/04/21 18:09:53 Unless we specialize the lock, it won't tell us if
+ MountNode *node = root_;
binji 2012/04/16 22:43:05 nit: swap * side
noelallen1 2012/04/21 18:09:53 Done.
- // Add a ref_count_ and link count for the directory
- obj_node->Acquire();
- obj_node->stat_.st_nlink++;
- return 0;
-}
+ // If there is no root there, we have an error
+ if (node == NULL) {
+ errno = ENOTDIR;
+ return NULL;
+ }
+ // We are expecting an "absolute" path from this mount point
+ if (!path.IsAbsolute()) {
+ errno = EINVAL;
+ return NULL;
+ }
+ for (size_t index = 1; node && index < path.Size(); index++) {
+ // If not a directory, then we have an error so return.
+ if (!node->IsaDir()) {
+ errno = ENOTDIR;
+ return NULL;
+ }
+ // Find the child node
+ node = node->FindChild(path.Part(index));
+ }
-int MountMem::DelDirEntry_locked(int dir_ino, const char *name) {
- MountMemNode* dir_node = inodes_.At(dir_ino);
- struct dirent* d = (struct dirent *) dir_node->data_;
- size_t cnt = dir_node->Size() / sizeof(struct dirent);
- size_t off = 0;
+ // If we can't find it, fail
+ if (NULL == node) return NULL;
- // Find a free location
- for (off = 0; off < cnt; off++) {
- if (!strcmp(d->d_name, name)) {
- d->d_name[0] = 0;
- obj_node->stat_.st_nlink--;
- if (0 == --obj_node->ref_count_) FreeNode(obj_node->ino_);
- dir_node->ref_count_--;
- return 0;
- }
+ // If a directory is expected, but it's not a directory, then fail
+ if ((type & _S_IFDIR) && !node->IsaDir()) {
+ errno = ENOTDIR;
+ return NULL;
}
- errno = ENOENT;
- return -1;
-}
+ // If a file is expected, but it's not a file, then fail
+ if ((type & _S_IFREG) && node->IsaDir()) {
+ errno = EISDIR;
+ return NULL;
+ }
-MountNodeMem* MountMem::AcquireNode(int ino) {
- SimpleAutoLock lock(&lock_);
- MountNodeMem *node = inodes_.At(ino);
- if (node) node->ref_count_++;
+ // We now have a valid directory object, so increment the ref count
binji 2012/04/16 22:43:05 Where do we increment a refcount?
noelallen1 2012/04/21 18:09:53 Bad comment, fixed.
+ // and return it.
return node;
}
-void MountMem::ReleaseNode(MountMemNode* node) {
- if (node) {
- SimpleAutoLock lock(&lock_);
- if (--node->ref_count_) inodes_.Free(node->ino_);
- }
-}
+MountNode *MountMem::Open(const Path& path, int mode) {
binji 2012/04/16 22:43:05 nit: swap * side
noelallen1 2012/04/21 18:09:53 Done.
+ AutoLock lock(&lock_);
+ MountNode* node = FindNode(path);
-void MountMem::Init(void) {
- int root = inodes_.Alloc();
+ if (NULL == node) {
+ // Now first find the parent directory to see if we can add it
+ MountNode* parent = FindNode(path.Parent(), _S_IFDIR);
+ if (NULL == parent) return NULL;
- assert(root == 0);
- AddDirEntry(root, root, "/");
-}
+ // If the node does not exist and we can't create it, fail
+ if ((mode & O_CREAT) == 0) return NULL;
-int MountMem::Mkdir(const std::string& path, mode_t mode, struct stat *buf) {
- SimpleAutoLock lock(&lock_);
+ // Otherwise, create it with a single refernece
+ mode = OpenModeToPermission(mode);
binji 2012/04/16 22:43:05 Is OpenModeToPermission new? I can't find it.
noelallen1 2012/04/21 18:09:53 A public static defined in the parent class.
+ node = AllocateData(mode);
+ if (NULL == node) return NULL;
binji 2012/04/16 22:43:05 nit: remove extra blank line
noelallen1 2012/04/21 18:09:53 Done.
- int ino = AcquireNode(path);
- // Make sure it doesn't already exist.
- child = GetMemNode(path);
- if (child) {
+ if (parent->AddChild(path.Filename(), node) == -1) {
+ // Or if it fails, release it
+ node->Release();
+ return NULL;
+ }
+ return node;
+ }
+
+ // If we were expected to create it exclusively, fail
+ if (mode & O_EXCL) {
errno = EEXIST;
- return -1;
+ return NULL;
}
- // Get the parent node.
- int parent_slot = GetParentSlot(path);
- if (parent_slot == -1) {
- errno = ENOENT;
- return -1;
- }
- parent = slots_.At(parent_slot);
- if (!parent->is_dir()) {
- errno = ENOTDIR;
- return -1;
- }
- // Create a new node
- int slot = slots_.Alloc();
- child = slots_.At(slot);
- child->set_slot(slot);
- child->set_mount(this);
- child->set_is_dir(true);
- Path p(path);
- child->set_name(p.Last());
- child->set_parent(parent_slot);
- parent->AddChild(slot);
- if (!buf) {
- return 0;
+ // Verify we got the requested permisions.
+ int req_mode = OpenModeToPermission(mode);
+ int obj_mode = node->GetMode() & OpenModeToPermission(_O_RDWR);
+ if ((obj_mode & req_mode) != req_mode) {
+ errno = EACCES;
+ return NULL;
}
- return Stat(slot, buf);
+ // We opened it, so ref count it before passing it back.
+ node->Acquire();
+ return node;
}
-int MountMem::Rmdir(int node) {
-}
-
-int MountMem::Chmod(int ino, int mode) {
- MountMemNode* node = AcquireNode(ino);
-
- if (NULL == node) {
- errno = BADF;
- return -1;
- }
-
- node->stat_.st_mode = mode;
+int MountMem::Close(MountNode* node) {
+ AutoLock lock(&lock_);
+ node->Close();
ReleaseNode(node);
return 0;
}
-int MountMem::Stat(int ino, struct stat *buf) {
- MountMemNode* node = AcquireNode(ino);
+int MountMem::Unlink(const Path& path) {
+ AutoLock lock(&lock_);
+ MountNode* parent = FindNode(path.Parent(), _S_IFDIR);
- if (NULL == node) {
- errno = BADF;
+ if (NULL == parent) return -1;
+
+ MountNode* child = parent->FindChild(path.Filename());
+ if (NULL == child) {
+ errno = ENOENT;
return -1;
}
-
- memcpy(buf, node->stat_, sizeof(struct stat));
- ReleaseNode(node);
- return 0;
-}
-
-int MountMem::Fsync(int ino) {
- // Acquire the node in case he node
- MountMemNode* node = AcquireNode(ino);
- if (node) {
- ReleaseNode(node);
- return 0
+ if (child->IsaDir()) {
+ errno = EISDIR;
+ return -1;
}
-
- errno = BADF;
- return -1;
+ return parent->RemoveChild(path.Filename());
}
-int MountMem::Getdents(int ino, off_t offset, struct dirent *dirp, unsigned int count) {
- MountMemNode* node = AcquireNode(ino);
- if ((NULL == node) == 0) {
- errno = EBADF;
+int MountMem::Mkdir(const Path& path, int mode) {
+ AutoLock lock(&lock_);
+
+ // We expect a Mount "absolute" path
+ if (!path.IsAbsolute()) {
+ errno = ENOENT;
return -1;
}
- if ((node->stat_.st_mode & S_IFDIR) == 0) {
- errno =ENOTDIR;
+ // The root of the mount is already created by the mount
+ if (path.Size() == 1) {
+ errno = EEXIST;
return -1;
}
- if (offset + count > node->Size()) {
- count = node->Size() - offset;
- }
+ MountNode* parent = FindNode(path.Parent(), _S_IFDIR);
+ MountNode* node;
- memcpy(dirp, &node->data_[offset], count);
- return count;
-}
+ // If we failed to find the parent, the error code is already set.
+ if (NULL == parent) return -1;
-ssize_t MountMem::Write(int ino, off_t offset, const void *buf, size_t count) {
- MountMemNode* node = AcquireNode(ino);
-
- if (NULL == node || (node->stat_.st_mode & S_IWUSER) == 0) {
- errno = EBADF;
+ node = parent->FindChild(path.Filename());
+ if (NULL != node) {
+ errno = EEXIST;
return -1;
}
- if (offset + count > node->Size()) {
- int err = node->Resize();
- if (err) {
- errno = err;
- ReleaseNode(node);
- return -1;
- }
+ // Otherwise, create a new node and attempt to add it
+ mode = OpenModeToPermission(mode);
+ node = AllocatePath(_S_IREAD | _S_IWRITE);
+ if (NULL == node) return -1;
+
+ if (parent->AddChild(path.Filename(), node) == -1) {
+ node->Release();
+ return -1;
}
- mempcy(&node->data_[offset], buf, count);
- ReleaseNode(node);
- return count;
+ node->Release();
binji 2012/04/16 22:43:05 Document this release
noelallen1 2012/04/21 18:09:53 Done.
+ return 0;
}
-ssize_t MountMem::Read(int ino, off_t offset, const void *buf, size_t count) {
- MountMemNode* node = AcquireNode(ino);
+int MountMem::Rmdir(const Path& path) {
+ AutoLock lock(&lock_);
- if (NULL == node || (node->stat_.st_mode & S_IRUSER) == 0) {
- errno = EBADF;
+ // We expect a Mount "absolute" path
+ if (!path.IsAbsolute()) {
+ errno = ENOENT;
return -1;
}
- if (offset + count > node->Size()) {
- count = node->Size() - offset;
+ // The root of the mount is already created by the mount
+ if (path.Size() == 1) {
+ errno = EEXIST;
+ return -1;
}
- mempcy(buf, &node->data_[offset], count);
- ReleaseNode(node);
- return count;
-}
+ MountNode* parent = FindNode(path.Parent(), _S_IFDIR);
+ MountNode* node;
-int MountMem::Isatty(int ino) {
- // Acquire the node in case he node array is in flux
- MountMemNode* node = AcquireNode(ino);
+ // If we failed to find the parent, the error code is already set.
+ if (NULL == parent) return -1;
- if (node) {
- errno = ENOTTY;
- ReleaseNode(node);
+ // Verify we find a child which is also a directory
+ node = parent->FindChild(path.Filename());
+ if (NULL == node) {
+ errno = ENOENT;
+ return -1;
}
- else {
- errno = BADF;
+ if (!node->IsaDir()) {
+ errno = ENOTDIR;
+ return -1;
}
- return 0;
+ if (node->ChildCount() > 0) {
+ errno = ENOTEMPTY;
+ return -1;
+ }
+ return parent->RemoveChild(path.Filename());
}
-

Powered by Google App Engine
This is Rietveld 408576698