2 /// \file configfileunix.cc
3 /// Barry configuration class, for one device PIN, using Unix APIs
7 Copyright (C) 2007-2012, 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"
34 #include <sys/types.h>
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
)
47 time_t t
= time(NULL
);
48 struct tm
*lt
= localtime(&t
);
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') << (lt
->tm_year
+ 1900)
69 << setw(2) << setfill('0') << (lt
->tm_mon
+ 1)
70 << setw(2) << setfill('0') << lt
->tm_mday
72 << setw(2) << setfill('0') << lt
->tm_hour
73 << setw(2) << setfill('0') << lt
->tm_min
74 << setw(2) << setfill('0') << lt
->tm_sec
77 return tarfilename
.str();
80 void ConfigFile::BuildFilename()
82 size_t strsize
= 255 * 5;
83 char *strbuf
= new char[strsize
];
87 getpwuid_r(getuid(), &pwbuf
, strbuf
, strsize
, &pw
);
90 throw ConfigFileError("BuildFilename: getpwuid failed", errno
);
93 m_filename
= pw
->pw_dir
;
94 m_filename
+= "/.barry/backup/";
95 m_filename
+= m_pin
.Str();
96 m_filename
+= "/config";
101 void ConfigFile::BuildDefaultPath()
103 struct passwd
*pw
= getpwuid(getuid());
105 m_path
+= "/.barry/backup/";
106 m_path
+= m_pin
.Str();
109 /// Checks that the path in path exists, and if not, creates it.
110 /// Returns false if unable to create path, true if ok.
111 bool ConfigFile::CheckPath(const std::string
&path
, std::string
*perr
)
113 if( path
.size() == 0 ) {
115 *perr
= "path is empty!";
119 if( access(path
.c_str(), F_OK
) == 0 )
123 std::string::size_type slash
= 0;
124 while( (slash
= path
.find('/', slash
+ 1)) != std::string::npos
) {
125 base
= path
.substr(0, slash
);
126 if( access(base
.c_str(), F_OK
) != 0 ) {
127 if( mkdir(base
.c_str(), 0755) == -1 ) {
129 *perr
= "mkdir(" + base
+ ") failed: ";
130 *perr
+= strerror(errno
);
136 if( mkdir(path
.c_str(), 0755) == -1 ) {
138 *perr
= "last mkdir(" + path
+ ") failed: ";
139 *perr
+= strerror(errno
);
146 void GlobalConfigFile::BuildFilename()
148 struct passwd
*pw
= getpwuid(getuid());
150 throw ConfigFileError("BuildFilename: getpwuid failed", errno
);
152 m_filename
= pw
->pw_dir
;
153 m_filename
+= "/.barry/config";
155 // build the global path too, since this never changes