2 /// \file configfilewin32.cc
3 /// Barry configuration class, for one device PIN, using Win32 APIs
7 Copyright (C) 2007-2013, Net Direct Inc. (http://www.netdirect.ca/)
8 Portions Copyright (C) 2012, RealVNC Ltd.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
24 #include "configfile.h"
26 #include "r_message.h"
34 // Returned by GetFileAttributes to indicate failure
35 #define INVALID_ATTRIB 0xFFFFFFFF
40 /// Creates a tar.gz filename using PIN + date + time + label.
41 /// Does not include any path, just returns a new filename.
42 std::string
MakeBackupFilename(const Barry::Pin
&pin
,
43 const std::string
&label
)
50 std::string fileLabel
= label
;
51 if( fileLabel
.size() ) {
53 fileLabel
.insert(fileLabel
.begin(), '-');
55 // translate all spaces and slashes
56 for( size_t i
= 0; i
< fileLabel
.size(); i
++ ) {
57 if( fileLabel
[i
] == ' ' )
59 else if( fileLabel
[i
] == '/' )
61 else if( fileLabel
[i
] == '\\' )
66 ostringstream tarfilename
;
67 tarfilename
<< pin
.Str() << "-"
68 << setw(4) << setfill('0') << st
.wYear
69 << setw(2) << setfill('0') << st
.wMonth
70 << setw(2) << setfill('0') << st
.wDay
72 << setw(2) << setfill('0') << st
.wHour
73 << setw(2) << setfill('0') << st
.wMinute
74 << setw(2) << setfill('0') << st
.wSecond
77 return tarfilename
.str();
80 void ConfigFile::BuildFilename()
82 TCHAR dirName
[MAX_PATH
];
83 CHAR dirNameA
[MAX_PATH
];
84 if( !SHGetSpecialFolderPath(NULL
, dirName
, CSIDL_APPDATA
, TRUE
) ) {
85 throw ConfigFileError(_("BuildFilename: SHGetSpecialFolderPath failed"), GetLastError());
87 if( WideCharToMultiByte(CP_ACP
, 0, dirName
, -1, dirNameA
, MAX_PATH
, NULL
, NULL
) <= 0 ) {
88 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
90 dirNameA
[MAX_PATH
-1] = 0; // Make sure it's NUL terminated
91 m_filename
= dirNameA
;
92 m_filename
+= "\\.barry\\backup\\";
93 m_filename
+= m_pin
.Str();
94 m_filename
+= "\\config";
97 void ConfigFile::BuildDefaultPath()
99 TCHAR dirName
[MAX_PATH
];
100 CHAR dirNameA
[MAX_PATH
];
101 if( !SHGetSpecialFolderPath(NULL
, dirName
, CSIDL_APPDATA
, TRUE
) ) {
102 throw ConfigFileError(_("BuildDefaultPath: SHGetSpecialFolderPath failed"), GetLastError());
104 if( WideCharToMultiByte(CP_ACP
, 0, dirName
, -1, dirNameA
, MAX_PATH
, NULL
, NULL
) <= 0 ) {
105 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
107 dirNameA
[MAX_PATH
-1] = 0; // Make sure it's NUL terminated
109 m_path
+= "\\.barry\\backup\\";
110 m_path
+= m_pin
.Str();
113 /// Checks that the path in path exists, and if not, creates it.
114 /// Returns false if unable to create path, true if ok.
115 bool ConfigFile::CheckPath(const std::string
&path
, std::string
*perr
)
117 if( path
.size() == 0 ) {
119 *perr
= _("ConfigFile::CheckPath(): path is empty!");
123 TCHAR dirName
[MAX_PATH
];
124 if( MultiByteToWideChar(CP_ACP
, 0, path
.c_str(), -1, dirName
, MAX_PATH
) <= 0 ) {
126 *perr
= _("Failed to convert to widechar");
129 dirName
[MAX_PATH
-1] = 0; // Make sure it's NUL terminated
131 DWORD attrs
= GetFileAttributes(dirName
);
132 if( (attrs
!= INVALID_ATTRIB
) &&
133 ((attrs
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
) )
137 for( int i
= 0; dirName
[i
] != 0; ++i
) {
138 if( dirName
[i
] != '\\' )
140 // At a path separator, turn it into NUL so
141 // that string handling APIs see just this part of the path
143 DWORD attrs
= GetFileAttributes(dirName
);
144 if( (attrs
!= INVALID_ATTRIB
) &&
145 ((attrs
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
) )
147 if( !CreateDirectory(dirName
, NULL
) ) {
149 *perr
= _("failed to create directory");
153 // Turn it back into a directory separator
159 void GlobalConfigFile::BuildFilename()
161 TCHAR dirName
[MAX_PATH
];
162 CHAR dirNameA
[MAX_PATH
];
163 if (!SHGetSpecialFolderPath(NULL
, dirName
, CSIDL_APPDATA
, TRUE
)) {
164 throw ConfigFileError(_("BuildFilename: SHGetSpecialFolderPath failed"), GetLastError());
166 if (WideCharToMultiByte(CP_ACP
, 0, dirName
, -1, dirNameA
, MAX_PATH
, NULL
, NULL
) <= 0) {
167 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
169 dirNameA
[MAX_PATH
-1] = 0; // Make sure it's NUL terminated
170 m_filename
= dirNameA
;
171 m_filename
+= "\\.barry\\config";
173 // build the global path too, since this never changes
175 m_path
+= "\\.barry";