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

[DON'T MERGE][DEV] feat: introduce file_table with sqlite3, re-implement create_standby.rebuild_mode #453

Draft
wants to merge 184 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
184 commits
Select commit Hold shift + click to select a range
08e6f76
add msgpack to deps
Bodong-Yang Dec 5, 2024
24dd822
ota_metadata: implement file_table
Bodong-Yang Dec 5, 2024
8cc3378
ota_metadata.legacy: add rs_table
Bodong-Yang Dec 5, 2024
a88584b
minor update
Bodong-Yang Dec 5, 2024
5639201
implement csv_parser
Bodong-Yang Dec 5, 2024
9688360
deps: bump pydantic to >=2.10,<3 (#451)
Bodong-Yang Dec 5, 2024
5c55be7
bump to use simple-sqlite3-orm<0.4,>=0.3
Bodong-Yang Dec 6, 2024
fb374c8
minor update
Bodong-Yang Dec 6, 2024
17793ba
WIP: parser: add OTAMeta2
Bodong-Yang Dec 6, 2024
9f33dea
WIP: OTAMeta2: add connect_fstable and connect_rstable API
Bodong-Yang Dec 6, 2024
da1a9f2
cleanup ota_metadata.legacy.parser
Bodong-Yang Dec 6, 2024
d5fa7fd
finish cleanup ota_metadata.legacy
Bodong-Yang Dec 6, 2024
26dbcb5
file_table: record table name in tablespec
Bodong-Yang Dec 6, 2024
b72fc71
ota_metadata orms: embbeded table name
Bodong-Yang Dec 6, 2024
483a5d1
minor update
Bodong-Yang Dec 6, 2024
948e91b
parser: add db fpath properties
Bodong-Yang Dec 6, 2024
4f1e5a5
rs_table: add size field
Bodong-Yang Dec 6, 2024
5fda42d
ota_metadata.parser: split OTAMetadata class to a new module metadata
Bodong-Yang Dec 6, 2024
4c15dfe
move DownloadInfo to ota_metadata.utils.__init__
Bodong-Yang Dec 6, 2024
e51e616
finish up ota_metadata.legacy refactoring
Bodong-Yang Dec 6, 2024
1afc03b
finish up create_standby.delta_gen
Bodong-Yang Dec 6, 2024
a2c4894
fstable: add copy_to_target
Bodong-Yang Dec 6, 2024
eeeb7d7
ota_metadata.file_table: use NamedTuple for InodeTable to reduce size
Bodong-Yang Dec 6, 2024
e512525
file_table.types: fix Xattr and InodeTable validator
Bodong-Yang Dec 6, 2024
3969d36
minor update
Bodong-Yang Dec 6, 2024
7b49c5b
create_standby._delta_gen: remove hardlink group related implementation
Bodong-Yang Dec 6, 2024
3d0794b
delta_gen: refine calculate_delta
Bodong-Yang Dec 6, 2024
d6ae559
file_table: now support handling hardlink files
Bodong-Yang Dec 6, 2024
adb7328
table.copy_to_target: minor refinement
Bodong-Yang Dec 6, 2024
3abb339
table: add support for xattr
Bodong-Yang Dec 6, 2024
aa0cd16
minor update
Bodong-Yang Dec 6, 2024
ac9b9d9
re-implement rebuild_mode
Bodong-Yang Dec 6, 2024
f045647
minor update
Bodong-Yang Dec 6, 2024
15f3a53
create_standby currently only support rebuild mode
Bodong-Yang Dec 6, 2024
03dd165
proto: remove ota_metafiles.proto
Bodong-Yang Dec 6, 2024
6ba5d8f
minor update
Bodong-Yang Dec 6, 2024
b7d34ae
metadata: download_dir now is a tempdir
Bodong-Yang Dec 6, 2024
7fa95aa
metadata: add iter_persist_entries API
Bodong-Yang Dec 6, 2024
462708a
ota_metadata: total_regulars_num
Bodong-Yang Dec 6, 2024
8d1e7c5
ota_core: integrate new ota_metadata
Bodong-Yang Dec 6, 2024
ce844d2
retry_task_map: expose shutdown API
Bodong-Yang Dec 6, 2024
7a39cd2
metadata: get_download_list now support shuffle each batch
Bodong-Yang Dec 6, 2024
cba68b1
delta_gen: only use active_slot as workdir when file size is smaller …
Bodong-Yang Dec 6, 2024
42c8b43
minor update
Bodong-Yang Dec 6, 2024
1c4749f
slot_mnt_helper: remove standby_boot_dir attr
Bodong-Yang Dec 6, 2024
05030ec
boot_control protocol: remove deprecated api get_standby_boot_dir
Bodong-Yang Dec 6, 2024
75d1272
remove the deprecated APIs
Bodong-Yang Dec 6, 2024
328b19b
re-design file_table, now split file_table to ft_regular and ft_non_r…
Bodong-Yang Dec 9, 2024
b141717
finish up file_table
Bodong-Yang Dec 9, 2024
853dc06
rs_table: rename size to original_size
Bodong-Yang Dec 9, 2024
8975dcf
csv_parser: integrate new table def
Bodong-Yang Dec 9, 2024
3dec3ff
ota_metadata.legacy.metadata: implement the new method: sort the ft_r…
Bodong-Yang Dec 9, 2024
abbfdb5
ota_metadata.legacy: use new seek method to iter through resource table
Bodong-Yang Dec 9, 2024
c81f43d
table_name should be literal
Bodong-Yang Dec 9, 2024
0b4a03d
delta_gen: will do a vacuum on rstable on finish
Bodong-Yang Dec 9, 2024
f1d6d04
minor fix
Bodong-Yang Dec 9, 2024
834b561
metadata: _sort_ft_regular_in_place: use sql script
Bodong-Yang Dec 10, 2024
8e41b56
file_table.regular: add is_hardlink helper method
Bodong-Yang Dec 10, 2024
36e5143
table: implement fpath_on_target and is_hardlink helpers
Bodong-Yang Dec 10, 2024
1319f4b
table: fix forgeting to prepare parents
Bodong-Yang Dec 10, 2024
a30635e
rebuild_mode: re-implement _process_regular_files_group
Bodong-Yang Dec 10, 2024
dee7e8e
implement ota_metadata.utils.sqlite3_helper
Bodong-Yang Dec 10, 2024
1d6cc7c
minor fix
Bodong-Yang Dec 10, 2024
a49812b
file_table.orm: add iter_all method
Bodong-Yang Dec 10, 2024
e3108a7
legacy.rs_table: add iter_all_with_shuffle
Bodong-Yang Dec 10, 2024
2b8ae79
finish up re-implementing rebuild_mode
Bodong-Yang Dec 10, 2024
77b6228
cfg_configurable: lower the default value of MAX_CONCURRENT_PROCESS_F…
Bodong-Yang Dec 10, 2024
7e651e3
minor fix
Bodong-Yang Dec 10, 2024
3bb8ae0
rs_table: fix digest must be not None
Bodong-Yang Dec 10, 2024
30dffc1
fix up ota_metadata.legacy.metadata
Bodong-Yang Dec 10, 2024
3a08a4c
fix up ota_core
Bodong-Yang Dec 10, 2024
86dbfeb
minor update
Bodong-Yang Dec 10, 2024
5f98e61
DownloadInfo: now dst is a Path
Bodong-Yang Dec 10, 2024
a3ae0bc
fix up according to DownloadInfo's change
Bodong-Yang Dec 10, 2024
3508522
ota_core: cleanup workdir and resource_dir on succeeded OTA
Bodong-Yang Dec 10, 2024
8892609
rebuild_mode._iter_regular_file_entres: use iter_all method instead
Bodong-Yang Dec 10, 2024
09705bd
minor update
Bodong-Yang Dec 10, 2024
3f37c82
metadata: fix wrong usage of sqlite3 fetchone
Bodong-Yang Dec 10, 2024
7d30b05
ota_core: fix missing report the download_size and download_num after…
Bodong-Yang Dec 10, 2024
1ebd396
csv_parser: minor update
Bodong-Yang Dec 10, 2024
4a0a9c3
metadata: no need to use tempdir
Bodong-Yang Dec 10, 2024
4422c2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 11, 2024
09befc7
[GHA] Update requirements.txt on pyproject.toml changed
github-actions[bot] Dec 11, 2024
b69f7bd
metadata: fix download_metafiles
Bodong-Yang Dec 11, 2024
874e6b5
fix condition usage
Bodong-Yang Dec 11, 2024
9b9fc0d
fix file_table
Bodong-Yang Dec 11, 2024
94e1b03
minor fix
Bodong-Yang Dec 11, 2024
d37e9e2
metadata: sqlite3 connection: disable same thread check
Bodong-Yang Dec 11, 2024
bd2d204
enlarge db timeout
Bodong-Yang Dec 11, 2024
693e60f
minor fix
Bodong-Yang Dec 11, 2024
d190413
retry_task_map: _dispatcher now use logger.exception
Bodong-Yang Dec 11, 2024
2bfcddc
ota_core: remove session_workdir on finish/faiiled
Bodong-Yang Dec 11, 2024
7d75521
fix delta_gen not scanning any dirs
Bodong-Yang Dec 11, 2024
f68b250
fix iterating through resource table with seek method by recreating t…
Bodong-Yang Dec 11, 2024
99022ea
minor fix
Bodong-Yang Dec 11, 2024
f46c002
minor update
Bodong-Yang Dec 11, 2024
95fc721
fix rebuildmode
Bodong-Yang Dec 11, 2024
da99aad
rebuild_mode: close db on finish
Bodong-Yang Dec 11, 2024
8ec3fe0
minor fix
Bodong-Yang Dec 11, 2024
6a967ee
metadata: connect_table API now enable sqlite3 optimization by default
Bodong-Yang Dec 11, 2024
0a027d2
minor update
Bodong-Yang Dec 11, 2024
b91e966
minor fix
Bodong-Yang Dec 11, 2024
e0aab93
remove unused code
Bodong-Yang Dec 11, 2024
5b8178f
fix delta_gen report incorrect status
Bodong-Yang Dec 11, 2024
f6767da
minor update
Bodong-Yang Dec 12, 2024
515304b
still increase concurrent file process tasks to 512
Bodong-Yang Dec 12, 2024
7056de1
delta_gen: minor refinement
Bodong-Yang Dec 12, 2024
4bce6f9
orm: add FTRegularORMThreadPool
Bodong-Yang Dec 12, 2024
44d45ad
file_table: not define table name in the file_table class
Bodong-Yang Dec 12, 2024
8bc7acf
orm: define table name at the ORM classes
Bodong-Yang Dec 12, 2024
9b8297c
orm: define table name at the ORM classes
Bodong-Yang Dec 12, 2024
2bfa446
metadata: integrate
Bodong-Yang Dec 12, 2024
2b7ab1a
delta_gen: integrate
Bodong-Yang Dec 12, 2024
7da10d2
metadata: only call enable_wal_mode on read_only=False
Bodong-Yang Dec 12, 2024
931f0bc
bump to use simple-sqlite3-orm<0.5,>=0.4
Bodong-Yang Dec 12, 2024
652ecf9
file_table.orm: use upstream APIs
Bodong-Yang Dec 12, 2024
5d10c10
ota_metadata.legacy: use upstream available sort_and_replace helper
Bodong-Yang Dec 12, 2024
ce1648a
rstable: use upstream available API
Bodong-Yang Dec 12, 2024
d784d0e
remove ota_metadata.utils.sqlite3_helper
Bodong-Yang Dec 12, 2024
1ffae66
create_standby._delta_gen: no need to sort the rs table with new upst…
Bodong-Yang Dec 12, 2024
bd28994
rebuild_mode: use upstream select_all_with_pagination API
Bodong-Yang Dec 12, 2024
49ab685
minor update
Bodong-Yang Dec 12, 2024
998d8f1
minor fix
Bodong-Yang Dec 12, 2024
e2db188
[GHA] Update requirements.txt on pyproject.toml changed
github-actions[bot] Dec 12, 2024
1610166
delta_gen: not so neccessary to do a vacuum here.
Bodong-Yang Dec 12, 2024
743f9b0
orm: implement check_entry
Bodong-Yang Dec 12, 2024
34f2130
minor update
Bodong-Yang Dec 12, 2024
0855d87
minor fix
Bodong-Yang Dec 12, 2024
31f77a1
minor update
Bodong-Yang Dec 12, 2024
7fede3b
rs_table: add check_entry for RSTableORMThreadPool
Bodong-Yang Dec 12, 2024
37794ed
delta_gen: use a new method to update the rstable
Bodong-Yang Dec 12, 2024
fa54817
file_table: split tables, now list directory in a separated table
Bodong-Yang Dec 12, 2024
fcde19d
ota_metadata.utils: implement orm base
Bodong-Yang Dec 12, 2024
f4199a6
orm_base: add __init__
Bodong-Yang Dec 12, 2024
965bb18
file_table.orm: add orm for dir, use new base classes
Bodong-Yang Dec 13, 2024
1cee46e
file_table: expose ftdirectory
Bodong-Yang Dec 13, 2024
870dc1e
file_table: minor update and docstring write
Bodong-Yang Dec 13, 2024
cdc9c67
csv_parser: use ftdirorm
Bodong-Yang Dec 13, 2024
7ee5d21
legacy.rs_table: use ormbases fro ota_metadata
Bodong-Yang Dec 13, 2024
7c1a32c
metadata: implements helper methods for iterating meta db
Bodong-Yang Dec 13, 2024
91f9246
metadata: add remove_entries_from_resource_table helper, refine Resou…
Bodong-Yang Dec 13, 2024
cd20237
delta_gen: integrate
Bodong-Yang Dec 13, 2024
d5e8de8
rebuild_mode: integrate new otametadata, use burst_compressed_logger
Bodong-Yang Dec 13, 2024
e96c795
rebuild_mode: use warning level for logging
Bodong-Yang Dec 13, 2024
ba2e0f7
rebuild_mode: refactor related to logging
Bodong-Yang Dec 13, 2024
f4cd8a0
rebuild_mode: minor fix
Bodong-Yang Dec 13, 2024
232dcb0
implement a small helper method
Bodong-Yang Dec 13, 2024
0004ab6
minor update
Bodong-Yang Dec 13, 2024
a27b97b
delta_gen: fix a problem that we prepare the same unique resource again
Bodong-Yang Dec 13, 2024
eef6d9a
_delta_gen: still only do db lookup only when not in full scan mode
Bodong-Yang Dec 13, 2024
1c81efe
metadata: use in-memory db
Bodong-Yang Dec 13, 2024
92937fb
orm_base: now check_entry will return something
Bodong-Yang Dec 13, 2024
ff070c2
metadata: implement conns_factory
Bodong-Yang Dec 13, 2024
a7c78af
delta_gen: integrate
Bodong-Yang Dec 13, 2024
8715805
cleanup unused code
Bodong-Yang Dec 13, 2024
a4ee23b
orm_base.ORMPoolBase: add a helper method
Bodong-Yang Dec 13, 2024
c2a21f1
orm_base: minor fix to check_entry
Bodong-Yang Dec 13, 2024
b7ac912
metadata: use the original connections to write
Bodong-Yang Dec 13, 2024
e2977da
orm_base: check_entry
Bodong-Yang Dec 13, 2024
1d2f2a9
working for now
Bodong-Yang Dec 13, 2024
9ff7fcf
minor update
Bodong-Yang Dec 13, 2024
0659e06
first working version
Bodong-Yang Dec 13, 2024
79a12c9
delta_gen: minor refinement
Bodong-Yang Dec 13, 2024
f267656
minor cleanup
Bodong-Yang Dec 13, 2024
9def53d
no need to enable wal mode for in-memory db
Bodong-Yang Dec 13, 2024
5a1ebcd
metadata: remember all the conns made, and provide APIs to close all …
Bodong-Yang Dec 13, 2024
ecab819
ota_core: release db connection resources
Bodong-Yang Dec 13, 2024
e7bc6b9
fix sqlite3 connection cannot be weakrefed
Bodong-Yang Dec 13, 2024
708c793
minor update
Bodong-Yang Dec 13, 2024
b6c011d
minor update
Bodong-Yang Dec 13, 2024
2789ade
delta_gen: still use the previous method
Bodong-Yang Dec 13, 2024
c6e157b
delta_gen: still use the old but fast method
Bodong-Yang Dec 13, 2024
318b44f
minor update
Bodong-Yang Dec 13, 2024
efe52d5
fix _delta_gen
Bodong-Yang Dec 13, 2024
23e92d4
minor update
Bodong-Yang Dec 13, 2024
3370cc0
rebuild_mode: no need to use threadpool for configuring dirs and syml…
Bodong-Yang Dec 13, 2024
21aa019
minor update and refine
Bodong-Yang Dec 13, 2024
c562b28
remove duplicated logic
Bodong-Yang Dec 13, 2024
499fc2e
remove duplicated logic
Bodong-Yang Dec 13, 2024
a32e6c2
minor update
Bodong-Yang Dec 13, 2024
e395b20
file_table: fix chown reset sticky bit. always do chown before chmod!
Bodong-Yang Dec 13, 2024
7998d63
Revert "rebuild_mode: no need to use threadpool for configuring dirs …
Bodong-Yang Dec 13, 2024
9f0f2ad
delta_gen: minor optimization
Bodong-Yang Dec 13, 2024
056e518
minor fix
Bodong-Yang Dec 13, 2024
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
34 changes: 0 additions & 34 deletions proto/ota_metafiles.proto

This file was deleted.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ dependencies = [
"cryptography>=43.0.1,<45",
"grpcio>=1.53.2,<1.69",
"multidict<7.0,>=4.5",
"msgpack>=1,<1.2",
"protobuf>=4.21.12,<5.29",
"pydantic<3,>=2.10",
"pydantic-settings<3,>=2.3",
"pyyaml<7,>=6.0.1",
"requests<2.33,>=2.32",
"simple-sqlite3-orm<0.3,>=0.2",
"simple-sqlite3-orm<0.5,>=0.4",
"typing-extensions>=4.6.3",
"urllib3<2.3,>=2.2.2",
"uvicorn[standard]>=0.30,<0.33",
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ aiohttp>=3.10.11,<3.12
cryptography>=43.0.1,<45
grpcio>=1.53.2,<1.69
multidict<7.0,>=4.5
msgpack>=1,<1.2
protobuf>=4.21.12,<5.29
pydantic<3,>=2.10
pydantic-settings<3,>=2.3
pyyaml<7,>=6.0.1
requests<2.33,>=2.32
simple-sqlite3-orm<0.3,>=0.2
simple-sqlite3-orm<0.5,>=0.4
typing-extensions>=4.6.3
urllib3<2.3,>=2.2.2
uvicorn[standard]>=0.30,<0.33
Expand Down
34 changes: 34 additions & 0 deletions src/ota_metadata/file_table/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2022 TIER IV, INC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from ._orm import FTDirORM, FTNonRegularORM, FTRegularORM, FTRegularORMPool
from ._table import (
FileTableDirectories,
FileTableNonRegularFiles,
FileTableRegularFiles,
)
from ._types import InodeTable, Xattr

__all__ = [
"FTNonRegularORM",
"FTRegularORM",
"FTDirORM",
"FTRegularORMPool",
"FileTableNonRegularFiles",
"FileTableRegularFiles",
"FileTableDirectories",
"InodeTable",
"Xattr",
]
46 changes: 46 additions & 0 deletions src/ota_metadata/file_table/_orm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2022 TIER IV, INC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import annotations

from typing import ClassVar, Literal

from ota_metadata.utils.orm_base import ORMBase, ORMPoolBase

from ._table import (
FileTableDirectories,
FileTableNonRegularFiles,
FileTableRegularFiles,
)


class FTRegularORM(ORMBase[FileTableRegularFiles]):

table_name: ClassVar[Literal["ft_regular"]] = "ft_regular"


class FTRegularORMPool(ORMPoolBase[FileTableRegularFiles]):

table_name: ClassVar[Literal["ft_regular"]] = "ft_regular"


class FTNonRegularORM(ORMBase[FileTableNonRegularFiles]):

table_name: ClassVar[Literal["ft_non_regular"]] = "ft_non_regular"


class FTDirORM(ORMBase[FileTableDirectories]):

table_name: ClassVar[Literal["ft_dir"]] = "ft_dir"
214 changes: 214 additions & 0 deletions src/ota_metadata/file_table/_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
# Copyright 2022 TIER IV, INC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import annotations

import os
import shutil
import stat
from pathlib import Path
from typing import ClassVar, Literal, Optional

from pydantic import BaseModel, SkipValidation
from simple_sqlite3_orm import ConstrainRepr, TableSpec, TypeAffinityRepr
from typing_extensions import Annotated

from otaclient_common.typing import StrOrPath

from ._types import InodeTableType, XattrType

CANONICAL_ROOT = "/"

PrepareMethod = Literal["move", "hardlink", "copy"]


class FileTableBase(BaseModel):
schema_ver: ClassVar[Literal[1]] = 1

path: Annotated[
str,
TypeAffinityRepr(str),
ConstrainRepr("PRIMARY KEY"),
SkipValidation,
]

inode: Annotated[
InodeTableType,
TypeAffinityRepr(bytes),
ConstrainRepr("NOT NULL"),
]
"""msgpacked basic attrs from inode table for this file entry.

Ref: https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html

including the following fields from inode table:
1. mode bits
2. uid
3. gid
4. inode(when the file is hardlinked)
"""

xattrs: Annotated[
Optional[XattrType],
TypeAffinityRepr(bytes),
] = None
"""msgpacked extended attrs for the entry.

It contains a dict of xattr names and xattr values.
"""

def set_xattr(self, _target: StrOrPath) -> None:
"""Set the xattr of self onto the <_target>.

NOTE: this method always don't follow symlink.
"""
if xattrs := self.xattrs:
for k, v in xattrs.items():
os.setxattr(
path=_target,
attribute=k,
value=v.encode(),
follow_symlinks=False,
)

def set_perm(self, _target: StrOrPath) -> None:
"""Set the mode,uid,gid of self onto the <_target>.

NOTE: this method always don't follow symlink.
"""
inode_table = self.inode
# NOTE(20241213): chown will reset the sticky bit of the file!!!
# Remember to always put chown before chmod !!!
os.chown(
_target, uid=inode_table.uid, gid=inode_table.gid, follow_symlinks=False
)
os.chmod(_target, mode=inode_table.mode, follow_symlinks=False)

def fpath_on_target(self, target_mnt: StrOrPath) -> Path:
"""Return the fpath of self joined to <target_mnt>."""
_canonical_path = Path(self.path)
_target_on_mnt = Path(target_mnt) / _canonical_path.relative_to(CANONICAL_ROOT)
return _target_on_mnt


class FileTableRegularFiles(TableSpec, FileTableBase):
"""DB table for regular file entries."""

digest: Annotated[
bytes,
TypeAffinityRepr(bytes),
SkipValidation,
]

def prepare_target(
self,
_rs: StrOrPath,
*,
target_mnt: StrOrPath,
prepare_method: PrepareMethod,
) -> None:
_target_on_mnt = self.fpath_on_target(target_mnt=target_mnt)

if prepare_method == "copy":
shutil.copy(_rs, _target_on_mnt)
self.set_perm(_target_on_mnt)
self.set_xattr(_target_on_mnt)
return

if prepare_method == "hardlink":
# NOTE: os.link will make dst a hardlink to src.
os.link(_rs, _target_on_mnt)
# NOTE: although we actually don't need to set_perm and set_xattr everytime
# to file paths point to the same inode, for simplicity here we just
# do it everytime.
self.set_perm(_target_on_mnt)
self.set_xattr(_target_on_mnt)
return

if prepare_method == "move":
shutil.move(str(_rs), _target_on_mnt)
self.set_perm(_target_on_mnt)
self.set_xattr(_target_on_mnt)


class FileTableNonRegularFiles(TableSpec, FileTableBase):
"""DB table for non-regular file entries.

This includes:
1. symlink.
2. chardev file.

NOTE that support for chardev file is only for overlayfs' whiteout file,
so only device num as 0,0 will be allowed.
"""

contents: Annotated[
Optional[bytes],
TypeAffinityRepr(bytes),
SkipValidation,
] = None
"""The contents of the file. Currently only used by symlink.

When is symlink, <contents> is the symlink target.
"""

def set_perm(self, _target: StrOrPath) -> None:
"""Set the mode,uid,gid of self onto the <_target>.

NOTE: this method always don't follow symlink.
"""
inode_table = self.inode

# NOTE(20241213): chown will reset the sticky bit of the file!!!
# Remember to always put chown before chmod !!!
os.chown(
_target, uid=inode_table.uid, gid=inode_table.gid, follow_symlinks=False
)
# NOTE: changing mode of symlink is not needed and uneffective, and on some platform
# changing mode of symlink will even result in exception raised.
if not stat.S_ISLNK(inode_table.mode):
os.chmod(_target, mode=inode_table.mode, follow_symlinks=False)

def prepare_target(self, *, target_mnt: StrOrPath) -> None:
_target_on_mnt = self.fpath_on_target(target_mnt=target_mnt)

inode_table = self.inode
_mode = inode_table.mode
if stat.S_ISLNK(_mode):
assert (_symlink_target_raw := self.contents), f"invalid entry {self}"
_symlink_target = _symlink_target_raw.decode()
_target_on_mnt.symlink_to(_symlink_target)
self.set_perm(_target_on_mnt)
self.set_xattr(_target_on_mnt)
return

if stat.S_ISCHR(_mode):
_device_num = os.makedev(0, 0)
os.mknod(_target_on_mnt, mode=_mode, device=_device_num)
self.set_perm(_target_on_mnt)
self.set_xattr(_target_on_mnt)
return

raise ValueError(f"invalid entry {self}")


class FileTableDirectories(TableSpec, FileTableBase):
"""DB table for directory entries."""

def prepare_target(self, *, target_mnt: StrOrPath) -> None:
_target_on_mnt = self.fpath_on_target(target_mnt=target_mnt)
_target_on_mnt.mkdir(exist_ok=True, parents=True)
self.set_perm(_target_on_mnt)
self.set_xattr(_target_on_mnt)
Loading
Loading