2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2002-11-13 17:16:18 +00:00
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-11-13 17:16:18 +00:00
|
|
|
*
|
2006-02-11 12:47:47 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-11-13 17:16:18 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-03 14:09:04 +00:00
|
|
|
#if defined(UNIX)
|
2003-11-03 22:20:51 +00:00
|
|
|
|
2008-08-22 11:19:41 +00:00
|
|
|
#include "backends/fs/posix/posix-fs.h"
|
2008-09-03 12:56:46 +00:00
|
|
|
#include "backends/fs/stdiostream.h"
|
2008-09-02 15:19:31 +00:00
|
|
|
#include "common/algorithm.h"
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2003-03-25 12:14:14 +00:00
|
|
|
#include <sys/param.h>
|
2002-11-15 17:05:50 +00:00
|
|
|
#include <sys/stat.h>
|
2002-11-13 17:16:18 +00:00
|
|
|
#include <dirent.h>
|
2002-11-15 17:54:38 +00:00
|
|
|
#include <stdio.h>
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2008-08-27 18:38:06 +00:00
|
|
|
#ifdef __OS2__
|
|
|
|
#define INCL_DOS
|
|
|
|
#include <os2.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-05-03 02:39:33 +00:00
|
|
|
void POSIXFilesystemNode::setFlags() {
|
|
|
|
struct stat st;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-05-03 02:39:33 +00:00
|
|
|
_isValid = (0 == stat(_path.c_str(), &st));
|
2007-06-04 03:46:56 +00:00
|
|
|
_isDirectory = _isValid ? S_ISDIR(st.st_mode) : false;
|
2004-02-01 01:31:50 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 14:55:19 +00:00
|
|
|
POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p) {
|
2006-04-03 21:18:24 +00:00
|
|
|
assert(p.size() > 0);
|
2004-02-01 01:31:50 +00:00
|
|
|
|
2007-12-09 17:28:29 +00:00
|
|
|
// Expand "~/" to the value of the HOME env variable
|
|
|
|
if (p.hasPrefix("~/")) {
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
if (home != NULL && strlen(home) < MAXPATHLEN) {
|
|
|
|
_path = home;
|
|
|
|
// Skip over the tilda. We know that p contains at least
|
|
|
|
// two chars, so this is safe:
|
|
|
|
_path += p.c_str() + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_path = p;
|
|
|
|
}
|
2008-09-02 15:19:31 +00:00
|
|
|
|
|
|
|
// Normalize the path (that is, remove unneeded slashes etc.)
|
|
|
|
_path = Common::normalizePath(_path, '/');
|
|
|
|
_displayName = Common::lastPathComponent(_path, '/');
|
|
|
|
|
2008-09-03 14:55:19 +00:00
|
|
|
// TODO: should we turn relative paths into absolute ones?
|
2008-09-02 15:19:31 +00:00
|
|
|
// Pro: Ensures the "getParent" works correctly even for relative dirs.
|
|
|
|
// Contra: The user may wish to use (and keep!) relative paths in his
|
|
|
|
// config file, and converting relative to absolute paths may hurt him...
|
|
|
|
//
|
|
|
|
// An alternative approach would be to change getParent() to work correctly
|
|
|
|
// if "_path" is the empty string.
|
|
|
|
#if 0
|
|
|
|
if (!_path.hasPrefix("/")) {
|
|
|
|
char buf[MAXPATHLEN+1];
|
|
|
|
getcwd(buf, MAXPATHLEN);
|
|
|
|
strcat(buf, "/");
|
|
|
|
_path = buf + _path;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// TODO: Should we enforce that the path is absolute at this point?
|
|
|
|
//assert(_path.hasPrefix("/"));
|
2006-04-23 12:29:43 +00:00
|
|
|
|
2008-09-03 14:55:19 +00:00
|
|
|
setFlags();
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2008-08-03 18:29:37 +00:00
|
|
|
AbstractFilesystemNode *POSIXFilesystemNode::getChild(const Common::String &n) const {
|
2008-09-03 14:55:19 +00:00
|
|
|
assert(!_path.empty());
|
2002-11-13 17:16:18 +00:00
|
|
|
assert(_isDirectory);
|
2008-09-02 15:19:31 +00:00
|
|
|
|
|
|
|
// Make sure the string contains no slashes
|
|
|
|
assert(Common::find(n.begin(), n.end(), '/') == n.end());
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-09-02 15:19:31 +00:00
|
|
|
// We assume here that _path is already normalized (hence don't bother to call
|
2008-09-03 14:55:19 +00:00
|
|
|
// Common::normalizePath on the final path).
|
2008-08-03 18:29:37 +00:00
|
|
|
Common::String newPath(_path);
|
2008-09-03 14:55:19 +00:00
|
|
|
if (_path != "/")
|
|
|
|
newPath += '/';
|
2007-05-03 02:39:33 +00:00
|
|
|
newPath += n;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-09-03 14:55:19 +00:00
|
|
|
return makeNode(newPath);
|
2007-05-03 02:39:33 +00:00
|
|
|
}
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2007-06-17 17:17:38 +00:00
|
|
|
bool POSIXFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const {
|
2007-05-03 02:39:33 +00:00
|
|
|
assert(_isDirectory);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-08-27 18:38:06 +00:00
|
|
|
#ifdef __OS2__
|
2008-09-02 15:19:31 +00:00
|
|
|
if (_path == "/") {
|
|
|
|
// Special case for the root dir: List all DOS drives
|
|
|
|
ULONG ulDrvNum;
|
|
|
|
ULONG ulDrvMap;
|
|
|
|
|
|
|
|
DosQueryCurrentDisk(&ulDrvNum, &ulDrvMap);
|
|
|
|
|
|
|
|
for (int i = 0; i < 26; i++) {
|
|
|
|
if (ulDrvMap & 1) {
|
|
|
|
char drive_root[4];
|
|
|
|
|
|
|
|
drive_root[0] = i + 'A';
|
|
|
|
drive_root[1] = ':';
|
|
|
|
drive_root[2] = '/';
|
|
|
|
drive_root[3] = 0;
|
|
|
|
|
|
|
|
POSIXFilesystemNode entry;
|
|
|
|
|
|
|
|
entry._isDirectory = true;
|
|
|
|
entry._isValid = true;
|
|
|
|
entry._path = drive_root;
|
|
|
|
entry._displayName = "[" + Common::String(drive_root, 2) + "]";
|
|
|
|
myList.push_back(new POSIXFilesystemNode(entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
ulDrvMap >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2008-08-27 18:38:06 +00:00
|
|
|
#endif
|
|
|
|
|
2007-05-03 02:39:33 +00:00
|
|
|
DIR *dirp = opendir(_path.c_str());
|
2002-11-13 17:16:18 +00:00
|
|
|
struct dirent *dp;
|
2002-12-27 00:13:51 +00:00
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
if (dirp == NULL)
|
2006-05-03 20:43:26 +00:00
|
|
|
return false;
|
2004-02-01 01:31:50 +00:00
|
|
|
|
2007-05-03 02:39:33 +00:00
|
|
|
// loop over dir entries using readdir
|
2002-11-13 17:16:18 +00:00
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
2007-06-17 17:17:38 +00:00
|
|
|
// Skip 'invisible' files if necessary
|
2007-06-20 00:28:04 +00:00
|
|
|
if (dp->d_name[0] == '.' && !hidden) {
|
2002-11-14 14:42:04 +00:00
|
|
|
continue;
|
2007-06-20 00:28:04 +00:00
|
|
|
}
|
|
|
|
// Skip '.' and '..' to avoid cycles
|
2007-09-18 20:16:33 +00:00
|
|
|
if ((dp->d_name[0] == '.' && dp->d_name[1] == 0) || (dp->d_name[0] == '.' && dp->d_name[1] == '.')) {
|
2002-11-14 14:42:04 +00:00
|
|
|
continue;
|
2007-06-20 00:28:04 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2008-09-03 14:55:19 +00:00
|
|
|
// Start with a clone of this node, with the correct path set
|
|
|
|
POSIXFilesystemNode entry(*this);
|
|
|
|
entry._displayName = dp->d_name;
|
|
|
|
if (_path != "/")
|
|
|
|
entry._path += '/';
|
|
|
|
entry._path += entry._displayName;
|
2002-11-15 17:05:50 +00:00
|
|
|
|
2006-06-03 14:09:04 +00:00
|
|
|
#if defined(SYSTEM_NOT_SUPPORTING_D_TYPE)
|
2007-05-03 02:39:33 +00:00
|
|
|
/* TODO: d_type is not part of POSIX, so it might not be supported
|
|
|
|
* on some of our targets. For those systems where it isn't supported,
|
|
|
|
* add this #elif case, which tries to use stat() instead.
|
2008-01-27 19:47:41 +00:00
|
|
|
*
|
2007-05-03 02:39:33 +00:00
|
|
|
* The d_type method is used to avoid costly recurrent stat() calls in big
|
|
|
|
* directories.
|
|
|
|
*/
|
|
|
|
entry.setFlags();
|
2006-04-03 21:18:24 +00:00
|
|
|
#else
|
2006-04-10 19:26:40 +00:00
|
|
|
if (dp->d_type == DT_UNKNOWN) {
|
|
|
|
// Fall back to stat()
|
2007-05-03 02:39:33 +00:00
|
|
|
entry.setFlags();
|
2006-04-04 23:52:56 +00:00
|
|
|
} else {
|
2006-04-10 19:26:40 +00:00
|
|
|
entry._isValid = (dp->d_type == DT_DIR) || (dp->d_type == DT_REG) || (dp->d_type == DT_LNK);
|
|
|
|
if (dp->d_type == DT_LNK) {
|
|
|
|
struct stat st;
|
2006-05-08 04:48:40 +00:00
|
|
|
if (stat(entry._path.c_str(), &st) == 0)
|
|
|
|
entry._isDirectory = S_ISDIR(st.st_mode);
|
|
|
|
else
|
|
|
|
entry._isDirectory = false;
|
2006-04-10 19:26:40 +00:00
|
|
|
} else {
|
|
|
|
entry._isDirectory = (dp->d_type == DT_DIR);
|
|
|
|
}
|
2006-04-04 23:52:56 +00:00
|
|
|
}
|
2003-11-03 22:20:51 +00:00
|
|
|
#endif
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2006-04-03 21:18:24 +00:00
|
|
|
// Skip files that are invalid for some reason (e.g. because we couldn't
|
|
|
|
// properly stat them).
|
|
|
|
if (!entry._isValid)
|
|
|
|
continue;
|
|
|
|
|
2002-11-19 01:36:47 +00:00
|
|
|
// Honor the chosen mode
|
2008-09-03 11:22:51 +00:00
|
|
|
if ((mode == Common::FilesystemNode::kListFilesOnly && entry._isDirectory) ||
|
|
|
|
(mode == Common::FilesystemNode::kListDirectoriesOnly && !entry._isDirectory))
|
2002-11-19 01:36:47 +00:00
|
|
|
continue;
|
2002-11-14 13:46:35 +00:00
|
|
|
|
2006-05-03 11:13:21 +00:00
|
|
|
myList.push_back(new POSIXFilesystemNode(entry));
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
closedir(dirp);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2006-05-03 20:43:26 +00:00
|
|
|
return true;
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2007-05-03 02:39:33 +00:00
|
|
|
AbstractFilesystemNode *POSIXFilesystemNode::getParent() const {
|
2004-11-21 13:18:07 +00:00
|
|
|
if (_path == "/")
|
2008-09-02 15:19:31 +00:00
|
|
|
return 0; // The filesystem root has no parent
|
2004-11-21 13:18:07 +00:00
|
|
|
|
|
|
|
const char *start = _path.c_str();
|
2008-09-02 15:19:31 +00:00
|
|
|
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
|
2008-09-03 14:55:19 +00:00
|
|
|
while (end > start && *(end-1) != '/')
|
2008-09-02 15:19:31 +00:00
|
|
|
end--;
|
|
|
|
|
2008-09-03 14:55:19 +00:00
|
|
|
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));
|
2006-04-30 22:52:10 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 12:56:46 +00:00
|
|
|
Common::SeekableReadStream *POSIXFilesystemNode::openForReading() {
|
|
|
|
return StdioStream::makeFromPath(getPath().c_str(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::WriteStream *POSIXFilesystemNode::openForWriting() {
|
|
|
|
return StdioStream::makeFromPath(getPath().c_str(), true);
|
|
|
|
}
|
|
|
|
|
2007-05-03 02:39:33 +00:00
|
|
|
#endif //#if defined(UNIX)
|