Bumped copyright dates for 2013
[barry.git] / src / configfileunix.cc
blob94e6e06aa433cf9c68fdfe7a4faa884432cbb02b
1 ///
2 /// \file configfileunix.cc
3 /// Barry configuration class, for one device PIN, using Unix APIs
4 ///
6 /*
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 "i18n.h"
24 #include "configfile.h"
25 #include "error.h"
26 #include "r_message.h"
27 #include "getpwuid.h"
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <fstream>
32 #include <sstream>
33 #include <iostream>
34 #include <iomanip>
35 #include <sys/types.h>
36 #include <sys/stat.h>
38 namespace Barry {
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)
46 using namespace std;
48 time_t t = time(NULL);
49 struct tm *lt = localtime(&t);
51 std::string fileLabel = label;
52 if( fileLabel.size() ) {
53 // prepend a hyphen
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] == ' ' )
59 fileLabel[i] = '_';
60 else if( fileLabel[i] == '/' )
61 fileLabel[i] = '-';
62 else if( fileLabel[i] == '\\' )
63 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
72 << "-"
73 << setw(2) << setfill('0') << lt->tm_hour
74 << setw(2) << setfill('0') << lt->tm_min
75 << setw(2) << setfill('0') << lt->tm_sec
76 << fileLabel
77 << ".tar.gz";
78 return tarfilename.str();
81 void ConfigFile::BuildFilename()
83 size_t strsize = 255 * 5;
84 char *strbuf = new char[strsize];
85 struct passwd pwbuf;
86 struct passwd *pw;
88 getpwuid_r(getuid(), &pwbuf, strbuf, strsize, &pw);
89 if( !pw ) {
90 delete [] strbuf;
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";
99 delete [] strbuf;
102 void ConfigFile::BuildDefaultPath()
104 struct passwd *pw = getpwuid(getuid());
105 m_path = pw->pw_dir;
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 ) {
115 if( perr )
116 *perr = _("ConfigFile::CheckPath(): path is empty!");
117 return false;
120 if( access(path.c_str(), F_OK) == 0 )
121 return true;
123 std::string base;
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 ) {
129 if( perr ) {
130 *perr = _("mkdir() failed to create: ") + base + ": ";
131 *perr += strerror(errno);
133 return false;
137 if( mkdir(path.c_str(), 0755) == -1 ) {
138 if( perr ) {
139 *perr = _("last mkdir() failed to create: ") + path + ": ";
140 *perr += strerror(errno);
142 return false;
144 return true;
147 void GlobalConfigFile::BuildFilename()
149 struct passwd *pw = getpwuid(getuid());
150 if( !pw )
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
157 m_path = pw->pw_dir;
158 m_path += "/.barry";
161 } // namespace Barry