File: DataDirLocater.h

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (189 lines) | stat: -rw-r--r-- 5,199 bytes parent folder | download
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef DATA_DIR_LOCATER_H
#define DATA_DIR_LOCATER_H

#include <string>
#include <vector>

struct DataDir
{
	/**
	 * @brief construct a data directory object
	 *
	 * Appends a slash to the end of the path if there isn't one already.
	 */
	DataDir(const std::string& path);

	std::string path;
	bool writable;
};

class DataDirLocater
{
public:
	static DataDirLocater& GetInstance();
	static void FreeInstance();

	/**
	 * @brief construct a data directory locater
	 *
	 * Does not locate data directories, use LocateDataDirs() for that.
	 */
	DataDirLocater();

	/**
	 * @brief locate spring data directories
	 *
	 * Attempts to locate a writable data dir, and then tries to
	 * chdir to it.
	 * As the writable data dir will usually be the current dir already under
	 * windows, the chdir will have no effect.
	 *
	 * The first dir added will be the writable data dir.
	 * @see Manpage
	 */
	void LocateDataDirs();

	/**
	 * @brief call after LocateDataDirs()
	 */
	void Check();

	const std::vector<DataDir>& GetDataDirs() const;
	const DataDir* GetWriteDir() const { return writeDir; }

	/**
	 * @brief returns the highest priority writable directory, aka the writedir
	 */
	std::string GetWriteDirPath() const;
	std::vector<std::string> GetDataDirPaths() const;

	/**
	 * Returns whether isolation-mode is enabled.
	 * In isolation-mode, we will only use a singel data-dir.
	 * This defaults to false, but can be set to true by setting the env var
	 * SPRING_ISOLATED.
	 * @see #GetIsolationModeDir
	 */
	bool IsIsolationMode() const { return isolationMode; }

	/**
	 * Determines whether we are in portable mode.
	 * It defines portable mode as:
	 * The spring binary (spring binary), the unitsync
	 * and springsettings.cfg are in the same directory.
	 */
	static bool IsPortableMode();

	/**
	 * Sets whether isolation-mode is enabled.
	 * @see #IsIsolationMode
	 * @see #SetIsolationModeDir
	 */
	void SetIsolationMode(bool enabled) { isolationMode = enabled; }

	/**
	 * Returns the isolation-mode directory, or "", if the default one is used.
	 * The default one is CWD or CWD/.., in case of a versioned data-dir.
	 * If the env var SPRING_ISOLATED is set to a valid directory,
	 * it replaced the above mentioned default.
	 * This is only relevant if isolation-mode is active.
	 * @see #IsIsolationMode
	 */
	std::string GetIsolationModeDir() const { return isolationModeDir; }

	/**
	 * Sets the isolation-mode directory.
	 * If set to "", we use the default one, which is is CWD or CWD/..,
	 * in case of a versioned data-dir.
	 * This is only relevant if isolation-mode is active.
	 * @see #SetIsolationMode
	 */
	void SetIsolationModeDir(const std::string& dir) { isolationModeDir = dir; }

	/**
	 * Force the write directory.
	 * If set to "", we use the default one.
	 */
	void SetWriteDir(const std::string& dir) { forcedWriteDir = dir; }

	/**
	 * @brief reads envvar to detect if we should run in isolated mode
	 * used by unitsync.cpp
	 */
	void UpdateIsolationModeByEnvVar();

	/**
	 * @brief cd's into writedir
	 */
	void ChangeCwdToWriteDir();

	void CreateCacheDir(const std::string& cacheDir);

private:
	void AddCurWorkDir();
	void AddPortableDir();
	void AddHomeDirs();
	void AddEtcDirs();
	void AddShareDirs();

	/**
	 * @brief substitutes environment variables with their values
	 */
	std::string SubstEnvVars(const std::string& in) const;
	/**
	 * @brief Adds the directories from a colon separated string to the data-dir
	 *   handler.
	 * @param dirs colon separated string, use ';' on Windows and ':' on all
	 *   other OSs
	 */
	void AddDirs(const std::string& dirs);
	/**
	 * @brief Adds a single directory to the datadir handler.
	 * Will only add the directory if it was not already added,
	 * as lower index in the list of dirs means higher prefference,
	 * adding it again would be pointless.
	 */
	void AddDir(const std::string& dir);

	/**
	 * @brief Figure out permissions we have for a single data directory.
	 * @returns whether we have permissions to read the data directory.
	 */
	bool DeterminePermissions(DataDir* dataDir);
	/**
	 * @brief Figure out permissions we have for the data directories.
	 */
	void FilterUsableDataDirs();

	bool IsWriteableDir(DataDir* dataDir);
	void FindWriteableDataDir();

	/**
	 * Determines whether a given path may be a data-dir for multiple engine
	 * versions.
	 * This is done by checking the precense of some dirs,
	 * like "./maps/" and "./games/".
	 * You may think of this as denoting multi-engine-version portable-mode.
	 * This will return true on a default install on windows.
	 * @param dirPath a path to a dir to check for whether it is a data-dir
	 *   for multiple engine versions.
	 * @returns whether dirPath may be a data-dir for multiple engine versions.
	 */
	static bool IsInstallDirDataDir();
	static bool LooksLikeMultiVersionDataDir(const std::string& dirPath);


private:
	bool isolationMode;
	std::string isolationModeDir;
	std::string forcedWriteDir;

	std::vector<DataDir> dataDirs;
	const DataDir* writeDir;
};

#define dataDirLocater DataDirLocater::GetInstance()

#endif // !defined(DATA_DIR_LOCATER_H)