Skip to content

Commit e8e5530

Browse files
committed
Add high level filesystem documention
From nextcloud/server#26982 Adds some documentation for the filesystem layer, both a high level overview of how the various pieces interact and a high level guide for apps interacting with the filesystem. Hopefully this will be useful to anyone trying to either use the filesystem or work on the filesystem. Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent 15c3ff2 commit e8e5530

File tree

2 files changed

+235
-19
lines changed

2 files changed

+235
-19
lines changed

developer_manual/basics/storage/filesystem.rst

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,78 @@
11
==========
2-
Filesystem
2+
Nextcloud filesystem API
33
==========
44

55
.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>
66

7-
Because users can choose their storage backend, the filesystem should be accessed by using the appropriate filesystem classes.
7+
High level guide to using the Nextcloud filesystem API.
8+
9+
Because users can choose their storage backend, the filesystem should be accessed by using the appropriate filesystem classes. For a simplified filesystem for app specific data see `IAppData <appdata.html>`_
10+
11+
Node API
12+
^^^^^^^^
13+
14+
The "Node API" is the primary api for apps to access the Nextcloud filesystem, each item in the filesystem is
15+
represented as either a File or Folder node with each node providing access to the relevant filesystem information
16+
and actions for the node.
17+
18+
19+
Getting access
20+
--------------
21+
22+
Access to the filesystem is provided by the ``IRootFolder`` which can be injected into your class.
23+
From the root folder you can either access a user's home folder or access a file or folder by its absolute path.
24+
25+
.. code-block:: php
26+
27+
use OCP\Files\IRootFolder;
28+
use OCP\IUserSession;
29+
30+
class FileSystemAccessExample {
31+
private IUserSession $userSession;
32+
private IRootFolder $rootFolder;
33+
34+
public function __constructor(IUserSession $userSession, IRootFolder $rootFolder) {
35+
$this->userSession = $userSession;
36+
$this->rootFolder = $rootFolder;
37+
}
38+
39+
/**
40+
* Create a new file with specified content in the home folder of the current user
41+
* returning the size of the resulting file.
42+
*/
43+
public function getCurrentUserFolder(string $path, string $content): int {
44+
$user = $this->userSession->getUser();
45+
46+
if ($user === null) {
47+
return null;
48+
}
49+
50+
// the "user folder" corresponds to the root of the user visible files
51+
return $this->rootFolder->getUserFolder($user->getUID());
52+
}
53+
}
54+
55+
For more details on the specific methods provided by file and folder nodes see the method documentation from the ``OCP\Files\File`` and ``OCP\Files\Folder`` interfaces.
856

9-
Filesystem classes can be injected automatically with dependency injection. This is the user filesystem.
10-
For a simplified filestystem for app specific data see `IAppData <appdata.html>`_
1157

1258
Writing to a file
1359
-----------------
1460

15-
1661
All methods return a Folder object on which files and folders can be accessed, or filesystem operations can be performed relatively to their root. For instance for writing to file:`nextcloud/data/myfile.txt` you should get the root folder and use:
1762

