Last active
February 5, 2024 06:48
-
-
Save EteimZ/715e7d4fef9b279775164296763f4fbb to your computer and use it in GitHub Desktop.
Code snippets from my youtube series: Implementing a file system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A file system is the interface between a user and their storage device. | |
features: | |
Non-contiguous | |
Single-level directory | |
Storage device | |
Block | |
File system | |
Files | |
""" | |
class Storage: | |
""" | |
storage: | |
btyearray | |
binary file | |
methods: | |
create | |
read | |
write | |
""" | |
def __init__(self, path, capacity): | |
self.path = path | |
self.capacity = capacity | |
def create(self): | |
with open(self.path, "wb") as f: | |
data = bytearray([0] * self.capacity) | |
f.write(data) | |
print(f"Binary file '{self.path}' with {self.capacity} bytes has been created.") | |
def write(self, position, content): | |
with open(self.path, "r+b") as f: | |
f.seek(position) | |
f.write(content) | |
def read(self, position, size): | |
with open(self.path, "rb") as f: | |
f.seek(position) | |
data = f.read(size) | |
return data | |
storage = Storage("store.bin", 10) | |
storage.create() | |
storage.write(5, b"Hello world") | |
data = storage.read(5, 5) | |
print(data) | |
class Block: | |
pass | |
class FileSystem: | |
pass | |
class File: | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A file system is the interface between a user and their storage device. | |
features: | |
Non-contiguous | |
Single-level directory | |
Storage device | |
Block | |
File system | |
Files | |
""" | |
from dataclasses import dataclass | |
from typing import Optional | |
class Storage: | |
""" | |
storage: | |
btyearray | |
binary file | |
methods: | |
create | |
read | |
write | |
""" | |
def __init__(self, path, capacity): | |
self.path = path | |
self.capacity = capacity | |
def create(self): | |
with open(self.path, "wb") as f: | |
data = bytearray([0] * self.capacity) | |
f.write(data) | |
print(f"Binary file '{self.path}' with {self.capacity} bytes has been created.") | |
def write(self, position, content): | |
with open(self.path, "r+b") as f: | |
f.seek(position) | |
f.write(content) | |
def read(self, position, size): | |
with open(self.path, "rb") as f: | |
f.seek(position) | |
data = f.read(size) | |
return data | |
storage = Storage("store.bin", 10) | |
storage.create() | |
storage.write(5, b"Hello world") | |
data = storage.read(5, 5) | |
print(data) | |
@dataclass | |
class Block: | |
id: int | |
start: int | |
empty: bool | |
next: Optional["Block"] = None | |
class FileSystem: | |
pass | |
class File: | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A file system is the interface between a user and their storage device. | |
features: | |
Non-contiguous | |
Single-level directory | |
Storage device | |
Block | |
File system | |
Files | |
""" | |
from dataclasses import dataclass | |
from typing import Optional | |
from datetime import datetime | |
class Storage: | |
""" | |
storage: | |
btyearray | |
binary file | |
methods: | |
create | |
read | |
write | |
""" | |
def __init__(self, path, capacity): | |
self.path = path | |
self.capacity = capacity | |
def create(self): | |
with open(self.path, "wb") as f: | |
data = bytearray([0] * self.capacity) | |
f.write(data) | |
print(f"Binary file '{self.path}' with {self.capacity} bytes has been created.") | |
def write(self, position, content): | |
with open(self.path, "r+b") as f: | |
f.seek(position) | |
f.write(content) | |
def read(self, position, size): | |
with open(self.path, "rb") as f: | |
f.seek(position) | |
data = f.read(size) | |
return data | |
@dataclass | |
class Block: | |
id: int | |
start: int | |
empty: bool | |
next: Optional["Block"] = None | |
@dataclass | |
class MetaData: | |
file_size: float | |
created: datetime | |
last_modified: datetime | |
last_accessed: datetime | |
@dataclass | |
class File: | |
name: str | |
start_block: Block | |
meta_data: MetaData | |
def get_metadata(self): | |
print(f"File size: {self.meta_data.file_size} bytes") | |
print(f"Last accessed: {self.meta_data.last_accessed}") | |
print(f"Last modified: {self.meta_data.last_modified}") | |
print(f"Created: {self.meta_data.created}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A file system is the interface between a user and their storage device. | |
features: | |
Non-contiguous | |
Single-level directory | |
Storage device | |
Block | |
File system | |
Files | |
""" | |
from dataclasses import dataclass, field | |
from typing import Optional, List | |
from datetime import datetime | |
class Storage: | |
""" | |
storage: | |
btyearray | |
binary file | |
methods: | |
create | |
read | |
write | |
""" | |
def __init__(self, path, capacity): | |
self.path = path | |
self.capacity = capacity | |
def create(self): | |
with open(self.path, "wb") as f: | |
data = bytearray([0] * self.capacity) | |
f.write(data) | |
print(f"Binary file '{self.path}' with {self.capacity} bytes has been created.") | |
def write(self, position, content): | |
with open(self.path, "r+b") as f: | |
f.seek(position) | |
f.write(content) | |
def read(self, position, size): | |
with open(self.path, "rb") as f: | |
f.seek(position) | |
data = f.read(size) | |
return data | |
@dataclass | |
class Block: | |
id: int | |
start: int | |
empty: bool | |
next: Optional["Block"] = None | |
@dataclass | |
class MetaData: | |
file_size: float | |
created: datetime | |
last_modified: datetime | |
last_accessed: datetime | |
@dataclass | |
class File: | |
name: str | |
start_block: Block | |
meta_data: MetaData | |
def get_metadata(self): | |
print(f"File size: {self.meta_data.file_size} bytes") | |
print(f"Last accessed: {self.meta_data.last_accessed}") | |
print(f"Last modified: {self.meta_data.last_modified}") | |
print(f"Created: {self.meta_data.created}") | |
@dataclass | |
class FileSystem: | |
storage: Storage | |
block_size: int = 5 | |
blocks: List[Block] = field(default_factory=list) | |
def create(self, content): | |
""" | |
Create new file. | |
""" | |
pass | |
def open(self, file_name) -> File: | |
""" | |
Get access to an existing file. | |
""" | |
pass | |
def read(self, file: File): | |
""" | |
Get the content of a file. | |
""" | |
pass | |
def write(self, file: File, content): | |
""" | |
Write content to file. | |
""" | |
pass | |
def append(self, file: File, new_content): | |
""" | |
Add content to file. | |
""" | |
pass | |
def ls(self): | |
""" | |
List files. | |
""" | |
pass | |
def delete(self, filename): | |
""" | |
Delete file | |
""" | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A file system is the interface between a user and their storage device. | |
features: | |
Non-contiguous | |
Single-level directory | |
Storage device | |
Block | |
File system | |
Files | |
""" | |
from dataclasses import dataclass, field | |
from typing import Optional, List, Dict | |
import random | |
from datetime import datetime | |
class Storage: | |
""" | |
storage: | |
btyearray | |
binary file | |
methods: | |
create | |
read | |
write | |
""" | |
def __init__(self, path, capacity): | |
self.path = path | |
self.capacity = capacity | |
def create(self): | |
with open(self.path, "wb") as f: | |
data = bytearray([0] * self.capacity) | |
f.write(data) | |
print(f"Binary file '{self.path}' with {self.capacity} bytes has been created.") | |
def write(self, position, content): | |
with open(self.path, "r+b") as f: | |
f.seek(position) | |
f.write(content) | |
def read(self, position, size): | |
with open(self.path, "rb") as f: | |
f.seek(position) | |
data = f.read(size) | |
return data | |
@dataclass | |
class Block: | |
id: int | |
start: int | |
empty: bool | |
next: Optional["Block"] = None | |
@dataclass | |
class MetaData: | |
file_size: float | |
created: datetime | |
last_modified: datetime | |
last_accessed: datetime | |
@dataclass | |
class File: | |
name: str | |
start_block: Block | |
meta_data: MetaData | |
def get_metadata(self): | |
print(f"File size: {self.meta_data.file_size} bytes") | |
print(f"Last accessed: {self.meta_data.last_accessed}") | |
print(f"Last modified: {self.meta_data.last_modified}") | |
print(f"Created: {self.meta_data.created}") | |
@dataclass | |
class FileSystem: | |
storage: Storage | |
block_size: int = 5 | |
blocks: List[Block] = field(default_factory=list) | |
root_dir: Dict[str, File] = field(default_factory=dict) | |
def __post_init__(self): | |
num_block = int(self.total_space / self.block_size) | |
for i in range(num_block): | |
self.blocks.append(Block(id=i, start=i * self.block_size, empty=True)) | |
@property | |
def total_space(self): | |
return self.storage.capacity | |
@property | |
def free_space(self): | |
free_blocks = [block for block in self.blocks if block.empty] | |
return len(free_blocks) * self.block_size | |
def info(self): | |
print(f"Total space: {self.total_space} bytes") | |
print(f"Free space: {self.free_space} bytes") | |
def open(self, file_name) -> File: | |
""" | |
Get access to an existing file. | |
""" | |
pass | |
def read(self, file: File): | |
""" | |
Get the content of a file. | |
""" | |
pass | |
def write(self, file: File, content): | |
""" | |
Write content to file. | |
""" | |
pass | |
def append(self, file: File, new_content): | |
""" | |
Add content to file. | |
""" | |
pass | |
def ls(self): | |
""" | |
List files. | |
""" | |
pass | |
def delete(self, filename): | |
""" | |
Delete file | |
""" | |
pass | |
storage = Storage("store.bin", 50) | |
storage.create() | |
fs = FileSystem(storage) | |
fs.info() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment