Bug 44498 - Writing HFFS-Workbook on an net-drive
Summary: Writing HFFS-Workbook on an net-drive
Status: RESOLVED WONTFIX
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: unspecified
Hardware: Other All
: P4 major (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-02-27 04:07 UTC by Carsten Eider
Modified: 2008-03-01 01:47 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Carsten Eider 2008-02-27 04:07:55 UTC
Trying to write data into opened xls-sheet from local disc is detected by a java.io.FileNotFoundException.

Trying to write on a opened xls-sheet form a net-mounted drive fails.
Data is overwritten.
Comment 1 Josh Micich 2008-02-28 01:36:40 UTC
The functionality you are describing seems to be outside of POI.

I can get a similar exception to the one you are describing to be thrown from java.io.FileOutputStream.open():
java.io.FileNotFoundException: \\192.168.1.13\share\temp\netExcelFile.xls (The process cannot access the file because it is being used by another process)
and
java.io.FileNotFoundException: c:\temp\localExcelFile.xls (The process cannot access the file because it is being used by another process)

I did a few tests, and I get the same error regardless of whether the the file is on local disk, or on a network share.  (For reference, I am using Sun JDK 1.4 on WinXP.  The local drive was NTFS, the share was SMB.  I called CreateFile() from the locking process with parameter dwShareMode = FILE_SHARE_READ).
Just to be sure I checked the JDK(6) source, and there seems to be no evidence there of differing behaviour for shares vs local files.

My guess is that the differing behaviour is caused by whatever application has the file open.  I tried running the same java code with Excel 2007 holding the files open, but in _neither_ case was an exception thrown.  Java could write to either file even though Excel had them open.  Which application are you using?

From what I can see, java doesn't provide any easy way to detect if another process has locked a file.  I found that a call to CreateFile( , FILE_ALL_ACCESS, 0, , , , ) reliably detects whether another app (including Excel) has the file open.  You could try some native code like the following:

-- FileLockChecker.java:
package example.io;
class FileLockChecker {
	public static boolean attemptFullAccess(File f) { ... }
	private static boolean attemptFullAccess0(String fileName);
}
-- FileLockChecker.c:
JNIEXPORT boolean JNICALL Java_example_io_FileLockChecker_attemptFullAccess0
(JNIEnv *env, jclass, jstring jstrFileName) {

  // convert java string to char*
	int len = env->GetStringUTFLength(jstrFileName);
	jboolean isCopy;
	const char* tempStr = env->GetStringUTFChars(jstrFileName, &isCopy);
	char fileName[MAX_PATH+1];
	strcpy(fileName, tempStr);
	if(isCopy == JNI_TRUE) {
		env->ReleaseStringUTFChars(jstrFileName, tempStr);
	}

  HANDLE hf = CreateFile(fileName, FILE_ALL_ACCESS, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  if(hf == INVALID_HANDLE_VALUE) {
    return JNI_FALSE
  }
  CloseHandle(hf);
	return JNI_TRUE;
}

--  --  --  --

Hope this helps somewhat.