refactored solution to share file move logic

This commit is contained in:
Peter Tillemans 2024-12-09 13:38:14 +01:00
parent 2920259e4d
commit 83d67379cd
2 changed files with 29 additions and 30 deletions

View file

@ -69,29 +69,39 @@
if (inode-p n) if (inode-p n)
maximize (inode-id n))) maximize (inode-id n)))
(defun fs-move-file (fs inode inode-pos freespace free-pos)
"moves a file into a freespace. If the file is too large it will move as much as fits."
(strip-freespace-at-end
(append
(subseq fs 0 free-pos)
;; move file into freespace
(list (make-inode
:id (inode-id inode)
:start (freespace-start freespace)
:length (min (freespace-length freespace) (inode-length inode))))
;; update freespace if any remaining
(if (> (freespace-length freespace) (inode-length inode))
(list (make-freespace
:length (- (freespace-length freespace) (inode-length inode))
:start (+ (freespace-start freespace) (inode-length inode)))))
;; skip to where file was originally
(subseq fs (1+ free-pos) inode-pos)
;; if any of the file remaining update the length
(if (< (freespace-length freespace) (inode-length inode))
(list (make-inode
:id (inode-id inode)
:start (inode-start inode)
:length (- (inode-length inode) (freespace-length freespace)))))
;; copy rest of fs
(subseq fs (1+ inode-pos)))))
(defun fs-compact-step (fs) (defun fs-compact-step (fs)
(let* ((first-free (find-if #'freespace-p fs)) (let* ((first-free (find-if #'freespace-p fs))
(free-pos (position first-free fs)) (free-pos (position first-free fs))
(last-inode (find-if #'inode-p (reverse fs))) (last-inode (find-if #'inode-p (reverse fs)))
(inode-pos (position last-inode fs))) (inode-pos (position last-inode fs)))
(if first-free (if first-free
(strip-freespace-at-end (fs-move-file fs last-inode inode-pos first-free free-pos)
(append
(subseq fs 0 free-pos)
(list (make-inode
:id (inode-id last-inode)
:start (freespace-start first-free)
:length (min (freespace-length first-free) (inode-length last-inode))))
(if (> (freespace-length first-free) (inode-length last-inode))
(list (make-freespace
:length (- (freespace-length first-free) (inode-length last-inode))
:start (+ (freespace-start first-free) (inode-length last-inode)))))
(subseq fs (1+ free-pos) inode-pos)
(if (< (freespace-length first-free) (inode-length last-inode))
(list (make-inode
:id (inode-id last-inode)
:start (inode-start last-inode)
:length (- (inode-length last-inode) (freespace-length first-free)))))))
fs))) fs)))
(defun fs-compact (fs) (defun fs-compact (fs)
@ -126,18 +136,7 @@
(freespace-pos (position freespace fs)) (freespace-pos (position freespace fs))
) )
(if (and freespace (> inode-pos freespace-pos)) (if (and freespace (> inode-pos freespace-pos))
(append (fs-move-file fs inode inode-pos freespace freespace-pos)
(subseq fs 0 freespace-pos)
(list (make-inode
:id id
:start (freespace-start freespace)
:length (inode-length inode)))
(if (> (freespace-length freespace) (inode-length inode))
(list (make-freespace
:start (+ (freespace-start freespace) (inode-length inode))
:length (- (freespace-length freespace) (inode-length inode)))))
(subseq fs (1+ freespace-pos) inode-pos)
(subseq fs (1+ inode-pos)))
fs))) fs)))
(defun fs-optimize (fs) (defun fs-optimize (fs)

View file

@ -9,7 +9,7 @@
(define-test test-blocks-to-fs (define-test test-blocks-to-fs
:parent suite-2024-09 :parent suite-2024-09
(let ((fs (blocks-to-fs sample-data))) (let ((fs sample-data))
(is equalp (make-inode :id 0 :start 0 :length 2) (nth 0 fs)) (is equalp (make-inode :id 0 :start 0 :length 2) (nth 0 fs))
(is equalp (make-freespace :start 2 :length 3) (nth 1 fs)) (is equalp (make-freespace :start 2 :length 3) (nth 1 fs))
(is equalp (make-inode :id 1 :start 5 :length 3) (nth 2 fs)) (is equalp (make-inode :id 1 :start 5 :length 3) (nth 2 fs))