Home | History | Annotate | Download | only in nacl_io
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef LIBRARIES_NACL_IO_KERNEL_HANDLE_H_
      6 #define LIBRARIES_NACL_IO_KERNEL_HANDLE_H_
      7 
      8 #include <pthread.h>
      9 
     10 #include "nacl_io/error.h"
     11 #include "nacl_io/mount.h"
     12 #include "nacl_io/mount_node.h"
     13 #include "nacl_io/ostypes.h"
     14 
     15 #include "sdk_util/macros.h"
     16 #include "sdk_util/ref_object.h"
     17 #include "sdk_util/scoped_ref.h"
     18 #include "sdk_util/simple_lock.h"
     19 
     20 namespace nacl_io {
     21 
     22 // KernelHandle provides a reference counted container for the open
     23 // file information, such as it's mount, node, access type and offset.
     24 // KernelHandle can only be referenced when the KernelProxy lock is held.
     25 class KernelHandle : public sdk_util::RefObject {
     26  public:
     27   KernelHandle();
     28   KernelHandle(const ScopedMount& mnt, const ScopedMountNode& node);
     29   ~KernelHandle();
     30 
     31   Error Init(int open_flags);
     32 
     33   // Assumes |out_offset| is non-NULL.
     34   Error Seek(off_t offset, int whence, off_t* out_offset);
     35 
     36   // Dispatches Read, Write, GetDents to atomically update offs_.
     37   Error Read(void* buf, size_t nbytes, int* bytes_read);
     38   Error Write(const void* buf, size_t nbytes, int* bytes_written);
     39   Error GetDents(struct dirent* pdir, size_t count, int* bytes_written);
     40 
     41   const ScopedMountNode& node() { return node_; }
     42   const ScopedMount& mount() { return mount_; }
     43 
     44 private:
     45   ScopedMount mount_;
     46   ScopedMountNode node_;
     47   sdk_util::SimpleLock offs_lock_;
     48   size_t offs_;
     49 
     50   friend class KernelProxy;
     51   DISALLOW_COPY_AND_ASSIGN(KernelHandle);
     52 };
     53 
     54 typedef sdk_util::ScopedRef<KernelHandle> ScopedKernelHandle;
     55 
     56 }  // namespace nacl_io
     57 
     58 #endif  // LIBRARIES_NACL_IO_KERNEL_HANDLE_H_
     59