forked from facebookarchive/nfusr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InodeInternal.h
76 lines (68 loc) · 1.95 KB
/
InodeInternal.h
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
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include <cassert>
#include <cstring>
#include <nfsc/libnfs.h>
#include <nfsc/libnfs-raw.h>
#include <nfsc/libnfs-raw-nfs.h>
#include "logger.h"
/// @brief internal representation of an inode.
///
/// Currently really just a reference counted NFS file handle.
///
/// NB: the only reason to roll my own ref counting rather than
/// use shared_ptr is that I cheat like a bastard and stuff pointers
/// to inodes in fuse_ino_t, which can't hold a shared_ptr instance.
class InodeInternal {
public:
InodeInternal(
const nfs_fh3* fh,
std::shared_ptr<std::string> local_cache_path)
: refcount_(1), local_cache_path_(local_cache_path) {
fh_.data.data_val = new char[fh->data.data_len];
fh_.data.data_len = fh->data.data_len;
::memcpy(fh_.data.data_val, fh->data.data_val, fh_.data.data_len);
local_cache_path_ = local_cache_path;
}
void ref() {
assert(refcount_ >= 0);
++refcount_;
};
void deref(nfusr::Logger *logger = nullptr, const char *caller = nullptr) {
assert(refcount_ > 0);
if (--refcount_ == 0) {
if (logger && caller) {
logger->LOG_MSG(LOG_DEBUG, "%s deleting inode %p.\n", caller, this);
}
delete this;
}
};
const nfs_fh3& getFh() const {
return fh_;
};
bool hasLocalCachePath() {
if (local_cache_path_) {
return true;
} else {
return false;
}
}
std::shared_ptr<std::string> getLocalCachePath() {
return local_cache_path_;
};
private:
nfs_fh3 fh_;
~InodeInternal() {
assert(refcount_ == 0);
delete[] fh_.data.data_val;
}
std::atomic_int refcount_;
std::shared_ptr<std::string> local_cache_path_;
};