/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Linux implementation of WatchService based on inotify.
*
* In summary a background thread polls inotify plus a socket used for the wakeup
* mechanism. Requests to add or remove a watch, or close the watch service,
* cause the thread to wakeup and process the request. Events are processed
*/
class LinuxWatchService
extends AbstractWatchService
{
// background thread to read change events
// initialize inotify
int ifd = - 1;
try {
ifd = inotifyInit();
} catch (UnixException x) {
throw new IOException(x.errorString());
}
// configure inotify to be non-blocking
// create socketpair used in the close mechanism
int sp[] = new int[2];
try {
configureBlocking(ifd, false);
socketpair(sp);
} catch (UnixException x) {
throw new IOException(x.errorString());
}
}
throws IOException
{
// delegate to poller
}
// delegate to poller
}
/**
* WatchKey implementation
*/
// inotify descriptor
private final int ifd;
// watch descriptor
private volatile int wd;
}
int descriptor() {
return wd;
}
if (remove) {
try {
} catch (UnixException x) {
// ignore
}
}
wd = -1;
}
public boolean isValid() {
return (wd != -1);
}
public void cancel() {
if (isValid()) {
// delegate to poller
}
}
}
/**
* Background thread to read from inotify
*/
/**
* struct inotify_event {
* int wd;
* uint32_t mask;
* uint32_t len;
* char name __flexarr; // present if len > 0
* } act_t;
*/
// sizeof buffer for when polling inotify
// inotify file descriptor
private final int ifd;
// socketpair used to shutdown polling thread
private final int socketpair[];
// maps watch descriptor to Key
// address of read buffer
private final long address;
this.socketpair = sp;
}
// write to socketpair to wakeup polling thread
try {
} catch (UnixException x) {
throw new IOException(x.errorString());
}
}
{
int mask = 0;
continue;
}
continue;
}
continue;
}
}
// no modifiers supported at this time
return new NullPointerException();
continue; // ignore
return new UnsupportedOperationException("Modifier not supported");
}
}
// check file is directory
try {
} catch (UnixException x) {
return x.asIOException(dir);
}
if (!attrs.isDirectory()) {
}
// register with inotify (replaces existing mask if already registered)
int wd = -1;
try {
try {
} finally {
}
} catch (UnixException x) {
return new IOException("User limit of inotify watches reached");
}
return x.asIOException(dir);
}
// ensure watch descriptor is in map
}
return key;
}
// cancel single key
key.invalidate(true);
}
}
// close watch service
void implCloseAll() {
// invalidate all keys
}
// free resources
}
/**
* Poller main loop
*/
public void run() {
try {
for (;;) {
// wait for close or inotify event
// read from inotify
try {
} catch (UnixException x) {
throw x;
bytesRead = 0;
}
// process any pending requests
try {
boolean shutdown = processRequests();
if (shutdown)
break;
} catch (UnixException x) {
throw x;
}
}
// iterate over buffer to decode events
int offset = 0;
// file name
if (len > 0) {
// null-terminated and maybe additional null bytes to
// align the next event
while (actual > 0) {
break;
actual--;
}
if (actual > 0) {
}
}
// process event
}
}
} catch (UnixException x) {
x.printStackTrace();
}
}
/**
* map inotify event to WatchEvent.Kind
*/
return StandardWatchEventKinds.ENTRY_MODIFY;
return StandardWatchEventKinds.ENTRY_MODIFY;
return StandardWatchEventKinds.ENTRY_CREATE;
return StandardWatchEventKinds.ENTRY_CREATE;
return StandardWatchEventKinds.ENTRY_DELETE;
return StandardWatchEventKinds.ENTRY_DELETE;
return null;
}
/**
* Process event from inotify
*/
// overflow - signal all keys
}
return;
}
// lookup wd to get key
return; // should not happen
// file deleted
key.invalidate(false);
return;
}
// event for directory itself
return;
// map to event and queue to key
}
}
}
// -- native methods --
// sizeof inotify_event
private static native int eventSize();
// offsets of inotify_event
private static native int[] eventOffsets();
throws UnixException;
throws UnixException;
throws UnixException;
static {
return null;
}});
}
}