+/**
+ * Write a full block of data.
+ */
+static int write_block(int fd,const char *buf,size_t size) {
+ size_t orig = size;
+ while ( size > 0 ) {
+ ssize_t n = write( fd, buf, size );
+ if ( n <= 0 ) {
+ return n;
+ }
+ }
+ return orig;
+}
+
+/**
+ * Insert a fragment at <off>. The data is appended to the pad file,
+ * and a descriptor is inserted; the fragment containing <off> is
+ * first split, unless <off> is at its start, and then new fragment
+ * descriptor is inserted.
+ */
+static int fusefile_write(const char *path, const char *buf, size_t size,
+ off_t off, struct fuse_file_info *fi)
+{
+#if DEBUG
+ fprintf( stderr, "fusefile_write( %s )\n", path );
+#endif
+ if ( strcmp( path, "/" ) != 0 ) {
+ return -ENOENT;
+ }
+#if DEBUG
+ fprintf( stderr, "write %ld %ld\n", off, size );
+#endif
+ // Ensure a pad was nominated
+ if ( pad.filename == 0 ) {
+ return 1;
+ }
+ static char meta[ 100 ];
+ sprintf( meta, "%ld\n%ld\n", off, size );
+ if ( write_block( pad.fd, meta, strlen( meta ) ) <= 0 ) {
+ perror( pad.filename );
+ return -EIO;
+ }
+ struct Source source = {
+ .filename = pad.filename,
+ .fd = pad.fd,
+ .from = lseek( pad.fd, 0, SEEK_END ),
+ .to = 0,
+ .start = 0
+ };
+ ssize_t n = write_block( pad.fd, buf, size );
+ if ( n != size ) {
+ return n;
+ }
+ source.to = source.from + size;
+ insert_source( &source, off );
+ return size;
+}
+