File IO takeover - Virtual filesystem

How can you create an empty file, and when any program reads from the file, the content shuld come from somewhere else. And when any program writes to the file the bytes to be written goes some where else then to the disk.

I have been looking at java.nio.. + FileChannel but I don't know if this would even be posible.

Just to get started I would like to have a simple program that "graps" on to an empty file, sending anything written to the file (by other programs) to the system output stream, and not to the empty file.

My ide would be something like this :

FileChannel cha = new RandomAccessFile("file","rw").getChannel();

// map/redirect cha to MyFileChannel

class MyFileChannel extends FileChannel {

// overwride methods in FileChannel

public ... write(...) {

System.out.println(...);

}

}

Would this be posible ? Have I misunderstood the FileChannel ?

[942 byte] By [puffhans2a] at [2007-11-27 6:46:10]
# 1
> How can you create an empty file, and when any> program reads from the file, the content shuld come> from somewhere else.This line makes no sense. Please clarify.
jellystonesa at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 2

> How can you create an empty file, and when any

> program reads from the file, the content shuld come

> from somewhere else.

The java program shuld deliver the file content resieved by any program that reads the file.

The "virtual" file content could come from a SQL DB.

So when the program is running the empty file "contains" some content from a DB. And when the program is not running the file is empty.

puffhans2a at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 3

> > How can you create an empty file, and when any

> > program reads from the file, the content shuld

> come

> > from somewhere else.

>

> The java program shuld deliver the file content

> resieved by any program that reads the file.

> The "virtual" file content could come from a SQL DB.

>

> So when the program is running the empty file

> "contains" some content from a DB. And when the

> program is not running the file is empty.

Not possible in java.

Doing it at all would require extensive interaction with the OS at a very basic level. C or C++ would probably be the language of choice.

jschella at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 4

Come on there must be a way.

I think i sounds just a litte bit posible reading the :

http://java.sun.com./javase/6/docs/api/java/nio/channels/FileChannel.html

If the file is empty or not after a write opperation, it not that important.

If I could just "grap" what is beeing written to the file and controll what is read from the file it would be great.

puffhans2a at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 5

> Come on there must be a way.

No.

You want to create a pseudo entry in the file system. The file system exists within the operating system. Thus you must hook into the OS to provide that sort of functionality.

And again doing that would have to be done at a very low level in the OS.

>

> If the file is empty or not after a write opperation,

> it not that important.

> If I could just "grap" what is beeing written to the

> file and controll what is read from the file it would

> be great.

Again no.

An application reads from a file using (at some point) very low level OS calls. Those calls interact with data on the hard driver to determine if the end of the file has been reached. That data is management data for the file, not data in the file itself.

Java interacts with the data not the management data. And even if java did have access to the management data you would still need to intercept calls to those low level calls.

jschella at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 6

> If I could just "grap" what is beeing written to the

> file and controll what is read from the file it would

> be great.

What are you trying to achieve, what is the functionality that you are trying to implement?

For example, it kind of sounds like what you want is to pipe data from one part of a program to another. You don't need files for that.

jsalonena at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 7

I would like to create a virtual file system, where the files are saved some where else(on a server) and you can work(using other programs) directly on the "virtual folder / file" when you are logged on and have the virtual folder / file open.

A other I can se, is to write a customized NFS or SMB server, and mount it as a network drive. This will just give a bunch of problems : ports, firewall, Linux non-root mount.

Is there any way of creating a virtual / fake usb drive and controll the IO ?

Other ideas.

puffhans2a at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 8

Assuming Linux, you can do it this way:

http://fuse.sourceforge.net/

I haven't delved deep into the documentation, but people have used FUSE to create things similar to what you are talking about.

Again, no java hooks or bindings, so you have to do it in C/C++, or if you absolutely insist, you can use it via Java through JNI.

kevjavaa at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 9
> A other I can se, is to write a customized NFS or SMB> server, and mount it as a network drive. This will> just give a bunch of problems : ports, firewall,> Linux non-root mount.>Which something has to deal with regardless.
jschella at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 10
> > Is there any way of creating a virtual / fake usb> drive and controll the IO ?Of course.There many with different variations depending on the specifics of what you want to do.
jschella at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 11

It is importent to me that the solution is platform indepentent : works on Linux( standard Ubuntu/Fedora/Nowell - desktop instalation ), Windows, Mac, Solaris.

The specifics of what I want to do :

Create a client and server program with users and passwords. when users login to the server using the client program they have ther own server space/directory and maby a shared server space/directory. This remote directory shuld be accesable throu the local file system, so that you can do all the stuff that you can do on real local files .I can't use a ftp server as there seam to be some restrictions on what you can do over the ftp protocol. I would also like to create an automated file-revision system so that when a file is saved in the virtual folder(on the server) the old file is not realy overwritten, it just saves the new file and then keeps track of witch one is the latest one, witch has to be delivered on the next read request.

There is alot of work in this, but where my problem is how to make the virtual folder/disk/usb-drive appear when users login and how to controll the IO of that device. No problem in sending data to and from the server.

Can I somehow create a fake usb-disk and controll the IO ?

puffhans2a at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 12
> Can I somehow create a fake usb-disk and controll the> IO ?You can ask as many times as you want to, and you'll still get variations of the same answer. Not in pure Java.
kevjavaa at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 13
@Op. Why do you want to do this? It sounds like you just want to use (or implement) telnet or rloginKaj
kajbja at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...
# 14

> It is importent to me that the solution is platform

> indepentent : works on Linux( standard

> Ubuntu/Fedora/Nowell - desktop instalation ),

> Windows, Mac, Solaris.

> ....

> This remote directory shuld be

> accesable throu the local file system, so that you

> can do all the stuff that you can do on real local

> files .

Stating it again - impossible. Doesn't even matter what language you use.

You are describing something that it fundamentally basic to an OS. There are probably OSes that doen't have a file system but you are unlikely to ever see one. Educational courses in Operating Systems always cover the file system.

A file system depends on the hardware of the system. A device that uses a flash rom is completely different than a system that uses a hard driver. But even systems that use hard drives use different hardware and assumptions about the data that is on the hardware.

If you look at the specification for C/C++ you will find that although IO operations are supported it is at the stream and random access level. There are no disk management interfaces in the specifications.

Each OS has a different API to access the file system. And specifics of that varies by OS.

Perhaps if you altered your requirements to the file level (not byte read level) then you can manage files. Even with java. You simply delete a old file and rename a file with new data to have the name of the old file. Then the next request gets the new data.

> Can I somehow create a fake usb-disk and controll the IO ?

Is it possible to do what you are asking (with no restrictions)? Yes. You would have to write a lot of software for each specific targeted system (and version specific as well.) You will need to use C/C++ and you will have to learn a LOT about the internals of each OS. You will need to learn a lot about C/C++ as well.

The solution will be very platform dependent. I would suspect that most of the code will specific to each target.

jschella at 2007-7-12 18:18:33 > top of Java-index,Java Essentials,Java Programming...