A simple yet effective way to lock file from reading it

Hi,

I would like to know a simple yet effective way to lock a file from reading and writing.

Thanks.
wsyyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

basicinstinctCommented:
File file = new File("/home/rick/foo");
if(file.exists()){
    file.setReadable(false, false);
}

http://docs.oracle.com/javase/6/docs/api/java/io/File.html#setReadable(boolean, boolean)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wsyyAuthor Commented:
I wonder if the said solution can prevent anyone from reading the file by editor software or the vi command in Linux.
0
wsyyAuthor Commented:
Maybe I would like more out of it.

I want to prevent anyone from reading, modifying, and moving the file either in JVM or at the OS level.
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

CEHJCommented:
Not possibly really without native code
0
wsyyAuthor Commented:
what does "native code" mean?
0
basicinstinctCommented:
setReadable setWriteable and setExecutable do change the underlying file permissions (as long as the java app has the permissions to change the permissions in the first place).

Also the underlying operating system needs to support such file permissions.
0
CEHJCommented:
what does "native code" mean?

It means code that's native to the OS - Linux in your case. You don't say what your actual goal is, so it's difficult to say much more
0
wsyyAuthor Commented:
My goal is to prevent anyone from touching, meaning reading or modifying or moving, the file my java program is reading from.

The simplest way I can think of is to pretend to write to the file (actually I just read it), such that no one is able to touch it.

But how can I achieve it?


basicinstinct-

I want to change the permission temporarily when my program is reading the file. Also, I want to delete the file after my program finishes reading it.

Will the solution you provided work to the end?
0
basicinstinctCommented:
hi wsyy - changing the file permissions is probably not quite what you want because someone will always be able to read it.

i can't help thinking you are trying to solve a problem the wrong way.

perhaps if you give us the problem (WHY do you want to lock the file) rather than the solution (lock the file) we could help you more...
0
wsyyAuthor Commented:
The file contains information and format that I don't want to disclose to a client, while I have to deploy the program as well as the file on client's server.

The whole picture is:

The original file is encrypted, which then will be dencrypted in run time to a file which we have been talking about.

The dencrypted file must not be read or copied during it being processed by my program. That is why I plan to lock it from being touched.

After my program finishes, the program will delete the dencrypted file.
0
basicinstinctCommented:
Must you decrypt it to the file system?
 What about just decrypting it in memory?
0
anilallewarCommented:
You can actually use the file channel methods to lock a file so that it is NOT accessible to any other process of the OS.

FileOutputStream fos= new FileOutputStream("file.txt");
FileLock fl = fos.getChannel().tryLock();

Please see the link below that I had found useful.

http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ314_030.htm
0
CEHJCommented:
FileLock fl = fos.getChannel().tryLock();

Open in new window


could be tried, but the api docs will tell you that the lock is 'advisory', meaning that the lock might not be honoured in all situations

Basicinstinct's suggestion to decrypt it into memory sounds like a good one - why can't you do that?
0
anilallewarCommented:
trylock() is a nice way of trying to obtain a lock on a file; but if you absolutely want a lock you can use the
lock()

Open in new window

method instead.

lock( ) blocks until the lock is acquired, or the thread that invoked lock( ) is interrupted, or the channel on which the lock( ) method is called is closed.

I also agree with the idea of decrypting into memory, but it depends on how many files need to be opened concurrently. If its a small number, then we should not have any problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.