Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to create a packed img from a directory #62

Open
dvc94ch opened this issue Apr 22, 2022 · 3 comments
Open

how to create a packed img from a directory #62

dvc94ch opened this issue Apr 22, 2022 · 3 comments

Comments

@dvc94ch
Copy link

dvc94ch commented Apr 22, 2022

Looking into this a bit, I'm not quite sure how to estimate the number of sectors to provide during formatting so that a given directory can be stored in the file system with as little overhead as possible. The use case for this is creating apple disk images (dmg) with a FAT32 file system for distribution of appbundles, which currently requries playing around with the total_sectors parameter of FormatVolumeOptions.

@rafalh
Copy link
Owner

rafalh commented Apr 23, 2022

Well, it depends on many things:

  • how long are filenames (each started 13 characters contribute to additional LFN entry)
  • how long are files (a lot of small files would make internal fragmentation in clusters an important factor)
  • how nested is the directory tree (one big directory or a more complex structure - each directory takes some space)

There are also some heuristics in volume formatting code that determine FAT type (FAT12/16/32), FAT size, cluster size, etc. and all those values affect the final size. Because of all that it would be very hard to calculate the optimal size. Perhaps implementing some heuristics that determines close-enough size would be okay for you use case. The simplest one would be to use experimentally determined size multiplier e.g. total_size_of_files * X. Perhaps something like total_size_of_files * 1.1 would give enough space for all filesystem structures and internal fragmentation, but that's just a guess which needs some testing.

@rafalh
Copy link
Owner

rafalh commented Apr 23, 2022

You can calculate the size of all data using following pseudo-code:

total = 0
for f in files {
  if f.is_dir() { // don't forget about root dir
    dir_len = 0
    for entry in f.dir_entries() { 
      dir_len += 32 + ceil(entry.name / 13) * 32
    }
    total += (dir_len + CLUSTER_SIZE - 1) & (CLUSTER_SIZE - 1)
  } else {
    total += (file.len() + CLUSTER_SIZE - 1) & (CLUSTER_SIZE - 1) // file data clusters
  }
}

But of course you don't know CLUSTER_SIZE beforehand so I don't know if it's useful for you... Also it is only calculates length of data clusters, but there are also FATs which size cannot be easily calculated...

@dvc94ch
Copy link
Author

dvc94ch commented Apr 23, 2022

Would it be possible to start by formatting with a size of 0xffff_ffff and then shrink the file system to the actual size?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants