|
Prev: Boost Python - C++ class' private static data blown away beforeaccessing in Python?
Next: Python / WxPython error message
From: xahlee on 6 Jul 2008 04:05 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/ â
From: Sashi on 10 Jul 2008 14:35 On Jul 6, 4:05 am, "xah...(a)gmail.com" <xah...(a)gmail.com> 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?
From: xahlee on 11 Jul 2008 02:32
Xah Lee wrote: « ... emacs program and tutorial that does archiving a website for offline reading. (See http://xahlee.org/emacs/make_download_copy.html ) » Sashi wrote: «Why not use wget or curl?» The Emacs lisp program makes a archive of parts of website you own, so that readers of your website can click download to read it offline. For wget and curl, i have some tips here: http://xahlee.org/UnixResource_dir/unix_tips.html Xah â http://xahlee.org/ â |