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.
23 #include "configfile.h"
25 #include "r_message.h"
33 // Returned by GetFileAttributes to indicate failure
34 #define INVALID_ATTRIB 0xFFFFFFFF
39 /// Creates a tar.gz filename using PIN + date + time + label.
40 /// Does not include any path, just returns a new filename.
41 std::string
MakeBackupFilename(const Barry::Pin
&pin
,
42 const std::string
&label
)
49 std::string fileLabel
= label
;
50 if( fileLabel
.size() ) {
52 fileLabel
.insert(fileLabel
.begin(), '-');
54 // translate all spaces and slashes
55 for( size_t i
= 0; i
< fileLabel
.size(); i
++ ) {
56 if( fileLabel
[i
] == ' ' )
58 else if( fileLabel
[i
] == '/' )
60 else if( fileLabel
[i
] == '\\' )
65 ostringstream tarfilename
;
66 tarfilename
<< pin
.Str() << "-"
67 << setw(4) << setfill('0') << st
.wYear
68 << setw(2) << setfill('0') << st
.wMonth
69 << setw(2) << setfill('0') << st
.wDay
71 << setw(2) << setfill('0') << st
.wHour
72 << setw(2) << setfill('0') << st
.wMinute
73 << setw(2) << setfill('0') << st
.wSecond
76 return tarfilename
.str();
79 void ConfigFile::BuildFilename()
81 TCHAR dirName
[MAX_PATH
];
82 CHAR dirNameA
[MAX_PATH
];
83 if( !SHGetSpecialFolderPath(NULL
, dirName
, CSIDL_APPDATA
, TRUE
) ) {
84 throw ConfigFileError(_("BuildFilename: SHGetSpecialFolderPath failed"), GetLastError());
86 if( WideCharToMultiByte(CP_ACP
, 0, dirName
, -1, dirNameA
, MAX_PATH
, NULL
, NULL
) <= 0 ) {
87 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
89 dirNameA
[MAX_PATH
-1] = 0; // Make sure it's NUL terminated
90 m_filename
= dirNameA
;
91 m_filename
+= "\\.barry\\backup\\";
92 m_filename
+= m_pin
.Str();
93 m_filename
+= "\\config";
96 void ConfigFile::BuildDefaultPath()
98 TCHAR dirName
[MAX_PATH
];
99 CHAR dirNameA
[MAX_PATH
];
100 if( !SHGetSpecialFolderPath(NULL
, dirName
, CSIDL_APPDATA
, TRUE
) ) {
101 throw ConfigFileError(_("BuildDefaultPath: SHGetSpecialFolderPath failed"), GetLastError());
103 if( WideCharToMultiByte(CP_ACP
, 0, dirName
, -1, dirNameA
, MAX_PATH
, NULL
, NULL
) <= 0 ) {
104 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
106 dirNameA
[MAX_PATH
-1] = 0; // Make sure it's NUL terminated
108 m_path
+= "\\.barry\\backup\\";
109 m_path
+= m_pin
.Str();
112 /// Checks that the path in path exists, and if not, creates it.
113 /// Returns false if unable to create path, true if ok.
114 bool ConfigFile::CheckPath(const std::string
&path
, std::string
*perr
)
116 if( path
.size() == 0 ) {
118 *perr
= _("ConfigFile::CheckPath(): path is empty!");
122 TCHAR dirName
[MAX_PATH
];
123 if( MultiByteToWideChar(CP_ACP
, 0, path
.c_str(), -1, dirName
, MAX_PATH
) <= 0 ) {
125 *perr
= _("Failed to convert to widechar");
128 dirName
[MAX_PATH
-1] = 0; // Make sure it's NUL terminated
130 DWORD attrs
= GetFileAttributes(dirName
);
131 if( (attrs
!= INVALID_ATTRIB
) &&
132 ((attrs
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
) )
136 for( int i
= 0; dirName
[i
] != 0; ++i
) {
137 if( dirName
[i
] != '\\' )
139 // At a path separator, turn it into NUL so
140 // that string handling APIs see just this part of the path
142 DWORD attrs
= GetFileAttributes(dirName
);
143 if( (attrs
!= INVALID_ATTRIB
) &&
144 ((attrs
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
) )
146 if( !CreateDirectory(dirName
, NULL
) ) {
148 *perr
= _("failed to create directory");
152 // Turn it back into a directory separator
158 void GlobalConfigFile::BuildFilename()
160 TCHAR dirName
[MAX_PATH
];
161 CHAR dirNameA
[MAX_PATH
];
162 if (!SHGetSpecialFolderPath(NULL
, dirName
, CSIDL_APPDATA
, TRUE
)) {
163 throw ConfigFileError(_("BuildFilename: SHGetSpecialFolderPath failed"), GetLastError());
165 if (WideCharToMultiByte(CP_ACP
, 0, dirName
, -1, dirNameA
, MAX_PATH
, NULL
, NULL
) <= 0) {
166 throw ConfigFileError(_("BuildFilename: conversion failed"), GetLastError());
168 dirNameA
[MAX_PATH
-1] = 0; // Make sure it's NUL terminated
169 m_filename
= dirNameA
;
170 m_filename
+= "\\.barry\\config";
172 // build the global path too, since this never changes
174 m_path
+= "\\.barry";