Ticket #8583: abox-fs.cpp.patch

File abox-fs.cpp.patch, 7.6 KB (added by SF/fab1, 17 years ago)

*working* Morphos fs backend patch.

  • .cpp

    old new  
    1616 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
    1717 *
    1818 * $URL: https://svn.sourceforge.net/svnroot/scummvm/scummvm/trunk/backends/fs/morphos/abox-fs.cpp $
    19  * $Id: abox-fs.cpp 23974 2006-09-23 00:42:35Z fingolfin $
     19 * $Id: abox-fs.cpp 23274 2006-06-24 08:07:48Z fingolfin $
    2020 */
    2121
    2222#if defined(__MORPHOS__)
     
    2424#include <proto/dos.h>
    2525
    2626#include <stdio.h>
     27#include <sys/stat.h>
    2728
    28 #include "engines/engine.h"
     29#include <common/stdafx.h>
     30
     31#include "common/util.h"
     32#include "base/engine.h"
    2933#include "backends/fs/abstract-fs.h"
    3034
    3135/*
     
    4347        public:
    4448                ABoxFilesystemNode();
    4549                ABoxFilesystemNode(BPTR lock, CONST_STRPTR display_name = NULL);
     50                ABoxFilesystemNode(const String &p);
     51                ABoxFilesystemNode(const ABoxFilesystemNode &node);
     52
    4653                ~ABoxFilesystemNode();
    4754
    4855                virtual String displayName() const { return _displayName; }
    49                 virtual String name() const { return _displayName; }
     56                virtual String name() const { return _displayName; };
    5057                virtual bool isValid() const { return _isValid; }
    5158                virtual bool isDirectory() const { return _isDirectory; }
    5259                virtual String path() const { return _path; }
     
    5461                virtual bool listDir(AbstractFSList &list, ListMode mode) const;
    5562                static  AbstractFSList listRoot();
    5663                virtual AbstractFilesystemNode *parent() const;
    57                 virtual AbstractFilesystemNode *child(const String &n) const;
     64                virtual AbstractFilesystemNode *child(const String &name) const;
    5865};
    5966
    6067
     
    6269        return AbstractFilesystemNode::getRoot();
    6370}
    6471
     72AbstractFilesystemNode *AbstractFilesystemNode::getNodeForPath(const String     &path) {
     73        return new ABoxFilesystemNode(path);
     74}
     75
    6576AbstractFilesystemNode *AbstractFilesystemNode::getRoot()
    6677{
    6778        return new ABoxFilesystemNode();
     
    8394        _lock = NULL;
    8495        for (;;)
    8596        {
    86                 char n[bufsize];
    87                 if (NameFromLock(lock, n, bufsize) != DOSFALSE)
     97                char name[bufsize];
     98                if (NameFromLock(lock, name, bufsize) != DOSFALSE)
    8899                {
    89                         _path = n;
    90                         _displayName = display_name ? display_name : FilePart(n);
     100                        _path = name;
     101                        _displayName = display_name ? display_name : FilePart(name);
    91102                        break;
    92103                }
    93104                if (IoErr() != ERROR_LINE_TOO_LONG)
    94105                {
    95106                        _isValid = false;
    96                         warning("Error while retrieving path name: %d", IoErr());
     107                        debug(6, "Error while retrieving path name: %ld", IoErr());
    97108                        return;
    98109                }
    99110                bufsize *= 2;
    100111        }
    101112
     113        _isDirectory = false;
    102114        _isValid = false;
    103115
    104116        FileInfoBlock *fib = (FileInfoBlock*) AllocDosObject(DOS_FIB, NULL);
    105117        if (fib == NULL)
    106118        {
    107                 warning("Failed to allocate memory for FileInfoBlock");
     119                debug(6, "Failed to allocate memory for FileInfoBlock");
    108120                return;
    109121        }
    110122
     
    119131                        _isValid = (_lock != NULL);
    120132                }
    121133                else
     134                {
    122135                        _isValid = true;
     136                }
    123137        }
    124138        FreeDosObject(DOS_FIB, fib);
    125139}
    126140
     141ABoxFilesystemNode::ABoxFilesystemNode(const String &p) {
     142        int len = 0, offset = p.size();
     143
     144        assert(offset > 0);
     145
     146        _path = p;
     147
     148        // Extract last component from path
     149        const char *str = p.c_str();
     150        while (offset > 0 && (str[offset-1] == '/' || str[offset-1] == ':') )
     151                offset--;
     152        while (offset > 0 && (str[offset-1] != '/' && str[offset-1] != ':')) {
     153                len++;
     154                offset--;
     155        }
     156        _displayName = String(str + offset, len);
     157        _lock = NULL;
     158        _isDirectory = false;
     159
     160        struct FileInfoBlock *fib = (struct FileInfoBlock *)AllocDosObject(DOS_FIB, NULL);
     161        if (!fib)
     162        {
     163                debug(6, "FileInfoBlock is NULL");
     164                return;
     165        }
     166
     167        // Check whether the node exists and if it is a directory
     168
     169        BPTR pLock = Lock((STRPTR)_path.c_str(), SHARED_LOCK);
     170        if (pLock)
     171        {
     172                if (Examine(pLock, fib) != DOSFALSE) {
     173                        if (fib->fib_EntryType > 0)
     174                        {
     175                                _isDirectory = true;
     176                                _lock = DupLock(pLock);
     177                                _isValid = (_lock != 0);
     178
     179                                // Add a trailing slash if it is needed
     180                                const char c = _path.lastChar();
     181                                if (c != '/' && c != ':')
     182                                        _path += '/';
     183
     184                        }
     185                        else
     186                        {
     187                                _isDirectory = false;
     188                                _isValid = true;
     189                        }
     190                }
     191       
     192                UnLock(pLock);
     193        }
     194
     195        FreeDosObject(DOS_FIB, fib);
     196}
     197
     198ABoxFilesystemNode::ABoxFilesystemNode(const ABoxFilesystemNode& node)
     199{
     200        _displayName = node._displayName;
     201        _isValid = node._isValid;
     202        _isDirectory = node._isDirectory;
     203        _path = node._path;
     204        _lock = DupLock(node._lock);
     205}
     206
    127207ABoxFilesystemNode::~ABoxFilesystemNode()
    128208{
    129209        if (_lock)
     
    136216bool ABoxFilesystemNode::listDir(AbstractFSList &myList, ListMode mode) const
    137217{
    138218        if (!_isValid)
    139                 error("listDir() called on invalid node");
    140 
     219        {
     220                debug(6, "listDir() called on invalid node");
     221                return false;
     222        }
    141223        if (!_isDirectory)
    142                 error("listDir() called on file node");
     224        {
     225                debug(6, "listDir() called on file node");
     226                return false;
     227        }
    143228
    144229        if (_lock == NULL)
    145230        {
     
    153238
    154239        if (fib == NULL)
    155240        {
    156                 warning("Failed to allocate memory for FileInfoBlock");
     241                debug(6, "Failed to allocate memory for FileInfoBlock");
    157242                return false;
    158243        }
    159244
     
    165250                        String full_path;
    166251                        BPTR lock;
    167252
    168                         if ((fib->fib_EntryType > 0 && (mode & FilesystemNode::kListDirectoriesOnly)) ||
     253                        if ((mode == FilesystemNode::kListAll) ||
     254                             (fib->fib_EntryType > 0 && (mode & FilesystemNode::kListDirectoriesOnly)) ||
    169255                                 (fib->fib_EntryType < 0 && (mode & FilesystemNode::kListFilesOnly)))
    170256                        {
    171257                                full_path = _path;
     
    173259                                lock = Lock(full_path.c_str(), SHARED_LOCK);
    174260                                if (lock)
    175261                                {
    176                                         entry = new ABoxFilesystemNode(lock);
     262                                        entry = new ABoxFilesystemNode(lock, fib->fib_FileName);
    177263                                        if (entry)
    178264                                        {
    179265                                                if (entry->isValid())
     
    187273                }
    188274
    189275                if (IoErr() != ERROR_NO_MORE_ENTRIES)
    190                         warning("Error while reading directory: %d", IoErr());
     276                        debug(6, "Error while reading directory: %ld", IoErr());
    191277        }
    192278
    193279        FreeDosObject(DOS_FIB, fib);
    194280
    195         return tree;
     281        return true;
    196282}
    197283
    198284AbstractFilesystemNode *ABoxFilesystemNode::parent() const
     
    200286        AbstractFilesystemNode *node = NULL;
    201287
    202288        if (!_isDirectory)
    203                 error("parent() called on file node");
     289        {
     290                debug(6, "parent() called on file node");
     291                return NULL;
     292        }
    204293
    205294        if (_lock == NULL) {
    206295                /* Parent of the root is the root itself */
    207                 node = 0;
     296                return new ABoxFilesystemNode(*this);
    208297        } else {
    209298                BPTR parent_lock = ParentDir(_lock);
    210299                if (parent_lock) {
     
    217306        return node;
    218307}
    219308
    220 AbstractFilesystemNode *ABoxFilesystemNode::child(const String &n) const {
    221         TODO
     309AbstractFilesystemNode *ABoxFilesystemNode::child(const String &name) const {
     310        assert(_isDirectory);
     311        String newPath(_path);
     312
     313        if (_path.lastChar() != '/')
     314                newPath += '/';
     315        newPath += name;
     316
     317        BPTR lock = Lock(newPath.c_str(), SHARED_LOCK);
     318
     319        if (!lock)
     320        {
     321                return 0;
     322    }
     323
     324        UnLock(lock);
     325
     326        return new ABoxFilesystemNode(newPath);
    222327}
    223328
    224329AbstractFSList ABoxFilesystemNode::listRoot()
     
    226331        AbstractFSList myList;
    227332        DosList *dosList;
    228333        CONST ULONG lockDosListFlags = LDF_READ | LDF_VOLUMES;
    229         char n[256];
     334        char name[256];
    230335
    231336        dosList = LockDosList(lockDosListFlags);
    232337        if (dosList == NULL)
    233338        {
    234                 warning("Could not lock dos list");
    235339                return myList;
    236340        }
    237341
     
    248352                        CONST_STRPTR device_name = (CONST_STRPTR)((struct Task *)dosList->dol_Task->mp_SigTask)->tc_Node.ln_Name;
    249353                        BPTR volume_lock;
    250354
    251                         strcpy(n, volume_name);
    252                         strcat(n, ":");
    253                         volume_lock = Lock(n, SHARED_LOCK);
     355                        strcpy(name, volume_name);
     356                        strcat(name, ":");
     357                        volume_lock = Lock(name, SHARED_LOCK);
    254358                        if (volume_lock)
    255359                        {
    256                                 sprintf(n, "%s (%s)", volume_name, device_name);
    257                                 entry = new ABoxFilesystemNode(volume_lock, n);
     360                                sprintf(name, "%s (%s)", volume_name, device_name);
     361                                entry = new ABoxFilesystemNode(volume_lock, name);
    258362                                if (entry)
    259363                                {
    260364                                        if (entry->isValid())