Repository navigation
Expand file tree
/
Copy pathFileOpsImpl.cpp
More file actions
168 lines (151 loc) · 4.82 KB
/
Copy pathFileOpsImpl.cpp
File metadata and controls
168 lines (151 loc) · 4.82 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
//-----------------------------------------------------------------------------
//
// 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 <windows.h>
#include "FileOpsImpl.h"
#include "Utils.h"
namespace OpenZWave
{
namespace Internal
{
namespace Platform
{
//-----------------------------------------------------------------------------
// <FileOpsImpl::FileOpsImpl>
// Constructor
//-----------------------------------------------------------------------------
FileOpsImpl::FileOpsImpl()
{
}
//-----------------------------------------------------------------------------
// <FileOpsImpl::~FileOpsImpl>
// Destructor
//-----------------------------------------------------------------------------
FileOpsImpl::~FileOpsImpl()
{
}
//-----------------------------------------------------------------------------
// <FileOpsImpl::FolderExists>
// Determine if a folder exists
//-----------------------------------------------------------------------------
bool FileOpsImpl::FolderExists(const string &_folderName)
{
uint32 ftype = GetFileAttributesA(_folderName.c_str());
if (ftype == INVALID_FILE_ATTRIBUTES)
return false; // something is wrong with _foldername path
if (ftype & FILE_ATTRIBUTE_DIRECTORY)
return true;
return false;
}
bool FileOpsImpl::FileExists(const string _filename)
{
DWORD dwAttrib = GetFileAttributesA(_filename.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
bool FileOpsImpl::FileWriteable(const string _filename)
{
DWORD dwAttrib;
if (!FileExists(_filename))
{
/* check if the directory is writtable */
dwAttrib = GetFileAttributesA(ozwdirname(_filename).c_str());
}
else
{
dwAttrib = GetFileAttributesA(_filename.c_str());
}
return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_READONLY));
}
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 (DeleteFileA(_filename.c_str()) == 0)
{
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));
return false;
}
if (CopyFileA(_sourcefile.c_str(), _destfile.c_str(), FALSE) == 0)
{
Log::Write(LogLevel_Warning, "CopyFile Failed %s - %s", _sourcefile.c_str(), _destfile.c_str());
return false;
}
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;
}
if (CreateDirectoryA(_dirname.c_str(), NULL) == 0)
{
Log::Write(LogLevel_Warning, "Create Directory Failed: %s", _dirname.c_str());
return false;
}
return true;
}
} // namespace Platform
} // namespace Internal
} // namespace OpenZWave