+ * Poll for IO readiness.
+ */
+int fusefile_poll(const char *path, struct fuse_file_info *fi,
+ struct fuse_pollhandle *ph, unsigned *reventsp )
+{
+#if DEBUG
+ fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
+#endif
+ if( strcmp( path, "/" ) != 0 ) {
+ return -ENOENT;
+ }
+ if ( ph ) {
+ return fuse_notify_poll( ph );
+ }
+ return 0;
+}
+
+static void overlay_load() {
+ lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
+ size_t x = 0;
+ size_t size = sizeof( overlay.count );
+ if ( read( overlay.source.fd, &x, size ) != size ) {
+ return;
+ }
+#if DEBUG
+ fprintf( stderr, "overlay: %s with %ld regions\n",
+ overlay.source.filename, x );
+#endif
+ struct Region f = { 0, 0 };
+ size = sizeof( struct Region );
+ while ( x-- > 0 ) {
+ if ( read( overlay.source.fd, &f, size ) != size ) {
+ fprintf( stderr, "%s: bad meta data\n", overlay.source.filename );
+ exit( 1 );
+ }
+#if DEBUG
+ fprintf( stderr, "overlay region: %ld %ld\n", f.beg, f.end );
+#endif
+ overlay_mark( f.beg, f.end );
+ }
+}
+
+/**
+ * Inject an overlay fragment into the source table, end return the
+ * end of the injected fragment.
+ */
+static off_t overlay_inject_from_region(off_t beg,off_t end) {
+ int index = find_source( beg );
+ if ( index < 0 ) {
+ fprintf( stderr, "** Injecting %s at %ld failed\n",
+ overlay.source.filename, beg );
+ usage();
+ }
+ if ( end > ENDSOURCE( sources.array[ index ] ) ) {
+ end = ENDSOURCE( sources.array[ index ] );
+ }
+ struct Region frags[3] = {
+ { sources.array[ index ].start, beg },
+ { beg, ENDSOURCE( sources.array[ index ] ) },
+ { ENDSOURCE( sources.array[ index ] ), end } };
+ ssize_t size = frags[0].end - frags[0].beg;
+ if ( size ) {
+ // "Duplicate" the indexed source data, copying the filename
+ dup_source_item( index );
+ sources.array[ index ].to = sources.array[ index ].from + size;
+ index++;
+ sources.array[ index ].start = beg;
+ sources.array[ index ].from = sources.array[ index-1 ].to;
+ }
+ size = frags[2].end - frags[2].beg;
+ if ( size ) {
+ dup_source_item( index );
+ sources.array[ index+1 ].start = frags[2].beg;
+ sources.array[ index+1 ].from = sources.array[ index+1 ].to -size;
+ }
+ // Replace the [index] fragment
+ sources.array[ index ].filename = overlay.source.filename;
+ sources.array[ index ].start = beg;
+ sources.array[ index ].from = beg;
+ sources.array[ index ].to = end;
+ sources.array[ index ].fd = overlay.source.fd; //?
+ return end;
+}
+
+/**
+ * Inject the current (unopened) overlay into the source list.