Hi, what's the best (and if possible fastest) way to check for a valid file or drawer?
Using IDOS->Open()?
Using IDOS->Lock()?
Using IDOS->ExamineObject()?
Pros and Cons about above methods?
Now I'm using Open():
BPTR FileExists(CONST_STRPTR srcdir, CONST_STRPTR srcfile, STRPTR destpath, int32 path_size) { BPTR fh; IUtility->Strlcpy(destpath, srcdir, path_size); IDOS->AddPart(destpath, srcfile, path_size); fh = IDOS->Open(destpath, MODE_OLDFILE); IDOS->Close(fh); //IDOS->Printf("[%lx]'%s'\n",fh,destpath); return fh; }
TIA
Open -> only works on files. Open() on a directory fails.
Lock -> works on files and directories but does not tell you what it is.
ExamineObject -> works on both and tells you the type of the object
Speedwise I would say that Lock is fastest, Open is slowest and ExamineObject is somewhere inbetween.
ok, thx will do some tests and see what happens.
AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
Open() is unsuitable and Lock() has the problem of using Examine() with it being old and not liked by newer OS and SDK versions. Given you must allocate a FIB for use with Examine() as well you might as well use ExamineObject() if your code targets OS4.1. It's also less code to type as some things are done internally.
@thomas
To add to what you wrote Open() will also work with handlers which do not support locking (like DEV:, HTTP:, RANDOM:, AUDIO:, etc.) whereas Lock() will not.
Also the Open() method needs less code to be written and I doubt it is much slower than using ExamineObjectTags() which is also overkill for this purpose (ultimately which of these methods is faster/slower is very much dependent on the filesystem used).
@hypex
No idea what you are implying here. ExamineObjectTags() can be used with either a lock (EX_LockInput), a file handle (EX_FileHandleInput) or a string path to the object in question (EX_StringNameInput) in which case Lock()/Open() is called internally by the function.
Essentially it's a large file supporting replacement for the old and now obsolete Examine()/ExamineFH() functions.
Did some test using IDOS->Lock() and IDOS->Open(). Lock() seems a bit faster.
Various IDOS->Lock() results:
_RunTime 0.007032
_RunTime 0.007129
_RunTime 0.007078
Various IDOS->Open() results:
_RunTime 0.007091
_RunTime 0.007273
_RunTime 0.007037
Source code used, is part of the one I use on Mixer.docky:
AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
@jabirulo
If open() will succeed with handlers but lock() will not (as salass00 says), then combining Lock() and OpenFromLock() might be the fastest way to determine if an object exists and if it's a file or directory. Using Lock() first will succeed if a file or directory exists and using OpenFromLock() next will only succeed if the object is a file. Because ExamineObject() requires a subsequent call to FreeDosObject(), calls Lock() and UnLock() with filename input, and calls Open() & Close() if the filename is a handler, it might actually be faster to use a Lock()/OpenFromLock() combination in your code.
In other words, instead of using ExamineObject() it might be faster to use something like:
if (mylock=Lock("filename",SHARED_LOCK))
{
if (handle=OpenFromLock(mylock))
{
IsFile=TRUE;
Close(handle);
}
Unlock(mylock);
}
I'll leave it up to you to see which way is faster.
P.S. Since "-lauto" is now implicit if you don't use -nostartfiles or -nostartlibs, you don't need to open any libraries in your example code.
X1000 - OS 4.1FE
@xenic
Colin Wenzel doesn't have an account here but has asked me to reply.
I'm quoting from his e-mail:
So make sure that UnLock() is only called when OpenFromLock() fails and you should be fine. That is unless you are using this method with CrossDOSFileSystem older than 53.11.
@salass00
Thanks to you and Colin for pointing out my oversight. I couldn't find any way to edit my original post so I'll repost a corrected version here:
It's just a code snippit that is hopefully correct now:-)
I wonder how AmigaDOS reacts when you try to write to an opened directory on an old CrossDOSFileSystem?
X1000 - OS 4.1FE
Using 'IDOS->ExamineObjectTags()' seems as fast as 'IDOS->Open()':
_RunTime 0.007175
_RunTime 0.007393
_RunTime 0.007289
THX for all help guys!
AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
@salass00
What I was implying is that if Lock() is used then Examine() would be the next step. I didn't take ExamineObjectTags() into consideration as you can give it a file name and skip any need to lock an object. And in the OP a file name was being used.
When there is a post after yours I found your post is locked.
Regarding OpenFromLock() and freeing the lock. I have got caught on this myself. On a real Amiga with SFS used, it can freeze the system. On OS4 it will crash. If you look up the API docs you will see that it tells you not to free the lock on success. Funny how we have these slight overights on the docs that can have bigger consequences. :-)