Page 1 of 1

The Holy Grail of POSIX

Posted: Sun Oct 01, 2006 5:23 am
by Habbit
After some days of null activity on the SFS forums, I call for help here...

------ORIGINAL POST IN THE SFS IMPLEMENTATION FORUM------

The source code I talk about is visible below or at the SVN repository of the SF.NET project simplefs-fuse

As some in the SFS community may know, I am writing a Linux SFS driver based on FUSE. However, I want the "-tools" package (currently just the mksimplefs utility) to work on as many systems as possible, so I don't have to port it should I want to write, for instance, a Mac OS X driver. My (foolish?) objective is working on all POSIX compliant systems.

One of the most important things mksimplefs does is figuring out how big the volume to be formatted is. For the sake of portability (I then thought), I implemented what any sane programmer would now call a "not-so-happy idea" - looks standard but is really system-dependant: I open the device to be formatted, seek to its end and get the current position. Simple as hell, isn't it? It wasn't. On Linux (my development platform), you can open block device nodes as binary streams and seek into them, but OpenBSD (and, I suspect, all BSD-like systems) are not so kind. On them, I get the fstream.failbit set even after trying to seek.

So the question is: how can I obtain the size of the volume to be formatted in a POSIX-standard way?


Very simple C++ code to test what I said:

Code: Select all

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
    fstream volume(argv[1], ios::in | ios::out | ios::binary);
    if (volume.fail())
        cerr << "Couldn't open " << argv[1] << endl;
    else {
        volume.seekp(0, ios::end);
        cout << volume.tellp() << endl;
        volume.close();
    }
}
Running this code on Linux with a 1400 KiB floppy in the drive specified by the first parameter will return 1474560, while doing so in OpenBSD will print "Couldn't open " + your floppy device WITH NO DRIVE ACTIVITY.

PS: Thanks BryNet-Inc for telling me about this quite big bug.

Re:The Holy Grail of POSIX

Posted: Sun Oct 01, 2006 1:39 pm
by Candy
Did you use the correct device? It could be that OpenBSD uses a different device name for the floppy.

Re:The Holy Grail of POSIX

Posted: Sun Oct 01, 2006 2:08 pm
by Habbit
Candy wrote: Did you use the correct device? It could be that OpenBSD uses a different device name for the floppy.
Nope. It does use a different device name (/dev/fd0 for linux, and /dev/fd0s0 IIRC for OpenBSD, tested with plain fd0 too), but I tried many possible namings. All fail.

Re:The Holy Grail of POSIX

Posted: Sun Oct 01, 2006 2:16 pm
by Candy
Habbit wrote:
Candy wrote: Did you use the correct device? It could be that OpenBSD uses a different device name for the floppy.
Nope. It does use a different device name (/dev/fd0 for linux, and /dev/fd0s0 IIRC for OpenBSD, tested with plain fd0 too), but I tried many possible namings. All fail.
I assume you tried it all as root too, and that you can just plain mount the same device with success?

Re:The Holy Grail of POSIX

Posted: Sun Oct 01, 2006 2:17 pm
by Habbit
Candy wrote:
Habbit wrote:
Candy wrote: Did you use the correct device? It could be that OpenBSD uses a different device name for the floppy.
Nope. It does use a different device name (/dev/fd0 for linux, and /dev/fd0s0 IIRC for OpenBSD, tested with plain fd0 too), but I tried many possible namings. All fail.
I assume you tried it all as root too, and that you can just plain mount the same device with success?
You'd assume right. IIRC, /dev/fd0s0 mounted succesfully (when formatted as ext2 or fat, of course).

Re:The Holy Grail of POSIX

Posted: Sun Oct 01, 2006 9:11 pm
by Brynet-Inc
It's always been /dev/fd0a or /dev/fd0c for me, And I use OpenBSD. :-\

Re:The Holy Grail of POSIX

Posted: Sun Oct 01, 2006 11:02 pm
by Habbit
That's what the "IIRC" is for. Since I am a mere mortal Windows user, I can be wrong (and I am constantly). Would you remember all the details about a Windows API if you absolutely had to port something there? (I know you wouldn't even get NEAR a Windows box otherwise)

Disclaimer: This post is NOT sarcastic, nor a joke about another user's exclusivistic OS preferences

Re:The Holy Grail of POSIX

Posted: Sun Oct 01, 2006 11:56 pm
by Candy
Can you cat the file normally? If so, I really don't get why it can't access it itself...

Re:The Holy Grail of POSIX

Posted: Fri Oct 06, 2006 3:23 pm
by João Jerónimo
Habbit wrote: So the question is: how can I obtain the size of the volume to be formatted in a POSIX-standard way?
I'm not sure whether it works for block devices (at least for regular files it works), but see the "stat" system call... There is a member of the structure returned by it that reports the file size...

JJ

Re:The Holy Grail of POSIX

Posted: Sun Oct 08, 2006 5:20 am
by Habbit
Nope. Doing stat(FILE*, struct stat*) on a block device doesn't work in either Linux or OpenBSD. I'm going to test it in the HURD also for the sake of completeness.

Re:The Holy Grail of POSIX

Posted: Sun Oct 08, 2006 5:23 pm
by B.E
The device name for the floppy on FreeBSD is fd0.

Do you have acess to the device (I think normaly you need to be root to acess a device and I don't have a FreeBSD box on me at the moment, I hope to get one running tonight anyway), Can you mount it? Another sugestion is see if you can find the size of the drive by using procfs. Another way is to use the df (or to look at the source code ;) ) to get the information.