Skip to content

Add --compact and --no-pad options to create CLI. #99

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

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/littlefs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def __init__(self, context: Optional["UserContext"] = None, mount=True, **kwargs
def block_count(self) -> int:
return self.fs.block_count

@property
def used_block_count(self) -> int:
return lfs.fs_size(self.fs)

@property
def context(self) -> "UserContext":
"""User context of the file system"""
Expand All @@ -98,6 +102,10 @@ def fs_mkconsistent(self) -> int:
return lfs.fs_mkconsistent(self.fs)

def fs_grow(self, block_count: int) -> int:
"""WARNING: does not modify underlying ``self.context``.

Must be done externally.
"""
if block_count < self.block_count:
raise ValueError(
f"Supplied block_count='{block_count}' cannot be smaller than current block_count {self.block_count}"
Expand All @@ -109,6 +117,9 @@ def fs_stat(self) -> "LFSFSStat":
"""Get the status of the filesystem"""
return lfs.fs_stat(self.fs)

def fs_gc(self):
return lfs.fs_gc(self.fs)

def open(
self, fname: str, mode="r", buffering: int = -1, encoding: str = None, errors: str = None, newline: str = None
) -> IO:
Expand Down
37 changes: 36 additions & 1 deletion src/littlefs/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,33 @@ def create(parser: argparse.ArgumentParser, args: argparse.Namespace) -> int:
with fs.open(rel_path.as_posix(), "wb") as dest:
dest.write(path.read_bytes())

if args.compact:
if args.verbose:
print(f"Compacting... {fs.used_block_count} / {args.block_count}")
compact_fs = LittleFS(
block_size=args.block_size,
block_count=fs.used_block_count,
name_max=args.name_max,
)
for root, dirs, files in fs.walk("/"):
if not root.endswith("/"):
root += "/"
for _dir in dirs:
compact_fs.makedirs(root + _dir, exist_ok=True)
for file in files:
path = root + file
print(path)
with fs.open(path, "rb") as src, compact_fs.open(path, "wb") as dest:
dest.write(src.read())
compact_fs.fs_grow(args.block_count)
data = compact_fs.context.buffer
if not args.no_pad:
data = data.ljust(args.fs_size, b"\xFF")
else:
data = fs.context.buffer

args.destination.parent.mkdir(exist_ok=True, parents=True)
args.destination.write_bytes(fs.context.buffer)
args.destination.write_bytes(data)
return 0


Expand Down Expand Up @@ -219,6 +244,16 @@ def add_command(handler, name="", help=""):
required=True,
help="LittleFS block size.",
)
parser_create.add_argument(
"--compact",
action="store_true",
help="Store all data in the beginning blocks.",
)
parser_create.add_argument(
"--no-pad",
action="store_true",
help="Do not pad the binary to-size with 0xFF. Only valid with --compact.",
)
block_count_group = parser_create.add_mutually_exclusive_group(required=True)
block_count_group.add_argument(
"--block-count",
Expand Down
1 change: 1 addition & 0 deletions src/littlefs/lfs.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,4 @@ cdef extern from "lfs.h":
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data)
int lfs_fs_mkconsistent(lfs_t *lfs)
int lfs_fs_grow(lfs_t *lfs, lfs_size_t block_count);
int lfs_fs_gc(lfs_t *lfs)
1 change: 1 addition & 0 deletions src/littlefs/lfs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class LFSDirectory: ...

def fs_stat(fs: LFSFilesystem) -> LFSFSStat: ...
def fs_size(fs: LFSFilesystem) -> int: ...
def fs_gc(fs: LFSFilesystem) -> int: ...
def format(fs: LFSFilesystem, cfg: LFSConfig) -> int: ...
def mount(fs: LFSFilesystem, cfg: LFSConfig) -> int: ...
def unmount(fs: LFSFilesystem) -> int: ...
Expand Down
2 changes: 2 additions & 0 deletions src/littlefs/lfs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ def fs_stat(LFSFilesystem fs):
def fs_size(LFSFilesystem fs):
return _raise_on_error(lfs_fs_size(&fs._impl))

def fs_gc(LFSFilesystem fs):
return _raise_on_error(lfs_fs_gc(&fs._impl))

def format(LFSFilesystem fs, LFSConfig cfg):
"""Format the filesystem"""
Expand Down
Loading