oikonang
oikonang

Reputation: 51

Asynchronous copy-directory with the option to ignore subdirectory in Emacs-lisp?

I am using the code from the answer on this problem for asynchronous copy-directory for a few months now, but sometimes I need one or more subdirectories to be ignored. Is there an easy way by slightly modifying the code to do that?

I have tried to use Selective Directory Copying: SDC package from here, but it brakes when file or folder already exists.

This is the code I am using right now:

(async-start
    `(lambda()
        (copy-directory ,"~/Documents/data/" ,"~/Dropbox/data_backup/" t t t)
        ,"~/Documents/data/")
    (lambda(return-path)
        (message "Upload '%s' finished" return-path)))

There is a subdirectory in ~/Documents/data that sometimes I want it to be ignored because it is larger than a threshold.

Upvotes: 0

Views: 113

Answers (1)

jpkotta
jpkotta

Reputation: 9437

copy-directory calls itself recursively. You can use cl-flet to redefine it locally, while keeping the original definition. You can also do this with advice (and actually this cl-flet technique seems to break advice), but then it's effectively globally redefining the function and you need to control it with e.g. variables.

(defun jpk/copy-directory (directory newname &optional keep-time parents copy-contents)
  (cl-letf (((symbol-function 'orig/copy-directory) (symbol-function 'copy-directory))
            ((symbol-function 'copy-directory)
             (lambda (directory newname &optional keep-time parents copy-contents)
               (if (string= directory "/path/to/foo")
                   (message "skipping: %s" directory)
                 (orig/copy-directory directory newname keep-time parents copy-contents)))))
    (copy-directory directory newname keep-time parents copy-contents)))

In more detail: store the original function to orig/copy-directory, replace the function copy-directory with a lambda that calls orig/copy-directory only if the directory name doesn't match some string, then call the new definition of copy-directory. The recursive call to copy-directory also uses the new definition. All of this is wrapped up in jpk/copy-directory. To make it more flexible, you could add a predicate argument to jpk/copy-directory so the test isn't hard coded.

Upvotes: 1

Related Questions