2 * Copyright (C) 2008 Patrick Ohly
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 #include "SyncEvolutionUtil.h"
21 #include "EvolutionSyncClient.h"
22 #include <base/test.h>
24 #include <boost/scoped_array.hpp>
29 #include <sys/types.h>
32 #ifdef ENABLE_UNIT_TESTS
33 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("SyncEvolution");
36 string normalizePath(const string &path)
40 res.reserve(path.size());
42 while (index < path.size()) {
43 char curr = path[index];
47 while (index < path.size() &&
48 (path[index] == '/' ||
49 (path[index] == '.' &&
50 index + 1 < path.size() &&
51 (path[index + 1] == '.' ||
52 path[index + 1] == '/')))) {
57 if (!res.empty() && res[res.size() - 1] == '/') {
58 res.resize(res.size() - 1);
63 void mkdir_p(const string &path)
65 boost::scoped_array<char> dirs(new char[path.size() + 1]);
66 char *curr = dirs.get();
67 strcpy(curr, path.c_str());
69 char *nextdir = strchr(curr, '/');
75 if (access(dirs.get(),
76 nextdir ? (R_OK|X_OK) : (R_OK|X_OK|W_OK)) &&
78 mkdir(dirs.get(), 0777))) {
79 EvolutionSyncClient::throwError(string(dirs.get()) + ": " + strerror(errno));
89 void rm_r(const string &path)
91 if (!unlink(path.c_str()) ||
96 if (errno != EISDIR) {
97 EvolutionSyncClient::throwError(path + ": " + strerror(errno));
101 for (ReadDir::const_iterator it = dir.begin();
104 rm_r(path + "/" + *it);
106 if (rmdir(path.c_str())) {
107 EvolutionSyncClient::throwError(path + ": " + strerror(errno));
111 bool isDir(const string &path)
113 DIR *dir = opendir(path.c_str());
117 } else if (errno != ENOTDIR && errno != ENOENT) {
118 EvolutionSyncClient::throwError(path + ": " + strerror(errno));
126 static class InitSRand {
133 char buffer[16 * 4 + 5];
134 sprintf(buffer, "%08x-%04x-%04x-%02x%02x-%08x%04x",
137 rand() & 0x0FFF | 0x4000 /* RFC 4122 time_hi_and_version */,
138 rand() & 0xBF | 0x80 /* clock_seq_hi_and_reserved */,
143 this->assign(buffer);
147 ReadDir::ReadDir(const string &path) : m_path(path)
152 dir = opendir(path.c_str());
154 EvolutionSyncClient::throwError(path + ": " + strerror(errno));
157 struct dirent *entry = readdir(dir);
159 if (strcmp(entry->d_name, ".") &&
160 strcmp(entry->d_name, "..")) {
161 m_entries.push_back(entry->d_name);
163 entry = readdir(dir);
166 EvolutionSyncClient::throwError(path + ": " + strerror(errno));