Re: Elisp Lesson on file processing (make downloadable copy of awebsite)

Discussion in 'Python' started by xahlee@gmail.com, Jul 6, 2008.

  1. Guest

    In this week i wrote a emacs program and tutorial that does archiving
    a website for offline reading.
    (See http://xahlee.org/emacs/make_download_copy.html )

    In the process, i ran into a problem with the unix “cp†utility. I've
    been a unix admin for Solaris during 1998-2004. Even the first time i
    learned about cp, i noticed some pecularity. But only today, i thought
    about it and wrote it out exactly what's going on.

    Here's a excerpt from my emacs tutorial above regarding the issue.
    ------------------

    Copying a bunch of directories seems a trivial operation, but it
    actually took me a couple hours to arrive at the final code, due to
    the exact requirement of directory paths, and the unix “cp†tool's
    lack of precision and flexibility.

    Originally, i thought the code would be something simple like several
    “(shell-command (concat "cp -R " fromDir " " toDir))â€, one for each
    source dir, where fromDir and toDir are full paths. However, it turns
    out the problem is slightly more complex.

    Suppose the source dir is “/Users/xah/web/emacs†and dest dir is “/
    Users/xah/web/diklo/xahtut/emacs†and i want all branches under the
    source dir copied into the dest dir.

    If you do “cp -R /Users/xah/web/emacs /Users/xah/web/diklo/xahtut/
    emacs†with both xahtut and xahtut/emacs doesn't exist, then this
    error results: “cp: /Users/xah/web/diklo/xahtut/emacs: No such file or
    directoryâ€.

    If you do “cp -R /Users/xah/web/emacs /Users/xah/web/diklo/xahtut/
    emacs†with xahtut/emacs exists, then you got “/Users/xah/web/diklo/
    xahtut/emacs/emacsâ€, which is not what i want.

    If you do “cp -R /Users/xah/web/emacs /Users/xah/web/diklo/xahtutâ€
    with xahtut doesn't exist, it results in all branches of emacs in
    xahtut, which is wrong.

    Only when you do “cp -R /Users/xah/web/emacs /Users/xah/web/diklo/
    xahtut†with xahtut exists, you get the correct result with the new
    dir “/Users/xah/web/diklo/xahtut/emacs†having all branches of the
    original dir.

    So, the solution is to first create all the parent dirs of the dest
    dir, but without the dest dir node itself. Then, do a cp to the first
    parent of dest dir.

    Directories are abstractly a tree. Copying directories is like
    grafting a tree from one branch into another branch. To begin, we are
    given two spec: the source node and a destination node. The source
    node by definition is a existing node (i.e. existing dir or file),
    otherwise the copying won't make sense. However, the destination spec
    can be a node that doesn't exist, or its parents doesn't exist, or
    several levels of parents doesn't exist. When the destination spec has
    missing nodes, we can consider creating them as part of the grafting
    process, or we can consider it as a error.

    The unix “cp†tool's behavior is mathematically inconsistent. When the
    destination node exist, the source node will become new children of
    destination node. When the destination node does not exist (but its
    parent exists), “cp†will create the node, and copy only the children
    of the source node to it. When the destination node doesn't exist and
    its parent doesn't exist neither, then “cp†considers it a error.

    ---------------
    Related readings:

    • The Nature of the “Unix Philosophyâ€
    http://xahlee.org/UnixResource_dir/writ/unix_phil.html

    Xah
    ∑ http://xahlee.org/

    ☄
     
    , Jul 6, 2008
    #1
    1. Advertising

  2. Sashi Guest

    On Jul 6, 4:05 am, "" <> wrote:
    > In this week i wrote a emacs program and tutorial that does archiving
    > a website for offline reading.
    > (Seehttp://xahlee.org/emacs/make_download_copy.html)


    Why not use wget or curl?
     
    Sashi, Jul 10, 2008
    #2
    1. Advertising

  3. Guest

    , Jul 11, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    2
    Views:
    519
    j.oke
    Oct 18, 2007
  2. Xah Lee
    Replies:
    6
    Views:
    521
    Steve Holden
    Oct 21, 2007
  3. chandan
    Replies:
    2
    Views:
    350
    chandan
    Jan 15, 2008
  4. chandan
    Replies:
    2
    Views:
    118
  5. Martijn Lievaart
    Replies:
    17
    Views:
    749
    Jim Gibson
    Jun 20, 2012
Loading...

Share This Page