Explorar o código

refactor(fs): re-impl refresh and re-index on nfs backend

Andelf %!s(int64=2) %!d(string=hai) anos
pai
achega
c3daeae6eb

+ 1 - 3
src/main/frontend/components/repo.cljs

@@ -165,9 +165,7 @@
                        (when (and nfs-repo?
                                   (not= current-repo config/local-repo)
                                   (or (nfs-handler/supported?)
-                                      (mobile-util/native-platform?))
-                                  ;; Disable refresh temporally for nfs
-                                  (not util/nfs?))
+                                      (mobile-util/native-platform?)))
                          {:title (t :sync-from-local-files)
                           :hover-detail (t :sync-from-local-files-detail)
                           :options {:on-click #(state/pub-event! [:graph/ask-for-re-fresh])}}))

+ 6 - 6
src/main/frontend/fs.cljs

@@ -180,17 +180,17 @@
 
 (defn list-files
   "List all files in the directory, recursively.
-   {:path :files []}"
-  [path-or-handle ok-handler]
+   
+   Wrap as {:path string :files []}, using relative path"
+  [dir ok-handler]
   (let [fs-record (get-native-backend)]
     (when ok-handler
       (js/console.warn "ok-handler not nil"))
-    (p/let [result (protocol/list-files fs-record path-or-handle ok-handler)]
+    (p/let [result (protocol/list-files fs-record dir ok-handler)]
       (prn ::list-files (first result) "....")
-      (if (or (util/electron?)
-              (mobile-util/native-platform?))
+      (if (seq result) ;; electron, mobile, nfs
         (let [files result ;; TODO(andelf): rm first item from electron
-              dir path-or-handle
+              dir dir
               _ (prn ::prepare-rel-path dir)
               files (mapv (fn [entry]
                             ;; (prn ::xx entry)

+ 12 - 5
src/main/frontend/fs/nfs.cljs

@@ -108,6 +108,7 @@
                                   100000)))))
 
 (defn- list-and-reload-all-file-handles [root-dir root-handle]
+  (prn ::list-and-reload-all-file-handles root-handle)
   (p/let [files (utils/getFiles root-handle
                                 true
                                 (fn [path entry]
@@ -226,8 +227,9 @@
           ;; file exist
           (p/let [local-file (.getFile file-handle)
                   disk-content (.text local-file)
-                  db-content (db/get-file repo path)]
-            (prn ::file-exist file-handle)
+                  db-content (db/get-file repo path)
+                  contents-matched? (contents-matched? disk-content db-content)]
+            (prn ::file-exist file-handle disk-content db-content)
             (if (and
                  (not (string/blank? db-content))
                  (not (:skip-compare? opts))
@@ -334,9 +336,14 @@
       {:path dir-name
        :files files}))
 
-  (list-files [_this path-or-handle ok-handler]
-    (js/console.error "list-files" "unimpl")
-    (utils/getFiles path-or-handle true ok-handler))
+  (list-files [_this dir _ok-handler]
+    (when (string/includes? dir "/")
+      (js/console.error "BUG: list-files(nfs) only accepts repo-dir"))
+    (p/let [handle-path (str "handle/" dir)
+            handle (get-nfs-file-handle handle-path)
+            files (list-and-reload-all-file-handles dir handle)]
+      (prn ::list-files files)
+      files))
 
   (watch-dir! [_this _dir _options]
     nil)

+ 2 - 1
src/main/frontend/fs/protocol.cljs

@@ -5,6 +5,7 @@
 (defprotocol Fs
   (mkdir! [this dir])
   (mkdir-recur! [this dir])
+  ;; TODO(andelf): clarify the return value. How is this different from `list-files`?
   (readdir [this dir])
   (unlink! [this repo path opts])
   ;; FIXME(andelf): remove this API? since the only usage is plugin API
@@ -18,7 +19,7 @@
   (open-dir [this dir ok-handler]
     "=> {:path string :files [{...}]}")
   (list-files [this dir ok-handler]
-    "=> [{:path string :content string}]")
+    "=> [{:path string :content string}] (absolute path)")
   (watch-dir! [this dir options])
   (unwatch-dir! [this dir])
   ;; Ensure the dir is watched, window agnostic.

+ 15 - 11
src/main/frontend/handler/web/nfs.cljs

@@ -134,6 +134,7 @@
       (p/let [_ (prn :xxx-dir-result-fn dir-result-fn)
               result (if (fn? dir-result-fn)
                        (dir-result-fn {:path-handles path-handles :nfs? nfs?})
+                       ;; TODO: rm callback
                        (fs/open-dir dir
                                     (fn [path handle]
                                       (comment when nfs?
@@ -143,9 +144,6 @@
                   (on-open-dir result))
               root-handle (:path result)
               _ (when (fn? picked-root-fn) (picked-root-fn root-handle))
-              ;dir-name (if nfs?
-              ;           (gobj/get root-handle "name")
-              ;           root-handle)
               dir-name root-handle
               repo (str config/local-db-prefix root-handle)]
         
@@ -274,6 +272,7 @@
 (defn- handle-diffs!
   "Compute directory diffs and handle them."
   [repo nfs? old-files new-files handle-path path-handles re-index? ok-handler]
+  (prn ::handle-diff repo old-files new-files)
   (let [get-last-modified-at (fn [path] (some (fn [file]
                                                 (when (= path (:file/path file))
                                                   (:file/last-modified-at file)))
@@ -331,12 +330,14 @@
                       (db/transact! repo new-files))))))))
 
 (defn- reload-dir!
+  "Handle refresh and re-index"
   [repo {:keys [re-index? ok-handler]
          :or {re-index? false}}]
+  (prn ::reload-dir)
   (when (and repo (config/local-db? repo))
     (let [old-files (db/get-files-full repo)
-          dir-name (config/get-local-dir repo)
-          handle-path (str config/local-handle-prefix dir-name)
+          repo-dir (config/get-local-dir repo)
+          handle-path (str "handle/" repo-dir)
           path-handles (atom {})
           electron? (util/electron?)
           mobile-native? (mobile-util/native-platform?)
@@ -346,16 +347,19 @@
         (state/set-graph-syncing? true))
       (->
        (p/let [handle (when-not electron? (idb/get-item handle-path))]
+         (prn ::handle handle)
          (when (or handle electron? mobile-native?)   ; electron doesn't store the file handle
            (p/let [_ (when handle (nfs/verify-permission repo true))
                    local-files-result
-                   (fs/list-files (if nfs? handle
-                                      (config/get-local-dir repo))
+                   (fs/list-files repo-dir
                                   (fn [path handle]
-                                    (when nfs?
-                                      (swap! path-handles assoc path handle))))
+                                    (comment when false ; nfs?
+                                      (swap! path-handles assoc path handle)))
+                                  )
+                   _ (prn ::reading-local-fils local-files-result)
                    new-local-files (-> (->db-files (:files local-files-result) nfs?)
-                                       (remove-ignore-files dir-name nfs?))
+                                       (remove-ignore-files repo-dir nfs?))
+                   _ (prn ::new-local-files)
                   ;; new-global-files (if (and (config/global-config-enabled?)
                    ;;                          ;; Hack until we better understand failure in frontend.handler.file/alter-file
                     ;;                         (global-config-handler/global-config-dir-exists?))
@@ -368,7 +372,7 @@
                            ;;           (p/resolved []))
                    new-files new-local-files ;; (concat new-local-files new-global-files)
 
-                   _ (when nfs?
+                   _ (comment when nfs?
                        (let [file-paths (set (map :file/path new-files))]
                          (swap! path-handles (fn [handles]
                                                (->> handles