Skip to content

Commit

Permalink
Add a deoptimize option to add a file in non sequential chains
Browse files Browse the repository at this point in the history
  • Loading branch information
kakaroto committed Nov 14, 2019
1 parent 01e44d7 commit b160d8e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 11 additions & 2 deletions MFS.py
Expand Up @@ -344,13 +344,16 @@ def removeFile(self, id):
self.data_ids[chain - self.num_files] = 0
chain = next_chain

def addFile(self, id, data):
def addFile(self, id, data, optimize=True):
self.removeFile(id)
file = MFSFile(id)
size = len(data)
data_chain = []
for offset in xrange(0, size, MFS.CHUNK_SIZE):
chain = self.getNextFreeDataChunk()
if optimize:
chain = self.getNextFreeDataChunk()
else:
chain = self.getLastFreeDataChunk()
if chain == -1:
# If not enough space, free previously set chains
for chain in data_chain:
Expand All @@ -374,6 +377,12 @@ def getNextFreeDataChunk(self):
return i
return -1

def getLastFreeDataChunk(self):
for i, chain in reversed(list(enumerate(self.data_ids))):
if chain == 0:
return i
return -1

def generate(self):
data = self.SYSTEM_VOLUME_HEADER_FMT.pack(self.signature, self.version,
self.capacity, self.num_files) + \
Expand Down
4 changes: 3 additions & 1 deletion MFSUtil.py
Expand Up @@ -44,6 +44,8 @@ def main():
"0 : packed.\n"
"1 : align all files on chunk start.\n"
"2 : align end of files on end of chunk.")
parser.add_argument("--deoptimize", dest="optimize", action="store_false",
help="De-optimize chain sequences when adding a file to MFS.")

group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-m", "--mfs", dest="mfs", type=argparse.FileType('rb'),
Expand Down Expand Up @@ -93,7 +95,7 @@ def main():
print "File ID %d already exists in the MFS System Volume" % args.file_id
sys.exit(-1)
data = args.add.read()
mfs.getSystemVolume().addFile(args.file_id, data)
mfs.getSystemVolume().addFile(args.file_id, data, args.optimize)
mfs.generate()
with argparse.FileType("wb")(args.output) as f: f.write(mfs.data)
elif args.zip:
Expand Down

0 comments on commit b160d8e

Please sign in to comment.