Skip to content

Commit

Permalink
tar: simplify zero padding and to_sectors computations
Browse files Browse the repository at this point in the history
  • Loading branch information
robur-team committed Jan 3, 2024
1 parent 81943c3 commit 58c89b1
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/tar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module Header = struct
(** Unmarshal a pax Extended Header File time
It can contain a <period> ( '.' ) for sub-second granularity, that we ignore.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_05 *)
let unmarshal_pax_time (x:string ) : int64 =
let unmarshal_pax_time (x:string) : int64 =
match String.split_on_char '.' x with
| [seconds] -> Int64.of_string seconds
| [seconds; _subseconds] -> Int64.of_string seconds
Expand Down Expand Up @@ -632,19 +632,16 @@ module Header = struct
to a whole number of blocks *)
let compute_zero_padding_length (x: t) : int =
(* round up to next whole number of block lengths *)
let length = Int64.of_int length in
let lenm1 = Int64.sub length Int64.one in
let next_block_length = (Int64.mul length (Int64.div (Int64.add x.file_size lenm1) length)) in
Int64.to_int (Int64.sub next_block_length x.file_size)
let last_block_size = Int64.to_int (Int64.rem x.file_size (Int64.of_int length)) in
if last_block_size = 0 then 0 else length - last_block_size

(** Return the required zero-padding as a string *)
let zero_padding (x: t) =
let zero_padding_len = compute_zero_padding_length x in
Cstruct.sub zero_block 0 zero_padding_len

let to_sectors (x: t) =
let bytes = Int64.(add x.file_size (of_int (compute_zero_padding_length x))) in
Int64.div bytes 512L
Int64.(div (add (pred (of_int length)) x.file_size) (of_int length))
end

module type ASYNC = sig
Expand Down

0 comments on commit 58c89b1

Please sign in to comment.