2002-11-13 17:16:18 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2002-2006 The ScummVM project
|
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
|
|
|
*/
|
|
|
|
|
2005-03-09 22:21:57 +00:00
|
|
|
#if defined(UNIX) || defined(__DC__)
|
2003-11-03 22:20:51 +00:00
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2006-05-03 10:14:05 +00:00
|
|
|
#include "backends/fs/abstract-fs.h"
|
2006-04-05 00:18:22 +00:00
|
|
|
#include "backends/fs/fs.h"
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2003-03-26 21:17:14 +00:00
|
|
|
#ifdef MACOSX
|
2002-11-13 17:16:18 +00:00
|
|
|
#include <sys/types.h>
|
2003-03-26 21:17:14 +00:00
|
|
|
#endif
|
2003-03-25 12:14:14 +00:00
|
|
|
#include <sys/param.h>
|
2002-11-15 17:05:50 +00:00
|
|
|
#include <sys/stat.h>
|
2003-11-03 22:20:51 +00:00
|
|
|
#ifndef __DC__
|
2002-11-13 17:16:18 +00:00
|
|
|
#include <dirent.h>
|
2003-11-03 22:20:51 +00:00
|
|
|
#endif
|
2002-11-15 17:54:38 +00:00
|
|
|
#include <stdio.h>
|
2003-03-25 12:14:14 +00:00
|
|
|
#include <unistd.h>
|
2002-11-13 17:16:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of the ScummVM file system API based on POSIX.
|
|
|
|
*/
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
class POSIXFilesystemNode : public AbstractFilesystemNode {
|
2002-11-13 17:16:18 +00:00
|
|
|
protected:
|
|
|
|
String _displayName;
|
|
|
|
bool _isDirectory;
|
|
|
|
bool _isValid;
|
|
|
|
String _path;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
public:
|
|
|
|
POSIXFilesystemNode();
|
2006-05-12 21:41:54 +00:00
|
|
|
POSIXFilesystemNode(const String &path, bool verify);
|
2002-11-13 17:16:18 +00:00
|
|
|
|
|
|
|
virtual String displayName() const { return _displayName; }
|
|
|
|
virtual bool isValid() const { return _isValid; }
|
|
|
|
virtual bool isDirectory() const { return _isDirectory; }
|
|
|
|
virtual String path() const { return _path; }
|
|
|
|
|
2006-05-03 20:43:26 +00:00
|
|
|
virtual bool listDir(AbstractFSList &list, ListMode mode) const;
|
2004-11-20 21:35:49 +00:00
|
|
|
virtual AbstractFilesystemNode *parent() const;
|
2006-04-30 22:52:10 +00:00
|
|
|
virtual AbstractFilesystemNode *child(const String &name) const;
|
2002-11-13 17:16:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-10-02 17:43:02 +00:00
|
|
|
static const char *lastPathComponent(const Common::String &str) {
|
2003-03-25 12:14:14 +00:00
|
|
|
const char *start = str.c_str();
|
|
|
|
const char *cur = start + str.size() - 2;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-03-25 12:14:14 +00:00
|
|
|
while (cur > start && *cur != '/') {
|
|
|
|
--cur;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2006-04-04 21:03:17 +00:00
|
|
|
return cur + 1;
|
2003-03-25 12:14:14 +00:00
|
|
|
}
|
|
|
|
|
2006-05-12 21:41:54 +00:00
|
|
|
AbstractFilesystemNode *AbstractFilesystemNode::getCurrentDirectory() {
|
|
|
|
char buf[MAXPATHLEN];
|
|
|
|
getcwd(buf, MAXPATHLEN);
|
|
|
|
return new POSIXFilesystemNode(buf, true);
|
|
|
|
}
|
|
|
|
|
2006-05-03 10:19:05 +00:00
|
|
|
AbstractFilesystemNode *AbstractFilesystemNode::getRoot() {
|
2002-11-13 17:16:18 +00:00
|
|
|
return new POSIXFilesystemNode();
|
|
|
|
}
|
|
|
|
|
2006-05-03 10:19:05 +00:00
|
|
|
AbstractFilesystemNode *AbstractFilesystemNode::getNodeForPath(const String &path) {
|
2006-04-23 12:29:43 +00:00
|
|
|
return new POSIXFilesystemNode(path, true);
|
2004-02-01 01:31:50 +00:00
|
|
|
}
|
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
POSIXFilesystemNode::POSIXFilesystemNode() {
|
2006-05-03 11:36:07 +00:00
|
|
|
/* The Browser code now saves the last browsed directory into the config file.
|
|
|
|
Hence the need to start at the "current" directory is far less, and we can
|
|
|
|
remove this hack for now. Still, there may be some need to obtain a ref
|
|
|
|
to the "current" directory. See the TODO list for some thoughts on this.
|
|
|
|
|
|
|
|
I am leaving this code in here for the time being, to be used as reference.
|
|
|
|
|
2006-04-03 22:06:42 +00:00
|
|
|
// FIXME: It is evil & slow to always call getcwd here.
|
|
|
|
// The intention behind this hack was/is to be more user friendly
|
|
|
|
// in our save/load dialogs: Instead of starting at the FS root,
|
|
|
|
// we start at the current directory. However, that's just a hack.
|
|
|
|
// Proper solution would be to extend FilesystemNode by the concept
|
|
|
|
// of 'current' or 'default' directory, and then modify the
|
|
|
|
// save/load dialogs to explicitly use that as starting point.
|
2003-03-25 12:14:14 +00:00
|
|
|
char buf[MAXPATHLEN];
|
2003-03-26 21:17:14 +00:00
|
|
|
getcwd(buf, MAXPATHLEN);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-03-25 12:14:14 +00:00
|
|
|
_path = buf;
|
|
|
|
_displayName = lastPathComponent(_path);
|
|
|
|
_path += '/';
|
2006-05-03 11:36:07 +00:00
|
|
|
*/
|
|
|
|
// The root dir.
|
2003-03-25 12:14:14 +00:00
|
|
|
_path = "/";
|
|
|
|
_displayName = _path;
|
2002-11-13 17:16:18 +00:00
|
|
|
_isValid = true;
|
|
|
|
_isDirectory = true;
|
|
|
|
}
|
|
|
|
|
2006-04-30 22:52:10 +00:00
|
|
|
POSIXFilesystemNode::POSIXFilesystemNode(const String &p, bool verify) {
|
2006-04-03 21:18:24 +00:00
|
|
|
assert(p.size() > 0);
|
2004-02-01 01:31:50 +00:00
|
|
|
|
|
|
|
_path = p;
|
2006-04-03 21:18:24 +00:00
|
|
|
_displayName = lastPathComponent(_path);
|
2002-11-13 17:16:18 +00:00
|
|
|
_isValid = true;
|
|
|
|
_isDirectory = true;
|
2006-04-23 12:29:43 +00:00
|
|
|
|
2006-04-30 22:52:10 +00:00
|
|
|
if (verify) {
|
|
|
|
#ifdef __DC__
|
|
|
|
/*
|
2006-05-02 22:27:24 +00:00
|
|
|
FIXME: Why should I need to add a lot of code
|
|
|
|
just to figure out if a path exists / is a directory here?
|
|
|
|
We should know what we want it to be, and when open / listDir
|
|
|
|
is called it will become apparent if it isn't. Just
|
|
|
|
checking for the sake of checking seems bogus to me.
|
2006-04-30 22:52:10 +00:00
|
|
|
*/
|
|
|
|
#else
|
2006-04-23 12:29:43 +00:00
|
|
|
struct stat st;
|
|
|
|
_isValid = (0 == stat(_path.c_str(), &st));
|
2006-05-08 04:48:40 +00:00
|
|
|
_isDirectory = _isValid ? S_ISDIR(st.st_mode) : false;
|
2006-04-23 12:29:43 +00:00
|
|
|
#endif
|
2006-04-30 22:52:10 +00:00
|
|
|
}
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2006-05-03 20:43:26 +00:00
|
|
|
bool POSIXFilesystemNode::listDir(AbstractFSList &myList, ListMode mode) const {
|
2002-11-13 17:16:18 +00:00
|
|
|
assert(_isDirectory);
|
|
|
|
DIR *dirp = opendir(_path.c_str());
|
|
|
|
|
|
|
|
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
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
// ... loop over dir entries using readdir
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
2002-11-14 14:42:04 +00:00
|
|
|
// Skip 'invisible' files
|
|
|
|
if (dp->d_name[0] == '.')
|
|
|
|
continue;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2006-04-03 21:18:24 +00:00
|
|
|
String newPath(_path);
|
|
|
|
if (newPath.lastChar() != '/')
|
|
|
|
newPath += '/';
|
|
|
|
newPath += dp->d_name;
|
|
|
|
|
2006-05-12 21:41:54 +00:00
|
|
|
POSIXFilesystemNode entry(newPath, false);
|
2002-11-15 17:05:50 +00:00
|
|
|
|
2003-11-03 22:20:51 +00:00
|
|
|
#ifdef __DC__
|
|
|
|
entry._isDirectory = dp->d_size < 0;
|
2006-04-10 19:26:40 +00:00
|
|
|
#elif defined(SYSTEM_NOT_SUPPORTING_D_TYPE)
|
2006-04-03 21:18:24 +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.
|
|
|
|
struct stat st;
|
|
|
|
entry._isValid = (0 == stat(entry._path.c_str(), &st));
|
2006-05-08 04:48:40 +00:00
|
|
|
entry._isDirectory = entry._isValid ? S_ISDIR(st.st_mode) : false;
|
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()
|
2006-04-04 23:52:56 +00:00
|
|
|
struct stat st;
|
2006-04-10 19:26:40 +00:00
|
|
|
entry._isValid = (0 == stat(entry._path.c_str(), &st));
|
2006-05-08 04:48:40 +00:00
|
|
|
entry._isDirectory = entry._isValid ? S_ISDIR(st.st_mode) : false;
|
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
|
2006-05-03 10:14:05 +00:00
|
|
|
if ((mode == FilesystemNode::kListFilesOnly && entry._isDirectory) ||
|
|
|
|
(mode == FilesystemNode::kListDirectoriesOnly && !entry._isDirectory))
|
2002-11-19 01:36:47 +00:00
|
|
|
continue;
|
2002-11-14 13:46:35 +00:00
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
if (entry._isDirectory)
|
|
|
|
entry._path += "/";
|
2006-05-03 11:13:21 +00:00
|
|
|
myList.push_back(new POSIXFilesystemNode(entry));
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
closedir(dirp);
|
2006-05-03 20:43:26 +00:00
|
|
|
return true;
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
AbstractFilesystemNode *POSIXFilesystemNode::parent() const {
|
2004-11-21 13:18:07 +00:00
|
|
|
if (_path == "/")
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const char *start = _path.c_str();
|
|
|
|
const char *end = lastPathComponent(_path);
|
|
|
|
|
2006-05-12 21:41:54 +00:00
|
|
|
POSIXFilesystemNode *p = new POSIXFilesystemNode(String(start, end - start), false);
|
2004-11-21 13:18:07 +00:00
|
|
|
|
2002-11-15 17:54:38 +00:00
|
|
|
return p;
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2006-04-30 22:52:10 +00:00
|
|
|
AbstractFilesystemNode *POSIXFilesystemNode::child(const String &name) const {
|
|
|
|
// FIXME: Pretty lame implementation! We do no error checking to speak
|
|
|
|
// of, do not check if this is a special node, etc.
|
|
|
|
assert(_isDirectory);
|
|
|
|
String newPath(_path);
|
|
|
|
if (_path.lastChar() != '/')
|
|
|
|
newPath += '/';
|
|
|
|
newPath += name;
|
|
|
|
POSIXFilesystemNode *p = new POSIXFilesystemNode(newPath, true);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
#endif // defined(UNIX)
|