Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Mounts.cpp « src « far2l - github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e8901174e615f705a8c8bd6900643ef83677227 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "headers.hpp"

#include <crc64.h>
#include <wordexp.h>
#include <fstream>
#include "Mounts.hpp"
#include "lang.hpp"
#include "keys.hpp"
#include "help.hpp"
#include "vmenu.hpp"
#include "message.hpp"
#include "config.hpp"
#include "pathmix.hpp"
#include "strmix.hpp"
#include "dirmix.hpp"
#include "execute.hpp"
#include "manager.hpp"
#include "ConfigRW.hpp"
#include "HotkeyLetterDialog.hpp"
#include "MountInfo.h"

namespace Mounts
{
	#define HOTKEYS_SECTION "MountsHotkeys"
	#define ID_ROOT    0
	#define ID_HOME    1
	#define ID_ANOTHER 2

	static wchar_t DefaultHotKey(int id, const FARString &path)
	{
		switch (id) {
			case ID_ROOT:
				return L'/';

			case ID_HOME:
				return L'~';

			case ID_ANOTHER:
				return L'-';
		}

		return 0;
	}

	static std::string SettingsKey(int id)
	{
		return StrPrintf("%08x", id);
	}

	static int GenerateIdFromPath(const FARString &path)
	{
		if (path == L"/")
			return ID_ROOT;

		const std::string &path_mb = path.GetMB();
		const uint64_t id64 = crc64(0, (unsigned char *)path_mb.c_str(), path_mb.size());
		uint32_t out = (uint32_t)(id64 ^ (id64 >> 32));
		if (out == ID_ROOT || out == ID_HOME || out == ID_ANOTHER) {
			out^= 0xffffffff;
		}
		return (int)out;
	}

	/////

	Enum::Enum(FARString &another_curdir)
	{
		bool has_rootfs = false;
		if (Opt.ChangeDriveMode & DRIVE_SHOW_MOUNTS)
		{
			AddMounts(has_rootfs);
		}
		AddFavorites(has_rootfs);

		if (!has_rootfs) {
			emplace(begin(), Entry(L"/", Msg::MountsRoot, false, ID_ROOT));
		}

		emplace(begin(), Entry( GetMyHome(), Msg::MountsHome, false, ID_HOME));
		emplace(begin(), Entry( another_curdir, Msg::MountsOther, false, ID_ANOTHER));

		ConfigReader cfg_reader(HOTKEYS_SECTION);
		for (auto &m : *this) {
			if (max_path < m.path.GetLength())
				max_path = m.path.GetLength();
			if (max_info < m.info.GetLength())
				max_info = m.info.GetLength();
			if (max_usage < m.usage.GetLength())
				max_usage = m.usage.GetLength();
			wchar_t def_hk[] = {DefaultHotKey(m.id, m.path), 0};
			auto hk = cfg_reader.GetString(SettingsKey(m.id), def_hk);
			m.hotkey = hk.IsEmpty() ? 0 : *hk.CPtr();
		}
	}

