lib: Moving default Barry::Data size into constants.
[barry.git] / src / configfileunix.cc
blobfcc6a9edc74f62a51c7e373e4fc016243885bada
1 ///
2 /// \file configfileunix.cc
3 /// Barry configuration class, for one device PIN, using Unix APIs
4 ///
6 /*
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"
24 #include "error.h"
25 #include "r_message.h"
26 #include "getpwuid.h"
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fstream>
31 #include <sstream>
32 #include <iostream>
33 #include <iomanip>
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 namespace Barry {
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)
45 using namespace std;
47 time_t t = time(NULL);
48 struct tm *lt = localtime(&t);
50 std::string fileLabel = label;
51 if( fileLabel.size() ) {
52 // prepend a hyphen
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] == ' ' )
58 fileLabel[i] = '_';
59 else if( fileLabel[i] == '/' )
60 fileLabel[i] = '-';
61 else if( fileLabel[i] == '\\' )
62 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
71 << "-"
72 << setw(2) << setfill('0') << lt->tm_hour
73 << setw(2) << setfill('0') << lt->tm_min
74 << setw(2) << setfill('0') << lt->tm_sec
75 << fileLabel
76 << ".tar.gz";
77 return tarfilename.str();
80 void ConfigFile::BuildFilename()
82 size_t strsize = 255 * 5;
83 char *strbuf = new char[strsize];
84 struct passwd pwbuf;
85 struct passwd *pw;
87 getpwuid_r(getuid(), &pwbuf, strbuf, strsize, &pw);
88 if( !pw ) {
89 delete [] strbuf;
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";
98 delete [] strbuf;
101 void ConfigFile::BuildDefaultPath()
103 struct passwd *pw = getpwuid(getuid());
104 m_path = pw->pw_dir;
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 ) {
114 if( perr )
115 *perr = "path is empty!";
116 return false;
119 if( access(path.c_str(), F_OK) == 0 )
120 return true;
122 std::string base;
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 ) {
128 if( perr ) {
129 *perr = "mkdir(" + base + ") failed: ";
130 *perr += strerror(errno);
132 return false;
136 if( mkdir(path.c_str(), 0755) == -1 ) {
137 if( perr ) {
138 *perr = "last mkdir(" + path + ") failed: ";
139 *perr += strerror(errno);
141 return false;
143 return true;
146 void GlobalConfigFile::BuildFilename()
148 struct passwd *pw = getpwuid(getuid());
149 if( !pw )
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
156 m_path = pw->pw_dir;
157 m_path += "/.barry";
160 } // namespace Barry