summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/libraries/nacl_mounts/mount.h
blob: e1da7259add1822241e1a0229b3500ad15618ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Copyright (c) 2012 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef LIBRARIES_NACL_MOUNTS_MOUNT_H_
#define LIBRARIES_NACL_MOUNTS_MOUNT_H_

#include <map>
#include <string>

#include "nacl_mounts/inode_pool.h"
#include "nacl_mounts/mount_node.h"
#include "nacl_mounts/path.h"
#include "utils/macros.h"
#include "utils/ref_object.h"

class MountNode;
class PepperInterface;

typedef std::map<std::string, std::string> StringMap_t;


class Mount : public RefObject {
 protected:
  // The protected functions are only used internally and will not
  // acquire or release the mount's lock.
  Mount();
  virtual ~Mount();

  // Init must be called by the factory before the mount is used.
  // This function must assign a root node, or replace FindNode.
  // |ppapi| can be NULL. If so, this mount cannot make any pepper calls.
  virtual bool Init(int dev, StringMap_t& args, PepperInterface* ppapi);
  virtual void Destroy();

 public:
  template <class M>
  static Mount* Create(int dev, StringMap_t& args, PepperInterface* ppapi);

  PepperInterface* ppapi() { return ppapi_; }

  // All paths are expected to containing a leading "/"
  void AcquireNode(MountNode* node);
  void ReleaseNode(MountNode* node);

  // Open a node at |path| with the specified open flags. The resulting
  // MountNode is created with a ref count of 1.
  virtual MountNode *Open(const Path& path, int o_flags) = 0;

  // Unlink, Mkdir, Rmdir will affect the both the RefCount
  // and the nlink number in the stat object.
  virtual int Unlink(const Path& path) = 0;
  virtual int Mkdir(const Path& path, int permissions) = 0;
  virtual int Rmdir(const Path& path) = 0;
  virtual int Remove(const Path& path) = 0;

  // Convert from R,W,R/W open flags to STAT permission flags
  static int OpenModeToPermission(int mode);

  void OnNodeCreated(MountNode* node) ;
  void OnNodeDestroyed(MountNode* node);

 protected:
  // Device number for the mount.
  int dev_;
  PepperInterface* ppapi_;  // Weak reference.
  INodePool inode_pool_;

 private:
  // May only be called by the KernelProxy when the Kernel's
  // lock is held, so we make it private.
  friend class KernelObject;
  friend class KernelProxy;
  void Acquire() { RefObject::Acquire(); }
  bool Release() { return RefObject::Release(); }

  DISALLOW_COPY_AND_ASSIGN(Mount);
};


template <class M>
/*static*/
Mount* Mount::Create(int dev, StringMap_t& args, PepperInterface* ppapi) {
  Mount* mnt = new M();
  if (mnt->Init(dev, args, ppapi) == false) {
    delete mnt;
    return NULL;
  }
  return mnt;
}

#endif  // LIBRARIES_NACL_MOUNTS_MOUNT_H_