< prev index next >

src/java.base/linux/classes/sun/nio/fs/LinuxWatchService.java

Print this page
rev 50258 : [mq]: 8203369-Check-for-both-EAGAIN-and-EWOULDBLOCK-error-codes
   1 /*
   2  * Copyright (c) 2008, 2016, 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


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


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


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


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


< prev index next >