From a44e91322f032796cadd50826a2a01c5eee31fec Mon Sep 17 00:00:00 2001 From: cage Date: Fri, 14 Jan 2022 17:09:23 +0100 Subject: [PATCH] - [kami] prevented reading entire file in memory when downloading. --- src/filesystem-tree-window.lisp | 10 ++++------ src/kami/client.lisp | 27 ++++++++++++++++----------- src/package.lisp | 1 - 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/filesystem-tree-window.lisp b/src/filesystem-tree-window.lisp index 22a4833..c91033b 100644 --- a/src/filesystem-tree-window.lisp +++ b/src/filesystem-tree-window.lisp @@ -205,8 +205,6 @@ (assert path) (fs:recursive-delete path))) -(define-constant +download-buffer+ (expt 2 24) :test #'=) - (define-constant +octect-type+ '(unsigned-byte 8) :test #'equalp) (defun make-temporary-file-from-path (path) @@ -217,6 +215,8 @@ (let ((path (tree-path (data node)))) (make-temporary-file-from-path path))) +(define-constant +download-buffer+ (expt 2 24) :test #'=) + (defun download-local-filesystem-node (matching-node &optional (destination-file @@ -413,10 +413,8 @@ (defun upload-treenode (window source-file remote-path) (when-let* ((root-node (filesystem-root window)) (parent-node (find-node root-node (fs:parent-dir-path remote-path))) - (parent-path (tree-path (data parent-node))) - (destination-path (fs:append-file-to-path parent-path - (fs:path-last-element source-file)))) - (funcall (filesystem-upload-function window) source-file destination-path) + (parent-path (tree-path (data parent-node)))) + (funcall (filesystem-upload-function window) source-file remote-path) (remove-all-children parent-node) (expand-treenode window parent-path) (win-clear window :redraw nil) diff --git a/src/kami/client.lisp b/src/kami/client.lisp index 028cca4..87a6514 100644 --- a/src/kami/client.lisp +++ b/src/kami/client.lisp @@ -1,6 +1,6 @@ (in-package :kami) -(a:define-constant +download-buffer+ (expt 2 24) :test #'=) +(a:define-constant +download-buffer+ (expt 2 10) :test #'=) (a:define-constant +octect-type+ '(unsigned-byte 8) :test #'equalp) @@ -98,14 +98,18 @@ (let* ((*stream* stream) (*root-fid* root-fid) (path (tree-path (data node)))) - (with-cloned-root-fid (*stream* root-fid) - (let ((data (9p:slurp-file *stream* root-fid path))) - (with-open-file (output-stream destination-file - :direction :output - :if-exists :supersede - :if-does-not-exist :create - :element-type +octect-type+) - (write-sequence data output-stream))))) + (with-open-file (output-stream destination-file + :direction :output + :if-exists :supersede + :if-does-not-exist :create + :element-type +octect-type+) + (with-cloned-root-fid (*stream* root-fid) + (9p:read-entire-file-apply-function stream + root-fid + path + (lambda (data offset count) + (declare (ignore offset count)) + (write-sequence data output-stream)))))) destination-file)) (defun upload-node (stream root-fid) @@ -116,8 +120,9 @@ :direction :input :element-type +octect-type+) (with-cloned-root-fid (*stream* root-fid) - (let* ((buffer (misc:make-array-frame +download-buffer+ 0 +octect-type+ t)) - (fid (9p:create-path *stream* root-fid destination-path))) + (let* ((buffer (misc:make-array-frame +download-buffer+ 0 +octect-type+ t)) + (fid (9p:create-path *stream* root-fid destination-path))) + (loop named write-loop for read-so-far = (read-sequence buffer input-stream) then (read-sequence buffer input-stream) diff --git a/src/package.lisp b/src/package.lisp index 6a1e6ad..df7b37c 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -2029,7 +2029,6 @@ (:shadowing-import-from :misc :random-elt :shuffle) (:export :+octect-type+ - :+download-buffer+ :make-temporary-file-from-node :make-node-data :filesystem-tree-window