lisp - Common Lisp 确定目录是子目录还是另一个目录

标签 lisp common-lisp

我正在使用 hunchentoot 构建一个简单的 Web 应用程序以向 Web 公开目录树。我遇到的问题是找到一种可靠且安全的方法来确定请求的目录是否实际上是 *share-root* 的子目录,即 /srv/share .

我已经花时间使用 cl-fad,但它并不是我所需要的(或者我没有以解决我的问题的方式使用它)。

我的目标是能够接收像这样的路径:/srv/share/media/../../../ 并意识到请求应该被忽略,因为它要求共享之外的东西。

最佳答案

我建议 enough-namestring结合 truename : 如果

(enough-namestring foo bar)

是相对路径名,则foobar下。 换句话说:

(defun pathname-under-p (under top)
  (case (car (pathname-directory (enough-namestring (truename under)
                                                    (truename top))))
    ((nil :relative) t)
    (t nil)))

或者只是

(defun pathname-under-p (under top)
  (not (eq :absolute (car (pathname-directory (enough-namestring (truename under)
                                                                 (truename top)))))))

如果您的实现不支持目录上的 truename,您将不得不使用特定于实现的函数或 directory .

非常感谢@Svante 的调试。

关于lisp - Common Lisp 确定目录是子目录还是另一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25559746/

相关文章:

emacs - 在 emacs 中设置 SLIME

lisp - "lisp form"的定义?

functional-programming - 计算列表中的正元素

list - 每个级别(表面级别)LISP 的最大数量

loops - 非法语法错误

lisp - CLOS 插槽访问器 : read but not write

clojure - 为什么Clojure之父说Scheme的true/false坏了?

lisp - 从出生日期开始计算普通 LISP 的年龄

types - emacs 中的 most-positive-fixnum

networking - 为什么我在 Racket 上的网络服务器不会多次接收数据?