+ * Get the file handle and offset, given an export offset.
+ *
+ * @param export An array of export files
+ * @param a The offset to get corresponding file/offset for
+ * @param fhandle [out] File descriptor
+ * @param foffset [out] Offset into fhandle
+ * @param maxbytes [out] Tells how many bytes can be read/written
+ * from fhandle starting at foffset (0 if there is no limit)
+ * @return 0 on success, -1 on failure
+ **/
+int get_filepos(GArray* export, off_t a, int* fhandle, off_t* foffset, size_t* maxbytes ) {
+ /* Negative offset not allowed */
+ if(a < 0)
+ return -1;
+
+ /* Binary search for last file with starting offset <= a */
+ FILE_INFO fi;
+ int start = 0;
+ int end = export->len - 1;
+ while( start <= end ) {
+ int mid = (start + end) / 2;
+ fi = g_array_index(export, FILE_INFO, mid);
+ if( fi.startoff < a ) {
+ start = mid + 1;
+ } else if( fi.startoff > a ) {
+ end = mid - 1;
+ } else {
+ start = end = mid;
+ break;
+ }
+ }
+
+ /* end should never go negative, since first startoff is 0 and a >= 0 */
+ g_assert(end >= 0);
+
+ fi = g_array_index(export, FILE_INFO, end);
+ *fhandle = fi.fhandle;
+ *foffset = a - fi.startoff;
+ *maxbytes = 0;
+ if( end+1 < export->len ) {
+ FILE_INFO fi_next = g_array_index(export, FILE_INFO, end+1);
+ *maxbytes = fi_next.startoff - a;
+ }
+
+ return 0;
+}
+
+/**