How can I tell wheter a file is opened by an other application?
Is there a开发者_开发技巧 way to tell whether a file is opened by another application in PHP of Java?
Command fuser -v filename
will tell you everything you need to know:
$ fuser -v test.php
USER PID ACCESS COMMAND
test.php: guest 17983 F.... cat
I'd say No. Please read these threads.
- How to check if a file is already open by another process in C?
- Java: Check if file is already open
- How to check if a file has been opened by another application in C++
On windows you could download handle which is a command line tool to identify which windows file handles are owned by which processes.
Output looks like this:
Handle v3.46
Copyright (C) 1997-2011 Mark Russinovich
Sysinternals - www.sysinternals.com
------------------------------------------------------------------------------
System pid: 4 NT AUTHORITY\SYSTEM
84: File (R--) C:\System Volume Information\_restore{5D536487-92AI-5I25-9237-28AHSOU23728}\RP425\change.log
B4: File (RWD) C:\Documents and Settings\All Users\Application Data\avg9\Log\avgldr.log
728: File (-W-) C:\pagefile.sys
7A4: File (---) C:\WINDOWS\system32\config\SECURITY
(etc...)
Here an example application which uses handle.exe to determine if there is a handle on a file or directory (Windows only):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Application which determines which processes have a handle on a file or
* directory. Pass the file or directory to check as the first application
* parameter.
*
* This application uses handle.exe, which can be downloaded here:
* http://technet.microsoft.com/en-us/sysinternals/bb896655
*
* Copy handle.exe to C:/Program Files/handle/
*
* For the Runtime.exec() code I looked at:
* http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=2
*
* @author Adriaan
*/
public class Handle {
private static final String HANDLE_PATH = "C:/Program Files/handle/handle.exe";
private static final String DEFAULT_TARGET = "C:\\WINDOWS";
public static void main(String[] args) throws IOException,
InterruptedException {
checkOS();
String fileName = getFileName(args);
Process proc = executeCommand(fileName);
readResults(fileName, proc);
checkTermination(proc);
}
private static void checkOS() {
String osName = System.getProperty("os.name");
if (!osName.contains("Windows")) {
throw new IllegalStateException("Can only run under Windows");
}
}
private static String getFileName(String[] args) {
String fileName;
if (args != null && args.length > 0) {
fileName = args[0];
} else {
fileName = DEFAULT_TARGET;
}
return fileName;
}
private static Process executeCommand(String fileName) throws IOException {
String[] cmd = new String[] { HANDLE_PATH, fileName };
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
return proc;
}
private static void readResults(final String fileName, final Process proc) {
Thread errorHandler = new Thread() {
public void run() {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(proc.getErrorStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.err.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
Thread outputHandler = new Thread() {
public void run() {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
if (line.endsWith(fileName)) {
System.out.println(line);
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
errorHandler.start();
outputHandler.start();
}
private static void checkTermination(final Process proc)
throws InterruptedException {
int exitVal = proc.waitFor();
if (exitVal != 0) {
throw new IllegalStateException("Exitvalue " + exitVal);
}
}
}
You probably want to use file locking.
http://tuxradar.com/practicalphp/8/11/0
Edit: This is presuming you mean programatically with PHP or Java.
On linux you can scan through all the /proc/{pid}/fd/nnn file descriptors to see if the file in question is already open.
Using files to share data between running programs is generally a bad idea and error prone.
精彩评论