aboutsummaryrefslogtreecommitdiff
path: root/src/operations.cc
blob: 2eb326acc28e6044b41330fe682fbb850f6781e6 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
// Copyright (C) 2011  Sebastian Pipping <sebastian@pipping.org>
// Copyright (C) 2016  Benjamin Barenblat <benjamin@barenblat.name>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program.  If not, see <http://www.gnu.org/licenses/>.

#define BSD_SOURCE
#define _POSIX_C_SOURCE 201602L

#include "operations.h"

#include <cerrno>
#include <cstring>
#include <memory>
#include <new>

#define FUSE_USE_VERSION 26
#include <dirent.h>
#include <fuse.h>
#include <glog/logging.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "utility.h"

namespace scoville {

namespace {

int root_fd_;

struct Directory {
  DIR* fd;
  dirent* entry;
  off_t offset;
};

Directory* FileInfoDirectory(fuse_file_info* const file_info) {
  return reinterpret_cast<Directory*>(file_info->fh);
}

mode_t DirectoryTypeToFileType(const unsigned char type) {
  switch (type) {
    case DT_BLK:
      return S_IFBLK;
    case DT_CHR:
      return S_IFCHR;
    case DT_DIR:
      return S_IFDIR;
    case DT_FIFO:
      return S_IFIFO;
    case DT_LNK:
      return S_IFLNK;
    case DT_REG:
      return S_IFREG;
    case DT_SOCK:
      return S_IFSOCK;
    default:
      return 0;
  }
}

#define RETURN_ON_ERROR(call) \
  if ((call) == -1) {         \
    return -errno;            \
  }                           \
  do {                        \
  } while (false)

void* Initialize(fuse_conn_info*) {
  LOG(INFO) << "initialize";
  return nullptr;
}

void Destroy(void*) {
  LOG(INFO) << "destroy";
  LOG_IF(ERROR, close(root_fd_) == -1) << "could not close root FD: "
                                       << ErrnoText();
}

int Getattr(const char* const path, struct stat* output) {
  LOG(INFO) << "getattr(" << path << ")";

  if (path[0] == '\0') {
    LOG(ERROR) << "getattr called on path not starting with /";
    return -ENOENT;
  }

  if (strcmp(path, "/") == 0) {
    // They're asking for information about the mount point.
    RETURN_ON_ERROR(fstat(root_fd_, output));
    return 0;
  }

  // Trim the leading slash so fstatat will treat it relative to root_fd_.
  LOG(INFO) << "getattr: trimming leading slash";
  RETURN_ON_ERROR(fstatat(root_fd_, path + 1, output, AT_SYMLINK_NOFOLLOW));
  return 0;
}

int Opendir(const char* const path, fuse_file_info* const file_info) {
  LOG(INFO) << "opendir(" << path << ")";

  std::unique_ptr<Directory> directory;
  try {
    directory.reset(new Directory);
  } catch (std::bad_alloc) {
    return -ENOMEM;
  }

  if ((directory->fd = opendir(path)) == nullptr) {
    return -errno;
  }
  directory->entry = nullptr;
  directory->offset = 0;

  static_assert(sizeof(file_info->fh) == sizeof(uintptr_t),
                "FUSE file handles are a different size than pointers");
  file_info->fh = reinterpret_cast<uintptr_t>(directory.release());
  return 0;
}

int Readdir(const char*, void* const buffer, fuse_fill_dir_t filler,
            const off_t offset, fuse_file_info* const file_info) {
  LOG(INFO) << "readdir";

  Directory* const directory = FileInfoDirectory(file_info);

  if (offset != directory->offset) {
    seekdir(directory->fd, offset);
    directory->entry = nullptr;
    directory->offset = offset;
  }

  while (true) {
    if (!directory->entry) {
      directory->entry = readdir(directory->fd);
      if (!directory->entry) {
        break;
      }
    }

    struct stat stats;
    std::memset(&stats, 0, sizeof(stats));
    stats.st_ino = directory->entry->d_ino;
    stats.st_mode = DirectoryTypeToFileType(directory->entry->d_type);
    const off_t next_offset = telldir(directory->fd);
    if (filler(buffer, directory->entry->d_name, &stats, next_offset)) {
      break;
    }

    directory->entry = nullptr;
    directory->offset = next_offset;
  }

  return 0;
}

int Releasedir(const char*, fuse_file_info* const file_info) {
  LOG(INFO) << "releasedir";
  Directory* const directory = FileInfoDirectory(file_info);
  closedir(directory->fd);
  delete directory;
  return 0;
}

#undef RETURN_ON_ERROR

}  // namespace

fuse_operations FuseOperations(const int root_fd) {
  root_fd_ = root_fd;

  fuse_operations result;

  result.flag_nullpath_ok = true;
  result.flag_nopath = true;
  result.flag_utime_omit_ok = true;

  result.init = &Initialize;
  result.destroy = &Destroy;

  result.getattr = &Getattr;
  result.opendir = &Opendir;
  result.readdir = &Readdir;
  result.releasedir = &Releasedir;

  return result;
}

}  // namespace scoville