Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何等待文件写入?_Ruby_File_Wait - Fatal编程技术网

Ruby 如何等待文件写入?

Ruby 如何等待文件写入?,ruby,file,wait,Ruby,File,Wait,当我执行这个程序时,它运行良好,但验证返回false。如果我重新执行它,验证就会生效 fullpath是备份的目录,refpath是原始文件的路径: if (fullpath.include?(refpath) && refpath.empty? == false && fullpath.empty? == false) diffpath= "#{fullpath} #{refpath}" puts diffpath sortie = IO.popen

当我执行这个程序时,它运行良好,但验证返回false。如果我重新执行它,验证就会生效

fullpath
是备份的目录,
refpath
是原始文件的路径:

if (fullpath.include?(refpath) && refpath.empty? == false && fullpath.empty? == false)
  diffpath= "#{fullpath} #{refpath}"
  puts diffpath
  sortie = IO.popen("diff -Bb #{diffpath}").readlines #(fullpath backup_dir)
  #puts fullpath
  if sortie.empty?

    puts "Les fichiers -#{f} sont identiques."

  else
    puts "Modification : [#{refpath}] \n [#{fullpath}] "
  end
end 
主要方案是:

require "modif.rb"
require "testdate.rb"
require "restore_data.rb"

#Pour la sauvegarde des fichiers
puts "__________SAUVEGARDE__________"

#Pour la restauration des fichiers :
puts "__________RESTAURATION__________"

#Vérification de l'intégrité des fichiers restaurés.
puts "__________VERIFICATION__________"
sleep(5.0)
v = Verif.new
v.do_verif(outdir)
当我打开还原文件的目录时,文件并没有完全写入

在调用验证之前,我调用save、backup和verification


睡眠
不工作。该过程已完全暂停,不会写入丢失的文件。

这还没有经过测试,但更多的是我如何编写第一部分:

if ((fullpath != '') && fullpath[refpath] && (refpath != ''))
  sortie = `diff -Bb #{ fullpath } #{ refpath }`
  if sortie == ''
    puts "Les fichiers -#{ f } sont identiques."
  else
    puts "Modification : [#{ refpath }] \n [#{ fullpath }] "
  end
end 
通常,您可以简化测试。Ruby有
空?
方法来查看内容,这很好,但如果使用
='
!=''则更明显

使用
fullpath[refpath]
将返回一个匹配的字符串或
nil
,因此您在那里有一个“truthy/false”响应,代码噪音更小

使用backticks或
%x
获取“diff”的输出,而不是使用
popen
readlines


通常,您的代码看起来像来自Java。Ruby具有非常优雅的语法和书写风格,因此请充分利用它。

原始文件的大小是多少GB?我怀疑睡眠5.0是否真的没有意义,根本原因是其他原因。还是使用慢速USB闪存作为备份目录

如果您确定需要等待写入过程完成,或许可以在备份文件的
mtime
上进行轮询:

finished = false
30.times { # deadline of 30*10 == 300 seconds
  if 5 < (File.mtime(fullpath) - Time.now).abs
    # the backup process had done its job
    finished = true
    break
  end
  sleep 10
}

if finished
  v = Verif.new
...
finished=false
30.5倍{30*10的截止时间==300秒
如果5<(File.mtime(fullpath)-Time.now).abs
#备份过程已经完成了它的工作
完成=正确
打破
结束
睡眠10
}
如果完成
v=验证新
...

当备份过程处于写入输出文件的中间时,<代码>文件.MTIME(FultPATH)应在>时间2秒内。现在小心FAT文件系统,时间分辨率为2秒。我还使用了

abs
,因为一些备份程序会根据需要修改
mtime
值。

保存/还原用于写入文件的功能是什么?我整理了您的问题(希望)使其更易于理解。查看您的代码,我没有看到任何编写文件的尝试
IO.popen(“diff-Bb#{diffpath}”)。使用
%x
或倒勾可以更轻松地完成读取行
。此外,对于
所需的文件
,不需要使用“.rb”扩展名。如果Ruby找不到没有“.rb”扩展名的文件,它会自动添加“.rb”扩展名。这可能与此相关:另外,您可能希望将您的评论翻译成英语,因为这里不是每个人都说法语;-)该文件是创建,但是无效的,我需要的功能保存完成写入所有文件之前,主程序继续。