Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
StorageHelper.h
1/*
2* If not stated otherwise in this file or this component's LICENSE file the
3* following copyright and licenses apply:
4*
5* Copyright 2020 Sky UK
6*
7* Licensed under the Apache License, Version 2.0 (the "License");
8* you may not use this file except in compliance with the License.
9* You may obtain a copy of the License at
10*
11* http://www.apache.org/licenses/LICENSE-2.0
12*
13* Unless required by applicable law or agreed to in writing, software
14* distributed under the License is distributed on an "AS IS" BASIS,
15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16* See the License for the specific language governing permissions and
17* limitations under the License.
18*/
19/*
20 * File: Storage.h
21 *
22 */
23#ifndef STORAGE_HELPER_H
24#define STORAGE_HELPER_H
25
26#include <Logging.h>
27
28#include <sys/types.h>
29#include <netinet/in.h>
30#include <unistd.h>
31#include <string>
32#include <memory>
33#include <map>
34#include <mutex>
35#include <list>
36
37//#define ENABLE_TESTS 1
38
45{
46public:
47 static int loopDeviceAssociate(int fileFd, std::string* loopDevPath);
48 static int openLoopDevice(std::string* loopDevice);
49 static bool attachFileToLoopDevice(int loopFd, int fileFd);
50 static int attachLoopDevice(const std::string& sourceFile,
51 std::string* loopDevice);
52 static bool createFileIfNeeded(const std::string& filePath,
53 const size_t storageSize,
54 const uid_t userId,
55 const std::string& fileSystem);
56 static void cleanMountLostAndFound(const std::string& mountPoint,
57 const std::string& logTag);
58 static std::string getLoopDevice(const std::string &backingFile);
59 static int getMountOptions(const std::list<std::string> &mountOptions);
60
61 // -------------------------------------------------------------------------
81 static bool rmdirRecursive(int dirFd, const std::string& path);
82
83 // -------------------------------------------------------------------------
104 static bool rmdirContents(int dirFd, const std::string& path);
105 static bool deleteRecursive(int dirfd, int depth);
106
107 // Tests
108#ifdef ENABLE_TESTS
109 static bool Test_mkdirRecursive(const std::string& rootfsPath);
110 static bool Test_openLoopDevice();
111 static bool Test_attachLoopDevice(std::string& imagePath);
112 static bool Test_cleanMountLostAndFound(const std::string& rootfsPath);
113 static bool Test_checkWriteReadMount(const std::string& tmpPath);
114#endif // ENABLE_TESTS
115
116};
117
118#endif // !defined(STORAGE_HELPER_H)
Help functions for Storage related things.
Definition StorageHelper.h:45
static bool rmdirRecursive(int dirFd, const std::string &path)
Removes a directory and all it's contents.
Definition StorageHelper.cpp:490
static bool attachFileToLoopDevice(int loopFd, int fileFd)
Attempts to attach the file to the loop device.
Definition StorageHelper.cpp:146
static int attachLoopDevice(const std::string &sourceFile, std::string *loopDevice)
Attaches the given file to an available loop device.
Definition StorageHelper.cpp:228
static bool rmdirContents(int dirFd, const std::string &path)
Removes the contents of a directory but leave the actual directory in place.
Definition StorageHelper.cpp:531
static void cleanMountLostAndFound(const std::string &mountPoint, const std::string &logTag)
Logs and deletes any files found in the lost+found directory of the mount point.
Definition StorageHelper.cpp:329
static bool createFileIfNeeded(const std::string &filePath, const size_t storageSize, const uid_t userId, const std::string &fileSystem)
Checks if file exists, if yes return true, if not try to create it. If success return true,...
Definition StorageHelper.cpp:273
static int openLoopDevice(std::string *loopDevice)
Attempts to open an available loop device.
Definition StorageHelper.cpp:60
static bool deleteRecursive(int dirfd, int depth)
Recursive function that deletes everything within the supplied directory (as a descriptor).
Definition StorageHelper.cpp:564
static std::string getLoopDevice(const std::string &backingFile)
Get the loop device for a given backing file.
Definition StorageHelper.cpp:420
static int loopDeviceAssociate(int fileFd, std::string *loopDevPath)
Associates a give file descriptor with a loop device.
Definition StorageHelper.cpp:197