< prev index next >

src/solaris/classes/sun/nio/fs/LinuxWatchService.java

Print this page
rev 13048 : 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
Reviewed-by: alanb
   1 /*
   2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 300             UnixNativeDispatcher.close(socketpair[1]);
 301             UnixNativeDispatcher.close(ifd);
 302         }
 303 
 304         /**
 305          * Poller main loop
 306          */
 307         @Override
 308         public void run() {
 309             try {
 310                 for (;;) {
 311                     int nReady, bytesRead;
 312 
 313                     // wait for close or inotify event
 314                     nReady = poll(ifd, socketpair[0]);
 315 
 316                     // read from inotify
 317                     try {
 318                         bytesRead = read(ifd, address, BUFFER_SIZE);
 319                     } catch (UnixException x) {
 320                         if (x.errno() != EAGAIN)
 321                             throw x;
 322                         bytesRead = 0;
 323                     }
 324 
 325                     // iterate over buffer to decode events
 326                     int offset = 0;
 327                     while (offset < bytesRead) {
 328                         long event = address + offset;
 329                         int wd = unsafe.getInt(event + OFFSETOF_WD);
 330                         int mask = unsafe.getInt(event + OFFSETOF_MASK);
 331                         int len = unsafe.getInt(event + OFFSETOF_LEN);
 332 
 333                         // file name
 334                         UnixPath name = null;
 335                         if (len > 0) {
 336                             int actual = len;
 337 
 338                             // null-terminated and maybe additional null bytes to
 339                             // align the next event
 340                             while (actual > 0) {


 348                                 unsafe.copyMemory(null, event + OFFSETOF_NAME,
 349                                     buf, Unsafe.ARRAY_BYTE_BASE_OFFSET, actual);
 350                                 name = new UnixPath(fs, buf);
 351                             }
 352                         }
 353 
 354                         // process event
 355                         processEvent(wd, mask, name);
 356 
 357                         offset += (SIZEOF_INOTIFY_EVENT + len);
 358                     }
 359 
 360                     // process any pending requests
 361                     if ((nReady > 1) || (nReady == 1 && bytesRead == 0)) {
 362                         try {
 363                             read(socketpair[0], address, BUFFER_SIZE);
 364                             boolean shutdown = processRequests();
 365                             if (shutdown)
 366                                 break;
 367                         } catch (UnixException x) {
 368                             if (x.errno() != UnixConstants.EAGAIN)
 369                                 throw x;
 370                         }
 371                     }
 372                 }
 373             } catch (UnixException x) {
 374                 x.printStackTrace();
 375             }
 376         }
 377 
 378 
 379         /**
 380          * map inotify event to WatchEvent.Kind
 381          */
 382         private WatchEvent.Kind<?> maskToEventKind(int mask) {
 383             if ((mask & IN_MODIFY) > 0)
 384                 return StandardWatchEventKinds.ENTRY_MODIFY;
 385             if ((mask & IN_ATTRIB) > 0)
 386                 return StandardWatchEventKinds.ENTRY_MODIFY;
 387             if ((mask & IN_CREATE) > 0)
 388                 return StandardWatchEventKinds.ENTRY_CREATE;


   1 /*
   2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 300             UnixNativeDispatcher.close(socketpair[1]);
 301             UnixNativeDispatcher.close(ifd);
 302         }
 303 
 304         /**
 305          * Poller main loop
 306          */
 307         @Override
 308         public void run() {
 309             try {
 310                 for (;;) {
 311                     int nReady, bytesRead;
 312 
 313                     // wait for close or inotify event
 314                     nReady = poll(ifd, socketpair[0]);
 315 
 316                     // read from inotify
 317                     try {
 318                         bytesRead = read(ifd, address, BUFFER_SIZE);
 319                     } catch (UnixException x) {
 320                         if (x.errno() != EAGAIN && x.errno() != EWOULDBLOCK)
 321                             throw x;
 322                         bytesRead = 0;
 323                     }
 324 
 325                     // iterate over buffer to decode events
 326                     int offset = 0;
 327                     while (offset < bytesRead) {
 328                         long event = address + offset;
 329                         int wd = unsafe.getInt(event + OFFSETOF_WD);
 330                         int mask = unsafe.getInt(event + OFFSETOF_MASK);
 331                         int len = unsafe.getInt(event + OFFSETOF_LEN);
 332 
 333                         // file name
 334                         UnixPath name = null;
 335                         if (len > 0) {
 336                             int actual = len;
 337 
 338                             // null-terminated and maybe additional null bytes to
 339                             // align the next event
 340                             while (actual > 0) {


 348                                 unsafe.copyMemory(null, event + OFFSETOF_NAME,
 349                                     buf, Unsafe.ARRAY_BYTE_BASE_OFFSET, actual);
 350                                 name = new UnixPath(fs, buf);
 351                             }
 352                         }
 353 
 354                         // process event
 355                         processEvent(wd, mask, name);
 356 
 357                         offset += (SIZEOF_INOTIFY_EVENT + len);
 358                     }
 359 
 360                     // process any pending requests
 361                     if ((nReady > 1) || (nReady == 1 && bytesRead == 0)) {
 362                         try {
 363                             read(socketpair[0], address, BUFFER_SIZE);
 364                             boolean shutdown = processRequests();
 365                             if (shutdown)
 366                                 break;
 367                         } catch (UnixException x) {
 368                             if (x.errno() != EAGAIN && x.errno() != EWOULDBLOCK)
 369                                 throw x;
 370                         }
 371                     }
 372                 }
 373             } catch (UnixException x) {
 374                 x.printStackTrace();
 375             }
 376         }
 377 
 378 
 379         /**
 380          * map inotify event to WatchEvent.Kind
 381          */
 382         private WatchEvent.Kind<?> maskToEventKind(int mask) {
 383             if ((mask & IN_MODIFY) > 0)
 384                 return StandardWatchEventKinds.ENTRY_MODIFY;
 385             if ((mask & IN_ATTRIB) > 0)
 386                 return StandardWatchEventKinds.ENTRY_MODIFY;
 387             if ((mask & IN_CREATE) > 0)
 388                 return StandardWatchEventKinds.ENTRY_CREATE;


< prev index next >