F
fastcars
I have a C program scanning a directory for files that get dumped there
by a java program.
What is happening is that the C program is picking these files up
before they are completely written. They then fail to parse and cause
data loss in the system. My intended solution is to exclusively lock
the file that is being written to in the java program, so the C
program will always get a complete file.
The java program is executing the file copy as below.
The C program below that again.
The java program doesnt see the C programs file lock under linux
And the C program doesnt see the java program file locks
2.6.10-1.741_FC3 #1 Thu Jan 13 16:35:56 EST 2005 x86_64 x86_64 x86_64
GNU/Linux
And the C program doesnt see the java locks.
Each small program below locks a file and waits for input from
keyboard.
The C program when run twice detects the lock in the second instance
and exits as you would expect.
I know the locking is advisory only on some platforms, however is linux
one of these ?
I cant find any docs saying this is the case or otherwise
Any one any ideas?
public static void copyFileWithLock(File from, File to,String
errorLine) throws Exception {
FileInputStream fis = null;
FileChannel fc = null;
Exception errt = null;
MyByteBuffer mbb = new MyByteBuffer();
try{
fis = new FileInputStream(from);
fc = new RandomAccessFile(to,"rw").getChannel();
FileLock lock = null;
try{
System.out.println("size :" + fc.size());
lock = fc.lock(0,fc.size(),false);
byte [] data = new byte[1024*4];
int read = 0;
System.out.println("have lock, waiting");
System.in.read();
while ( (read=fis.read(data))>0)
mbb.add(data,0,read);
if(errorLine!=null && errorLine.length()>0){
errorLine = "\n" + errorLine +"\n";
mbb.add(errorLine.getBytes(),0, errorLine.length());
}
fc.write(mbb.getBuffer());
} finally{
if(lock!=null)
lock.release();
}
}finally{
try{
if(fis!=null)
fis.close();
}catch(Exception err){
errt = err;
}
if(fc!=null)
fc.close();
}
if(errt!=null)
throw errt;
}
#include <sys/types.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>
int main(char **argc){
printf("hello world\n");
int fd = open("/tmp/nextto.ini", O_RDONLY);
if(fd<0){
printf("cannot open file\n");
} else {
if (flock(fd, LOCK_EX | LOCK_NB) < 0){
printf("unable to lock file\n");
} else {
printf("locked ok \n");
int waitChar = fgetc(stdin);
}
close(fd);
}
printf("exiting\n");
}
by a java program.
What is happening is that the C program is picking these files up
before they are completely written. They then fail to parse and cause
data loss in the system. My intended solution is to exclusively lock
the file that is being written to in the java program, so the C
program will always get a complete file.
The java program is executing the file copy as below.
The C program below that again.
The java program doesnt see the C programs file lock under linux
And the C program doesnt see the java program file locks
2.6.10-1.741_FC3 #1 Thu Jan 13 16:35:56 EST 2005 x86_64 x86_64 x86_64
GNU/Linux
And the C program doesnt see the java locks.
Each small program below locks a file and waits for input from
keyboard.
The C program when run twice detects the lock in the second instance
and exits as you would expect.
I know the locking is advisory only on some platforms, however is linux
one of these ?
I cant find any docs saying this is the case or otherwise
Any one any ideas?
public static void copyFileWithLock(File from, File to,String
errorLine) throws Exception {
FileInputStream fis = null;
FileChannel fc = null;
Exception errt = null;
MyByteBuffer mbb = new MyByteBuffer();
try{
fis = new FileInputStream(from);
fc = new RandomAccessFile(to,"rw").getChannel();
FileLock lock = null;
try{
System.out.println("size :" + fc.size());
lock = fc.lock(0,fc.size(),false);
byte [] data = new byte[1024*4];
int read = 0;
System.out.println("have lock, waiting");
System.in.read();
while ( (read=fis.read(data))>0)
mbb.add(data,0,read);
if(errorLine!=null && errorLine.length()>0){
errorLine = "\n" + errorLine +"\n";
mbb.add(errorLine.getBytes(),0, errorLine.length());
}
fc.write(mbb.getBuffer());
} finally{
if(lock!=null)
lock.release();
}
}finally{
try{
if(fis!=null)
fis.close();
}catch(Exception err){
errt = err;
}
if(fc!=null)
fc.close();
}
if(errt!=null)
throw errt;
}
#include <sys/types.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>
int main(char **argc){
printf("hello world\n");
int fd = open("/tmp/nextto.ini", O_RDONLY);
if(fd<0){
printf("cannot open file\n");
} else {
if (flock(fd, LOCK_EX | LOCK_NB) < 0){
printf("unable to lock file\n");
} else {
printf("locked ok \n");
int waitChar = fgetc(stdin);
}
close(fd);
}
printf("exiting\n");
}