1863
.. code-block:: php
1964
20-
<?php
21-
namespace OCA\MyApp\Storage;
22-
2365
use OCP\Files\IRootFolder;
2466
25-
class AuthorStorage {
67+
class FileWritingExample {
2668
27-
/** @var IRootStorage */
28-
private $storage;
69+
private IRootStorage $storage;
2970
3071
public function __construct(IRootFolder $storage){
3172
$this->storage = $storage;
3273
}
3374
34-
public function writeTxt($content) {
75+
public function writeContentToFile($content) {
3576
3677
$userFolder = $this->storage->getUserFolder('myUser');
3778
@@ -54,22 +95,19 @@ All methods return a Folder object on which files and folders can be accessed, o
5495
}
5596
}
5697
98+
5799
Reading from a file
58100
-------------------
59101

60102
Files and folders can also be accessed by id, by calling the **getById** method on the folder.
61103

62104
.. code-block:: php
63105
64-
<?php
65-
namespace OCA\MyApp\Storage;
66-
67106
use OCP\Files\IRootFolder;
68107
69-
class AuthorStorage {
108+
class FileReadingExample {
70109
71-
/** @var IRootFolder */
72-
private $storage;
110+
private IRootFolder $storage;
73111
74112
public function __construct(IRootFolder $storage){
75113
$this->storage = $storage;
@@ -82,7 +120,7 @@ Files and folders can also be accessed by id, by calling the **getById** method
82120
// check if file exists and read from it if possible
83121
try {
84122
$file = $userFolder->getById($id);
85-
if($file instanceof \OCP\Files\File) {
123+
if ($file instanceof \OCP\Files\File) {
86124
return $file->getContent();
87125
} else {
88126
throw new StorageException('Can not read from folder');
@@ -92,3 +130,36 @@ Files and folders can also be accessed by id, by calling the **getById** method
92130
}
93131
}
94132
}
133+
134+
135+
Direct storage access
136+
---------------------
137+
138+
While it should be generally avoided in favor of the higher level apis,
139+
sometimes an app needs to talk directly to the storage implementation of it's metadata cache.
140+
141+
You can get access to the underlying storage of a file or folder by calling ``getStorage`` on the node or first getting
142+
the mountpoint by calling ``getMountPoint`` and getting the storage from there.
143+
144+
Once you have the storage instance you can use the storage api from ``OCP\Files\Storage\IStorage``, note however that
145+
all paths used in the storage api are internal to the storage, the ``IMountPoint`` returned from ``getMountPoint`` provides
146+
methods for translating between absolute filesystem paths and internal storage paths.
147+
148+
If you need to query the cached metadata directory you can get the ``OCP\Files\Cache\ICache`` from the storage by calling ``getCache`.
149+
150+
Implementing a storage
151+
----------------------
152+
153+
The recommended way for implementing a storage backend is by sub-classing ``OC\Files\Storage\Common`` which provides
154+
fallback implementations for various methods, reducing the amount of work required to implement the full storage api.
155+
Note however that various of these fallback implementations are likely to be significantly less efficient than an
156+
implementation of the method optimized for the abilities of the storage backend.
157+
158+
Adding mounts to the filesystem
159+
-------------------------------
160+
161+
The recommended way of adding your own mounts to the filesystem from an app is implementing ``OCP\Files\Config\IMountProvider``
162+
and registering the provider using ``OCP\Files\Config\IMountProviderCollection::registerProvider``.
163+
164+
Once registered, your provider will be called every time the filesystem is being setup for a user and your mount provider
165+
can return a list of mounts to add for that user.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
============
2+
Nextcloud filesystem API
3+
============
4+
5+
High level overview
6+
-------------------
7+
8+
The Nextcloud filesystem is roughly based on the unix filesystem, consisting of multiple storages
9+
mounted at various locations.
10+
11+
.. code-block:: txt
12+
13+
┌──────────────────────────────────┐
14+
│Code wanting to use the filesystem│
15+
└─────────┬─────────────────────┬──┘
16+
│ │
17+
│ │
18+
┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
19+
╎Filesystem │ │ ╎
20+
╎layer │new │legacy ╎
21+
╎ │ │ ╎
22+
╎ ▼ ▼ ╎
23+
╎ ┌────────┐ Partly build on ┌─┴──────┐ ╎
24+
╎ │Node API├─────────────────►│View API│ ╎
25+
╎ └───────┬┘ └─┬──────┘ ╎
26+
╎ │ │ ╎
27+
└╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┘
28+
│ │
29+
┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
30+
╎Storage layer │ │ ╎
31+
╎ ├─────────────────────┤ ╎
32+
╎ │ │ ╎
33+
╎ ▼ ▼ ╎
34+
╎ ┌───────┐ ┌───────┐ ┌──────┐ ╎
35+
╎ │Storage│═══>│Scanner│═══>│Cache │ ╎
36+
╎ └───────┘ └───────┘ └──────┘ ╎
37+
╎ ╎
38+
╎ ╎
39+
└╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┘
40+
41+
Filesystem layer
42+
^^^^^^^^^^^^^^^^
43+
44+
Any code that wants to use the filesystem has two API options to use, the new ``Node`` api and the old ``View`` api.
45+
New code should preferably use the ``Node`` api as it allows building systems with less overhead than the old api.
46+
47+
Besides the filesystem apis, this layer also manages the available mounts, containing the logic to allow apps
48+
to setup their mounts and translating filesystem paths into a mountpoint + "internal" path.
49+
50+
### Storage layer
51+
52+
The storage implementation handles the details of communicating with the filesystem or remote storage api
53+
and provide a uniform api for Nextcloud to use the storage.
54+
55+
For each storage a metadata cache/index is maintained to allow reading metadata of the storage without having
56+
to talk to the (potentially) slow storage backend. The scanner is responsible for updating the cache with
57+
information from the storage backend.
58+
59+
## Storage/Cache wrappers
60+
61+
To allow apps to customize the behavior of a storage without requiring the app to implement this for every
62+
possible storage backend, a ``Wrapper`` system is used.
63+
64+
A ``Wrapper`` encapsulates an inner storage and allows overwriting any method to customize its behavior, with
65+
all other methods being passed through to the inner storage.
66+
67+
Generally search storage wrapper has an equivalent cache wrapper encapsulating the cache of the inner storage
68+
to provide the same behavior modifications when reading metadata from the cache.
69+
70+
Wrappers can be layered to stack the behavior of the wrappers, for example the ``groupfolders`` app works by
71+
stacking a wrapper to provide access to a single folder on the root storage with a wrapper to limit the permissions
72+
of the storage.
73+
74+
.. code-block:: txt
75+
76+
┌───────────────┐ ┌────────────────────┐
77+
│PermissionsMask├─────►│CachePermissionsMask│ PermissionsMask applies a mask to the permissions of a storage
78+
└───────┬───────┘ └─────────┬──────────┘ to provide less-privilaged access to a storage
79+
│ │
80+
▼ ▼
81+
┌───────────────┐ ┌────────────────────┐
82+
│Jail ├─────►│CacheJail │ Jail restricts access to a file or folder of a storage providing
83+
└───────┬───────┘ └─────────┬──────────┘ a limited view into the storage (think unix chroot or bind mount)
84+
│ │
85+
▼ ▼
86+
┌───────────────┐ ┌────────────────────┐
87+
│Base Storage ├─────►│Base Cache │
88+
└───────────────┘ └────────────────────┘
89+
Code Map
90+
--------
91+
92+
Approximate overview of the significant filesystem code
93+
94+
AppData
95+
~~~~~~~
96+
97+
High level api for accessing "appdata" folders, based on the ``Node`` API
98+
99+
Cache
100+
~~~~~
101+
102+
- ``Cache`` implementation
103+
- Cache wrappers
104+
- Scanner and cache update logic
105+
- Search infrastructure
106+
107+
Mount
108+
~~~~~
109+
110+
Mountpoint management and setup
111+
112+
Node
113+
~~~~
114+
115+
``Node`` filesystem api implementation
116+
117+
ObjectStorage
118+
~~~~~~~~~~~~~
119+
120+
Implementation of the various supported object store storage backends
121+
122+
SimpleFS
123+
~~~~~~~~
124+
125+
Simplified version of the Node api, for providing a more limited api for some filesystem bits
126+
127+
Storage
128+
~~~~~~~
129+
130+
Implementation of various storage backends and wrappers
131+
132+
Streams
133+
~~~~~~~
134+
135+
Various low-level php stream wrapper used in storage implementations
136+
137+
Type
138+
~~~~
139+
140+
Mimetype management and detection
141+
142+
View.php
143+
~~~~~~~~
144+
145+
Legacy View api

0 commit comments

Comments
 (0)