Dir.exist?没有按预期工作

时间:2017-01-12 14:09:50

标签: ruby-on-rails ruby svn filesystems

检查目录是否存在时出现问题。

我正在编写一个小型Ruby应用程序,它从存储库执行svn checkout,然后查看工作副本以查看某个目录是否存在。

我有一个执行svn操作的SVNClient类,使用open3.popen3调用命令行客户端(我没有找到任何ruby gem来使用svn):

class SVNClient
    require 'open3'

    def initialize(repoUrl, wcPath, username, password)
        @repoUrl = repoUrl
        @wcPath = wcPath
        @username = username
        @password = password
    end

    def checkout
        cmd = "svn co %s %s  --non-interactive --trust-server-cert --username %s --password %s" % [@repoUrl, @wcPath, @username, @password]
        stdin, stdout, stderr = Open3.popen3(cmd)
    end

    def createDirIfNotExists(dirname)
        @subdirPath = @wcPath + "/" + dirname

        a = File.directory? @subdirPath
        b = File.exist? @subdirPath
        c = Dir.exist? @subdirPath
        Rails.logger.debug("#{a}")
        Rails.logger.debug("#{b}")
        Rails.logger.debug("#{c}")


        if !Dir.exist? @subdirPath
            Rails.logger.debug("#{@subdirPath} does not exist")
            Dir.mkdir @subdirPath
        end
    end
end

这个类的用法如下:

        wcDir = "/the/workingcopy/dir"
        logger.debug("#{wcDir}")
        urlToCkeckout = "http://somerepo/path

        client = SVNClient.new(urlToCkeckout, wcDir, username, password)
        client.checkout
        client.createDirIfNotExists("subdir")

现在,我正在做一些测试,我确信当我结帐时,"subdir"目录位于工作副本目录中。但是在createDirIfNotExists方法中,Dir.exist?调用返回false(以及File.directory?File.exist?),我得到" ...不存在&# 34;记录消息。
我错过了一些明显的东西吗?我已经检查了目录的权限,看起来很不错。

顺便说一句,代码在Rails应用程序中运行。

1 个答案:

答案 0 :(得分:1)

解决方案是在我的 Dmat <- Dmat / 10^6 方法中使用open3.popen3的块版本,以确保命令在检查目录存在之前结束:

checkout

现在,在结帐结束后调用 Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| pid = wait_thr.pid # pid of the started process. Rails.logger.debug("#{pid}") end ,因此正确找到了目录。

相关问题