Repository navigation
Expand file tree
/
Copy pathFileOpsImpl.cpp
More file actions
180 lines (161 loc) · 4.83 KB
/
Copy pathFileOpsImpl.cpp
File metadata and controls
180 lines (161 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//-----------------------------------------------------------------------------
//
// FileOpsImpl.cpp
//
// Unix implementation of file operations
//
// Copyright (c) 2012, Greg Satz <satz@iranger.com>
// All rights reserved.
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <errno.h>
#include "FileOpsImpl.h"
#include "Utils.h"
namespace OpenZWave
{
namespace Internal
{
namespace Platform
{
using std::ios_base;
//-----------------------------------------------------------------------------
// <FileOpsImpl::FileOpsImpl>
// Constructor
//-----------------------------------------------------------------------------
FileOpsImpl::FileOpsImpl()
{
}
//-----------------------------------------------------------------------------
// <FileOpsImpl::~FileOpsImpl>
// Destructor
//-----------------------------------------------------------------------------
FileOpsImpl::~FileOpsImpl()
{
}
//-----------------------------------------------------------------------------
// <FileOpsImpl::FolderExists>
// Determine if a folder exists
//-----------------------------------------------------------------------------
bool FileOpsImpl::FolderExists(const string _folderName)
{
DIR *dirp = opendir(_folderName.c_str());
if (dirp != NULL)
{
closedir(dirp);
return true;
}
else
return false;
}
bool FileOpsImpl::FileExists(const string _filename)
{
struct stat buffer;
return (stat(_filename.c_str(), &buffer) == 0);
}
bool FileOpsImpl::FileWriteable(const string _filename)
{
if (!FileExists(_filename))
{
string fn = ozwdirname(_filename);
return (access(fn.c_str(), W_OK | F_OK) == 0);
}
return (access(_filename.c_str(), W_OK | F_OK) == 0);
}
bool FileOpsImpl::FileRotate(const string _filename)
{
int i = 1;
string newFile;
/* find a filename not used yet */
newFile = _filename;
newFile.append(".").append(intToString(i));
while (FileExists(newFile))
{
i++;
newFile = _filename;
newFile.append(".").append(intToString(i));
}
/* copy the file */
if (!FileCopy(_filename, newFile))
{
Log::Write(LogLevel_Warning, "File Rotate Failed: %s -> %s", _filename.c_str(), newFile.c_str());
return false;
}
/* remove the old file */
if (remove(_filename.c_str()))
{
Log::Write(LogLevel_Warning, "File Removal failed: %s", _filename.c_str());
return false;
}
return true;
}
bool FileOpsImpl::FileCopy(const string _sourcefile, const string _destfile)
{
if (!FileExists(_sourcefile))
{
Log::Write(LogLevel_Warning, "Source File %s doesn't exist in FileCopy", _sourcefile.c_str());
return false;
}
if (FileExists(_destfile))
{
Log::Write(LogLevel_Warning, "Destination File %s exists in FileCopy", _destfile.c_str());
return false;
}
/* make sure the Destination Folder Exists */
if (!FolderExists(ozwdirname(_destfile)))
{
Log::Write(LogLevel_Warning, "Destination Folder %s Doesn't Exist", ozwdirname(_destfile).c_str());
return false;
}
std::ifstream in(_sourcefile.c_str(), ios_base::in | ios_base::binary);
std::ofstream out(_destfile.c_str(), ios_base::out | ios_base::binary);
char buf[COPY_BUF_SIZE];
do
{
in.read(&buf[0], COPY_BUF_SIZE);
out.write(&buf[0], in.gcount());
} while (in.gcount() > 0);
in.close();
out.close();
return true;
}
bool FileOpsImpl::FolderCreate(const string _dirname)
{
if (FolderExists(_dirname))
{
Log::Write(LogLevel_Warning, "Folder %s Exists for FolderCreate", _dirname.c_str());
return false;
}
int ret = mkdir(_dirname.c_str(), 0777);
if (ret == 0)
return true;
Log::Write(LogLevel_Warning, "Create Directory Failed: %s - %s", _dirname.c_str(), strerror(errno));
return false;
}
} // namespace Platform
} // namespace Internal
} // namespace OpenZWave