2014-01-17 38 views
1

我想知道如果我正在以正确的方式进行操作。打开文件后检查文件是否被修改关闭

def checkout 
    clone = system("svn export #{file} tmp/") 
    open_file = system("start tmp/#{@file}") 
end 

现在,我可以使用默认编辑器打开我想要的文件,但是如何在关闭之前记录文件是否被修改。

我应该创建一个Process并做Process.wait什么?

感谢您的帮助

回答

1

如果你的意思是你是在Windows中使用start,使用/wait/w选项,使其等到编辑终止。

使用IO::read检查文件内容修改。 (在编辑器执行之前,之后)。

before = IO.read('tmp/#{@file}', {mode: 'rb'}) 
system("start /wait tmp/#{@file}") 
after = IO.read('tmp/#{@file}', {mode: 'rb'}) 

# Check the file content modification. 
if before != after: 
    # File changed! 

如果你正在编辑一个巨大的文件,IO::read会相应地消耗memroy。如Arup Rakshit建议使用File::mtime,如果存储库中存在如此庞大的文件。 (缺点:假阳性警报保存未经修改)

+2

如果它的一个巨大的文件,你会消耗太多的内存这个微不足道的任务,检查修改时间似乎是这样做的最适当的方法 – bjhaid

+0

+1 /等,如果我知道我不会问这个问题谢谢 – Supersonic

+0

@bjhaid,我提到了内存消耗。感谢您的反馈。 – falsetru

2

用于同一File::mtime方法。

将指定文件的修改时间作为Time对象返回。

file_time_before_opening = File.mtime('your file/path') 
# do file operation as you like 
file_time_after_closing = File.mtime('your file/path') 
# now compare file_time_before_opening and file_time_after_closing to know 
# if it is modified or not. 
+0

听起来很有趣去检查出 – Supersonic

+0

用户,打开文件和before_opening时间已设置,用户尚未完成编辑该文件,我已经获得相同的时间before_closing – Supersonic