	static void ExpandMountpointInfo(const Mountpoint &mp, FARString &str)
	{
		FARString val = L" ";
		if (mp.bad) {
			val = L"?";
		} else if (mp.read_only) {
			val = L"!";
		}
		ReplaceStrings(str, L"$S", val);

		FileSizeToStr(val, mp.total, -1, COLUMN_ECONOMIC | COLUMN_FLOATSIZE | COLUMN_SHOWBYTESINDEX);
		ReplaceStrings(str, L"$T", val);

		FileSizeToStr(val, mp.avail, -1, COLUMN_ECONOMIC | COLUMN_FLOATSIZE | COLUMN_SHOWBYTESINDEX);
		ReplaceStrings(str, L"$A", val);

		FileSizeToStr(val, mp.freee, -1, COLUMN_ECONOMIC | COLUMN_FLOATSIZE | COLUMN_SHOWBYTESINDEX);
		ReplaceStrings(str, L"$F", val);

		FileSizeToStr(val, mp.total - mp.freee, -1, COLUMN_ECONOMIC | COLUMN_FLOATSIZE | COLUMN_SHOWBYTESINDEX);
		ReplaceStrings(str, L"$U", val);

		if (mp.total)
			val.Format(L"%d", (mp.avail * 100) / mp.total);
		else
			val = L"NA";
		ReplaceStrings(str, L"$a", val);

		if (mp.total)
			val.Format(L"%d", (mp.freee * 100) / mp.total);
		else
			val = L"NA";
		ReplaceStrings(str, L"$f", val);

		if (mp.total)
			val.Format(L"%d", ((mp.total - mp.freee) * 100) / mp.total);
		else
			val = L"NA";
		ReplaceStrings(str, L"$u", val);

		val = mp.filesystem;
		ReplaceStrings(str, L"$N", val);

		val = mp.device;
		ReplaceStrings(str, L"$D", val);
	}

	void Enum::AddMounts(bool &has_rootfs)
	{
		MountInfo mi(true);
		for (const auto &mp : mi.Enum()) {
			emplace_back();
			auto &e = back();
			e.path = mp.path;
			e.usage = Opt.ChangeDriveColumn2;
			e.info = Opt.ChangeDriveColumn3;
			ExpandMountpointInfo(mp, e.usage);
			ExpandMountpointInfo(mp, e.info);
			
			if (e.path == L"/") {
				has_rootfs = true;
			} else {
				e.unmountable = true;
			}
			e.id = GenerateIdFromPath(e.path);
		}
	}

	void Enum::AddFavorites(bool &has_rootfs)
	{
		std::ifstream favis(InMyConfig("favorites"));
		if (favis.is_open()) {
			std::string line;
			while (std::getline(favis, line)) {
				StrTrim(line, " \t\r\n");
				if (line.empty() || line.front() == '#') {
					continue;
				}
				Environment::ExpandString(line, true, true);
				std::vector<std::string> sublines;
				StrExplode(sublines, line, "\n");
				for (const auto &subline : sublines) {
					std::vector<std::string> parts;
					StrExplode(parts, subline, "\t");
					if (!parts.empty()) {
						emplace_back();
						auto &e = back();
						e.path = parts.front();
						if (parts.size() > 1) {
							e.info = parts.back();
							if (parts.size() > 2) {
								e.usage = parts[1];
							}
						}
						e.id = GenerateIdFromPath(e.path);
						if (e.path == L"/") {
							has_rootfs = true;

						} else if (*e.path.CPtr() == L'/') {
							e.unmountable = true;
						}
					}
				}
			}
		}
	}
	//////

	bool Unmount(const FARString &path, bool force)
	{
		std::string cmd = GetMyScriptQuoted("unmount.sh");
		cmd+= " \"";
		cmd+= EscapeCmdStr(Wide2MB(path));
		cmd+= "\"";
		if (force) {
			cmd+= " force";
		}
		int r = farExecuteA(cmd.c_str(), 0);
		if (FrameManager) {
			auto *current_frame = FrameManager->GetCurrentFrame();
			if (current_frame) {
				FrameManager->RefreshFrame(current_frame);
			}
		}
		return r == 0;
	}

	void EditHotkey(const FARString &path, int id)
	{
		wchar_t def_hk[] = {DefaultHotKey(id, path), 0};
		const auto &Key = SettingsKey(id);
		const auto &Setting = ConfigReader(HOTKEYS_SECTION).GetString(Key, def_hk);
		WCHAR Letter[2] = {Setting.IsEmpty() ? 0 : Setting[0], 0};
		if (HotkeyLetterDialog(Msg::LocationHotKeyTitle, path.CPtr(), Letter[0])) {
			ConfigWriter cw(HOTKEYS_SECTION);
			if (Letter[0])
				cw.SetString(Key, Letter);
			else
				cw.RemoveKey(Key);
		}
	}
}