Add .gitignore file for git.
[virt-p2v.git] / iso-attach
index 55bcd3e..a6aad26 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/ocamlrun /usr/bin/ocaml
 (* -*- tuareg -*- *)
-(* iso-attach attaches an updated 'virt-p2v.ml' file to the end of
+(* iso-attach attaches an updated 'virt-p2v' file to the end of
  * an ISO image.  This is just for quick developer builds because it
  * takes ages to rebuild a full ISO.
  *
@@ -50,7 +50,7 @@ open Unix
  * 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 *)
 
@@ -113,7 +113,7 @@ SYNOPSIS
 
 DESCRIPTION
 
-iso-attach attaches an updated 'virt-p2v.ml' file to the end of
+iso-attach attaches an updated 'virt-p2v' file to the end of
 an ISO image.  This is just for quick developer builds because it
 takes ages to rebuild a full ISO.
 
@@ -169,7 +169,8 @@ and do_add isoname attachment =
     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);
@@ -177,8 +178,8 @@ and do_add isoname attachment =
   (* 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);
@@ -192,14 +193,16 @@ and do_delete isoname =
   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
 
@@ -210,18 +213,18 @@ and do_get isoname output =
   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
@@ -242,3 +245,5 @@ and do_get isoname output =
   close fd
 
 let () = main ()
+
+(* This file must end with a newline. *)