2 /// \file configfileunix.cc
3 /// Barry configuration class, for one device PIN, using Unix 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"
35 #include <sys/types.h>
41 /// Creates a tar.gz filename using PIN + date + time + label.
42 /// Does not include any path, just returns a new filename.
43 std::string
MakeBackupFilename(const Barry::Pin
&pin
,
44 const std::string
&label
)
48 time_t t
= time(NULL
);
49 struct tm
*lt
= localtime(&t
);
51 std::string fileLabel
= label
;
52 if( fileLabel
.size() ) {
54 fileLabel
.insert(fileLabel
.begin(), '-');
56 // translate all spaces and slashes
57 for( size_t i
= 0; i
< fileLabel
.size(); i
++ ) {
58 if( fileLabel
[i
] == ' ' )
60 else if( fileLabel
[i
] == '/' )
62 else if( fileLabel
[i
] == '\\' )
67 ostringstream tarfilename
;
68 tarfilename
<< pin
.Str() << "-"
69 << setw(4) << setfill('0') << (lt
->tm_year
+ 1900)
70 << setw(2) << setfill('0') << (lt
->tm_mon
+ 1)
71 << setw(2) << setfill('0') << lt
->tm_mday
73 << setw(2) << setfill('0') << lt
->tm_hour
74 << setw(2) << setfill('0') << lt
->tm_min
75 << setw(2) << setfill('0') << lt
->tm_sec
78 return tarfilename
.str();
81 void ConfigFile::BuildFilename()
83 size_t strsize
= 255 * 5;
84 char *strbuf
= new char[strsize
];
88 getpwuid_r(getuid(), &pwbuf
, strbuf
, strsize
, &pw
);
91 throw ConfigFileError(_("BuildFilename: getpwuid failed"), errno
);
94 m_filename
= pw
->pw_dir
;
95 m_filename
+= "/.barry/backup/";
96 m_filename
+= m_pin
.Str();
97 m_filename
+= "/config";
102 void ConfigFile::BuildDefaultPath()
104 struct passwd
*pw
= getpwuid(getuid());
106 m_path
+= "/.barry/backup/";
107 m_path
+= m_pin
.Str();
110 /// Checks that the path in path exists, and if not, creates it.
111 /// Returns false if unable to create path, true if ok.
112 bool ConfigFile::CheckPath(const std::string
&path
, std::string
*perr
)
114 if( path
.size() == 0 ) {
116 *perr
= _("ConfigFile::CheckPath(): path is empty!");
120 if( access(path
.c_str(), F_OK
) == 0 )
124 std::string::size_type slash
= 0;
125 while( (slash
= path
.find('/', slash
+ 1)) != std::string::npos
) {
126 base
= path
.substr(0, slash
);
127 if( access(base
.c_str(), F_OK
) != 0 ) {
128 if( mkdir(base
.c_str(), 0755) == -1 ) {
130 *perr
= _("mkdir() failed to create: ") + base
+ ": ";
131 *perr
+= strerror(errno
);
137 if( mkdir(path
.c_str(), 0755) == -1 ) {
139 *perr
= _("last mkdir() failed to create: ") + path
+ ": ";
140 *perr
+= strerror(errno
);
147 void GlobalConfigFile::BuildFilename()
149 struct passwd
*pw
= getpwuid(getuid());
151 throw ConfigFileError(_("BuildFilename: getpwuid failed"), errno
);
153 m_filename
= pw
->pw_dir
;
154 m_filename
+= "/.barry/config";
156 // build the global path too, since this never changes