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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define FORBIDDEN_SYMBOL_EXCEPTION_time_h
#define FORBIDDEN_SYMBOL_EXCEPTION_unistd_h
#define FORBIDDEN_SYMBOL_EXCEPTION_mkdir
#define FORBIDDEN_SYMBOL_EXCEPTION_getenv
#include "backends/fs/kolibrios/kolibrios-fs.h"
#include "backends/fs/posix/posix-iostream.h"
#include "common/algorithm.h"
#include "common/debug.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ksys.h>
#include "common/pack-start.h" // START STRUCT PACKING
struct kol_readdir_result_entry {
ksys_file_info_t bdfe;
char fileName[520];
} PACKED_STRUCT;
struct kol_readdir_result {
ksys_dir_entry_header_t header;
kol_readdir_result_entry entries[0];
} PACKED_STRUCT;
#include "common/pack-end.h" // END STRUCT PACKING
namespace {
// opendir/readdir are buggy. And they encourage using syscalls. Whatever
kol_readdir_result *kol_readdir(const char *path, uint32 max_blocks) {
ksys_file_status_t ret_result;
int result_buffer_size = sizeof (ksys_dir_entry_header_t)
+ max_blocks * sizeof (kol_readdir_result_entry);
kol_readdir_result *result_buffer = (kol_readdir_result *) malloc (result_buffer_size);
if (!result_buffer)
return nullptr;
memset(result_buffer, 0, result_buffer_size);
ret_result = _ksys_file_read_dir(path, 0, KSYS_FILE_UTF8, max_blocks, result_buffer);
// KSYS_FS_ERR_SUCCESS (0) is returned for normal dirs, KSYS_FS_ERR_EOF (6) for virtual directories
if (ret_result.status != 0 && ret_result.status != KSYS_FS_ERR_EOF) {
free (result_buffer);
return nullptr;
}
return result_buffer;
}
kol_readdir_result *kol_readdir(const char *path) {
kol_readdir_result *res = kol_readdir(path, 2);
if (!res)
return nullptr;
uint32 tot_blocks = res->header.files;
free(res);
return kol_readdir(path, tot_blocks);
}
bool getFileAttrs(Common::String path, uint32& attrs) {
ksys_file_info_t info;
memset(&info, 0, sizeof(info));
info.attr = KSYS_FILE_ATTR_DIR;
if(_ksys_file_info(path.c_str(), &info)) {
attrs = 0;
return false;
}
attrs = info.attr;
return true;
}
} // namespace
bool KolibriOSFilesystemNode::exists() const {
uint32 attrs;
return getFileAttrs(_path, attrs);
}
bool KolibriOSFilesystemNode::isReadable() const {
uint32 attrs;
return getFileAttrs(_path, attrs);
}
bool KolibriOSFilesystemNode::isWritable() const {
uint32 attrs;
return getFileAttrs(_path, attrs) && !(attrs & 0x01);
}
void KolibriOSFilesystemNode::setFlags() {
_isValid = getFileAttrs(_path, _attributes);
}
KolibriOSFilesystemNode::KolibriOSFilesystemNode(const Common::String &p) {
assert(p.size() > 0);
_path = p;
// Normalize the path (that is, remove unneeded slashes etc.)
_path = Common::normalizePath(_path, '/');
_displayName = Common::lastPathComponent(_path, '/');
setFlags();
}
AbstractFSNode *KolibriOSFilesystemNode::getChild(const Common::String &n) const {
assert(!_path.empty());
assert(isDirectory());
// Make sure the string contains no slashes
assert(!n.contains('/'));
// We assume here that _path is already normalized (hence don't bother to call
// Common::normalizePath on the final path).
Common::String newPath(_path);
if (_path.lastChar() != '/')
newPath += '/';
newPath += n;
return makeNode(newPath);
}
bool KolibriOSFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const {
kol_readdir_result *res = kol_readdir(_path.c_str());
if (!res)
return false;
for (int i = 0; i < (int) res->header.blocks; i++)
{
// Skip 'invisible' files if necessary
if (res->entries[i].fileName[0] == '.' && !hidden) {
continue;
}
// Skip '.' and '..' to avoid cycles
if ((res->entries[i].fileName[0] == '.' && res->entries[i].fileName[1] == 0)
|| (res->entries[i].fileName[0] == '.' && res->entries[i].fileName[1] == '.' && res->entries[i].fileName[2] == 0)) {
continue;
}
// Start with a clone of this node, with the correct path set
KolibriOSFilesystemNode entry(*this);
entry._displayName = res->entries[i].fileName;
if (_path.lastChar() != '/')
entry._path += '/';
entry._path += entry._displayName;
entry._isValid = true;
entry._attributes = res->entries[i].bdfe.attr;
// Honor the chosen mode
if ((mode == Common::FSNode::kListFilesOnly && entry.isDirectory()) ||
(mode == Common::FSNode::kListDirectoriesOnly && !entry.isDirectory()))
continue;
myList.push_back(new KolibriOSFilesystemNode(entry));
}
free(res);
return true;
}
AbstractFSNode *KolibriOSFilesystemNode::getParent() const {
if (_path == "/")
return 0; // The filesystem root has no parent
const char *start = _path.c_str();
const char *end = start + _path.size();
// Strip of the last component. We make use of the fact that at this
// point, _path is guaranteed to be normalized
while (end > start && *(end-1) != '/')
end--;
if (end == start) {
// This only happens if we were called with a relative path, for which
// there simply is no parent.
// TODO: We could also resolve this by assuming that the parent is the
// current working directory, and returning a node referring to that.
return 0;
}
return makeNode(Common::String(start, end));
}
Common::SeekableReadStream *KolibriOSFilesystemNode::createReadStream() {
return PosixIoStream::makeFromPath(getPath(), StdioStream::WriteMode_Read);
}
Common::SeekableWriteStream *KolibriOSFilesystemNode::createWriteStream(bool atomic) {
return PosixIoStream::makeFromPath(getPath(), atomic ?
StdioStream::WriteMode_WriteAtomic : StdioStream::WriteMode_Write);
}
bool KolibriOSFilesystemNode::createDirectory() {
if (mkdir(_path.c_str(), 0755) == 0)
setFlags();
return isDirectory();
}
namespace KolibriOS {
bool assureDirectoryExists(const Common::String &dir, const char *prefix) {
// Check whether the prefix exists if one is supplied.
if (prefix) {
uint32 attrs;
if (!getFileAttrs(prefix, attrs) || !KolibriOSFilesystemNode::isDirectory(attrs)) {
return false;
}
}
// Obtain absolute path.
Common::String path;
if (prefix) {
path = prefix;
path += '/';
path += dir;
} else {
path = dir;
}
path = Common::normalizePath(path, '/');
const Common::String::iterator end = path.end();
Common::String::iterator cur = path.begin();
if (cur[0] == '/' && cur[1] >= 1 && cur[1] <= 3 && cur[2] == '/')
cur += 2;
if (*cur == '/')
++cur;
do {
if (cur + 1 != end) {
if (*cur != '/') {
continue;
}
// It is kind of ugly and against the purpose of Common::String to
// insert 0s inside, but this is just for a local string and
// simplifies the code a lot.
*cur = '\0';
}
if (mkdir(path.c_str(), 0755) != 0) {
uint32 attrs;
if (!getFileAttrs(path, attrs) || !KolibriOSFilesystemNode::isDirectory(attrs))
return false;
}
*cur = '/';
} while (cur++ != end);
return true;
}
} // End of namespace KolibriOS
|