* stuff them into a tarball or ZIP file and attach that.
*)
-let magic = "ISOATTACHMENT002"
+let magic = "ISOATTACHMENT003"
let magiclen = String.length magic (* = 16 bytes *)
let trailerlen = magiclen + 8 + 8 (* magic + file start + true size *)
let size = file_size + trailerlen in
let over = size land 2047 in
if over > 0 then 2048-over else 0 in
- assert ((padding_size + file_size + trailerlen) land 2047 = 0);
+ let attachment_size = padding_size + file_size + trailerlen in
+ assert (attachment_size land 2047 = 0);
(* Write the padding. *)
ignore (write fd (String.make padding_size 'x') 0 padding_size);
(* Write the magic. *)
ignore (write fd magic 0 magiclen);
- (* Write the file start and true size. *)
- let buffer = string_of_int64 iso_size in
+ (* Write the attachment size and true file size. *)
+ let buffer = string_of_int64 (Int64.of_int attachment_size) in
ignore (write fd buffer 0 8);
let buffer = string_of_int64 (Int64.of_int file_size) in
ignore (write fd buffer 0 8);
if read fd buf 0 magiclen <> magiclen || buf <> magic then
failwith "no attachment found";
- (* Read the start offset of the file. *)
+ (* Read the size of the attachment *)
let buf = String.create 8 in
if read fd buf 0 8 <> 8 then
failwith "cannot read attachment size";
- let offset = int64_of_string buf in
+ let attachment_size = int64_of_string buf in
+
+ let orig_size = LargeFile.lseek fd (Int64.neg attachment_size) SEEK_END in
(* Truncate to start of the file. *)
- LargeFile.ftruncate fd offset;
+ LargeFile.ftruncate fd orig_size;
close fd
if read fd buf 0 magiclen <> magiclen || buf <> magic then
failwith "no attachment found";
- (* Read the start and size. *)
+ (* Read the attachment size and true size. *)
let buf = String.create 8 in
if read fd buf 0 8 <> 8 then
- failwith "cannot read attachment offset";
- let offset = int64_of_string buf in
+ failwith "cannot read attachment size";
+ let attachment_size = int64_of_string buf in
let buf = String.create 8 in
if read fd buf 0 8 <> 8 then
- failwith "cannot read attachment size";
+ failwith "cannot read file true size";
let size = Int64.to_int (int64_of_string buf) in
(* Seek to beginning of the attachment. *)
- ignore (LargeFile.lseek fd offset SEEK_SET);
+ ignore (LargeFile.lseek fd (Int64.neg attachment_size) SEEK_END);
(* Copy out the attachment. *)
let fd2 = openfile output [O_WRONLY; O_CREAT; O_TRUNC] 0o644 in