1 /*
2  * Copyright 2014 Andrew Ayer
3  *
4  * This file is part of git-crypt.
5  *
6  * git-crypt is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * git-crypt is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with git-crypt.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Additional permission under GNU GPL version 3 section 7:
20  *
21  * If you modify the Program, or any covered work, by linking or
22  * combining it with the OpenSSL project's OpenSSL library (or a
23  * modified version of that library), containing parts covered by the
24  * terms of the OpenSSL or SSLeay licenses, the licensors of the Program
25  * grant you additional permission to convey the resulting work.
26  * Corresponding Source for a non-source form of such a combination
27  * shall include the source code for the parts of OpenSSL used as well
28  * as that of the covered work.
29  */
30 
31 #include <io.h>
32 #include <stdio.h>
33 #include <fcntl.h>
34 #include <windows.h>
35 #include <vector>
36 #include <cstring>
37 
message() const38 std::string System_error::message () const
39 {
40 	std::string	mesg(action);
41 	if (!target.empty()) {
42 		mesg += ": ";
43 		mesg += target;
44 	}
45 	if (error) {
46 		LPTSTR	error_message;
47 		FormatMessageA(
48 			FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
49 			nullptr,
50 			error,
51 			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
52 			reinterpret_cast<LPTSTR>(&error_message),
53 			0,
54 			nullptr);
55 		mesg += error_message;
56 		LocalFree(error_message);
57 	}
58 	return mesg;
59 }
60 
open(std::ios_base::openmode mode)61 void	temp_fstream::open (std::ios_base::openmode mode)
62 {
63 	close();
64 
65 	char			tmpdir[MAX_PATH + 1];
66 
67 	DWORD			ret = GetTempPath(sizeof(tmpdir), tmpdir);
68 	if (ret == 0) {
69 		throw System_error("GetTempPath", "", GetLastError());
70 	} else if (ret > sizeof(tmpdir) - 1) {
71 		throw System_error("GetTempPath", "", ERROR_BUFFER_OVERFLOW);
72 	}
73 
74 	char			tmpfilename[MAX_PATH + 1];
75 	if (GetTempFileName(tmpdir, TEXT("git-crypt"), 0, tmpfilename) == 0) {
76 		throw System_error("GetTempFileName", "", GetLastError());
77 	}
78 
79 	filename = tmpfilename;
80 
81 	std::fstream::open(filename.c_str(), mode);
82 	if (!std::fstream::is_open()) {
83 		DeleteFile(filename.c_str());
84 		throw System_error("std::fstream::open", filename, 0);
85 	}
86 }
87 
close()88 void	temp_fstream::close ()
89 {
90 	if (std::fstream::is_open()) {
91 		std::fstream::close();
92 		DeleteFile(filename.c_str());
93 	}
94 }
95 
mkdir_parent(const std::string & path)96 void	mkdir_parent (const std::string& path)
97 {
98 	std::string::size_type		slash(path.find('/', 1));
99 	while (slash != std::string::npos) {
100 		std::string		prefix(path.substr(0, slash));
101 		if (GetFileAttributes(prefix.c_str()) == INVALID_FILE_ATTRIBUTES) {
102 			// prefix does not exist, so try to create it
103 			if (!CreateDirectory(prefix.c_str(), nullptr)) {
104 				throw System_error("CreateDirectory", prefix, GetLastError());
105 			}
106 		}
107 
108 		slash = path.find('/', slash + 1);
109 	}
110 }
111 
our_exe_path()112 std::string our_exe_path ()
113 {
114 	std::vector<char>	buffer(128);
115 	size_t			len;
116 
117 	while ((len = GetModuleFileNameA(nullptr, &buffer[0], buffer.size())) == buffer.size()) {
118 		// buffer may have been truncated - grow and try again
119 		buffer.resize(buffer.size() * 2);
120 	}
121 	if (len == 0) {
122 		throw System_error("GetModuleFileNameA", "", GetLastError());
123 	}
124 
125 	return std::string(buffer.begin(), buffer.begin() + len);
126 }
127 
exit_status(int status)128 int exit_status (int status)
129 {
130 	return status;
131 }
132 
touch_file(const std::string & filename)133 void	touch_file (const std::string& filename)
134 {
135 	HANDLE	fh = CreateFileA(filename.c_str(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
136 	if (fh == INVALID_HANDLE_VALUE) {
137 		DWORD	error = GetLastError();
138 		if (error == ERROR_FILE_NOT_FOUND) {
139 			return;
140 		} else {
141 			throw System_error("CreateFileA", filename, error);
142 		}
143 	}
144 	SYSTEMTIME	system_time;
145 	GetSystemTime(&system_time);
146 	FILETIME	file_time;
147 	SystemTimeToFileTime(&system_time, &file_time);
148 
149 	if (!SetFileTime(fh, nullptr, nullptr, &file_time)) {
150 		DWORD	error = GetLastError();
151 		CloseHandle(fh);
152 		throw System_error("SetFileTime", filename, error);
153 	}
154 	CloseHandle(fh);
155 }
156 
remove_file(const std::string & filename)157 void	remove_file (const std::string& filename)
158 {
159 	if (!DeleteFileA(filename.c_str())) {
160 		DWORD	error = GetLastError();
161 		if (error == ERROR_FILE_NOT_FOUND) {
162 			return;
163 		} else {
164 			throw System_error("DeleteFileA", filename, error);
165 		}
166 	}
167 }
168 
init_std_streams_platform()169 static void	init_std_streams_platform ()
170 {
171 	_setmode(_fileno(stdin), _O_BINARY);
172 	_setmode(_fileno(stdout), _O_BINARY);
173 }
174 
create_protected_file(const char * path)175 void create_protected_file (const char* path) // TODO
176 {
177 }
178 
util_rename(const char * from,const char * to)179 int util_rename (const char* from, const char* to)
180 {
181 	// On Windows OS, it is necessary to ensure target file doesn't exist
182 	unlink(to);
183 	return rename(from, to);
184 }
185 
get_directory_contents(const char * path)186 std::vector<std::string> get_directory_contents (const char* path)
187 {
188 	std::vector<std::string>	filenames;
189 	std::string			patt(path);
190 	if (!patt.empty() && patt[patt.size() - 1] != '/' && patt[patt.size() - 1] != '\\') {
191 		patt.push_back('\\');
192 	}
193 	patt.push_back('*');
194 
195 	WIN32_FIND_DATAA		ffd;
196 	HANDLE				h = FindFirstFileA(patt.c_str(), &ffd);
197 	if (h == INVALID_HANDLE_VALUE) {
198 		throw System_error("FindFirstFileA", patt, GetLastError());
199 	}
200 	do {
201 		if (std::strcmp(ffd.cFileName, ".") != 0 && std::strcmp(ffd.cFileName, "..") != 0) {
202 			filenames.push_back(ffd.cFileName);
203 		}
204 	} while (FindNextFileA(h, &ffd) != 0);
205 
206 	DWORD				err = GetLastError();
207 	if (err != ERROR_NO_MORE_FILES) {
208 		throw System_error("FileNextFileA", patt, err);
209 	}
210 	FindClose(h);
211 	return filenames;
212 }
213