Belle II Software development
FileSystem.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/utilities/FileSystem.h>
9#include <framework/utilities/ScopeGuard.h>
10
11#include <gtest/gtest.h>
12
13#include <sys/wait.h>
14
15#include <unistd.h>
16#include <filesystem>
17
18using namespace std;
19using namespace Belle2;
20namespace fs = std::filesystem;
21
22namespace {
23 TEST(Utilities, TemporaryFile)
24 {
25 std::string filename;
26 {
28 filename = temp.getName();
29 ASSERT_TRUE(temp.is_open());
30 ASSERT_TRUE(fs::exists(filename));
31 }
32 ASSERT_FALSE(fs::exists(filename));
33 }
34 TEST(Utilities, FileSystem)
35 {
36 std::string filename;
37 {
39 filename = temp.getName();
40 ASSERT_TRUE(FileSystem::fileExists(filename));
41 ASSERT_FALSE(FileSystem::isDir(filename));
42 ASSERT_TRUE(FileSystem::fileDirExists(filename));
43
44 {
45 //check relative filenames
46 auto pwd = ScopeGuard::guardWorkingDirectory(fs::temp_directory_path()); //don't influence other tests by changing pwd
47 std::string relname = fs::relative(filename, fs::temp_directory_path()).string();
48 ASSERT_TRUE(FileSystem::fileExists(relname));
49 chdir("/");
50 ASSERT_FALSE(FileSystem::fileExists(relname));
51 }
52 }
53 ASSERT_FALSE(FileSystem::fileExists(filename));
54 ASSERT_FALSE(FileSystem::isDir(filename));
55 ASSERT_TRUE(FileSystem::fileDirExists(filename));
56
57 ASSERT_TRUE(FileSystem::isDir("/"));
58 }
59 TEST(Utilities, Lock)
60 {
62 std::string filename = temp.getName();
63
64 //check 0-timeout locks work
65 FileSystem::Lock lk(filename);
66 EXPECT_TRUE(lk.lock(0));
67
68 if (pid_t pid = fork()) {
69 //mother process
70
71 //NOTE: in same process, the lock is NOT exclusive!
72 EXPECT_TRUE(lk.lock(0));
73 int status = 0;
74 waitpid(pid, &status, 0);
75 EXPECT_TRUE(WIFEXITED(status));
76 EXPECT_EQ(0, WEXITSTATUS(status));
77 } else {
78 //child process
79 //do not use an ASSERT_TRUE or something here, or we'll run tests twice
80 if (lk.lock(0))
81 exit(1);
82
83 FileSystem::Lock lk2(filename);
84 if (lk2.lock(0))
85 exit(1);
86
87 exit(0);
88 }
89 }
90} // namespace
Helper class for locking a file.
Definition: FileSystem.h:97
Helper file to create a temporary file and ensure deletion if object goes out of scope.
Definition: FileSystem.h:128
std::string getName() const
get filename of the temporary file
Definition: FileSystem.h:141
Utility functions related to filename validation and filesystem access.
Definition: FileSystem.h:20
static bool fileDirExists(const std::string &filename)
Check if the dir containing the filename exists.
Definition: FileSystem.cc:38
static bool isDir(const std::string &filename)
Check if filename points to an existing directory.
Definition: FileSystem.cc:51
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
Definition: FileSystem.cc:32
static ScopeGuard guardWorkingDirectory()
Create a ScopeGuard of the current working directory.
Definition: ScopeGuard.h:296
Abstract base class for different kinds of events.
STL namespace.