1

When I execute this program, it works well but the verification returns false. If I re-execute it, the verification works.

fullpath is the directory of the backup, and refpath is the path to the original files:

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 

The main program is:

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)

When I open the directory where the files are restored, the files are not completely written.

Before calling the verification, I call save, backup and verification.

The sleep doesn't work. The process is completely paused and won't write the missing files.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Quentin
  • 75
  • 1
  • 2
  • 11
  • What functions do save / restore use to write the files? – Taum Feb 07 '14 at 14:52
  • 1
    I cleaned up your question to (hopefully) make it more understandable. Looking at your code, I don't see any attempt to write a file. `IO.popen("diff -Bb #{diffpath}").readlines` could be more easily done using `%x` or backticks. Also, you don't need to use ".rb" extensions for files you `require`. Ruby automatically adds the ".rb" extension if it can't find the file without it. – the Tin Man Feb 07 '14 at 14:53
  • This may be relevant: http://stackoverflow.com/questions/6701103/understanding-ruby-and-os-i-o-buffering Also, you probably want to translate your comments into english as not everyone speaks french here ;-) – Taum Feb 07 '14 at 14:53

2 Answers2

1

This hasn't been tested, but is more how I'd write the first part:

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 

In general you can simplify your tests. While it's nice that Ruby has the empty? method to see if something has content, it's more obvious if you use == '' or != ''.

Using fullpath[refpath] will return a matching string or nil, so you have a "truthy/falsey" response there, with less code noise.

Use backticks or %x to get the output of your "diff" instead of using popen with readlines.

In general, your code looked like you're coming from Java. Ruby has a very elegant syntax and writing style so take advantage of it.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • The file is create but is void and i need the function save finishes write all the files before the main program continues. – Quentin Feb 07 '14 at 15:23
0

How many gigabytes does your original file have in size? I suspect if sleep 5.0 isn't really meaningful and the root cause is something else. Or do you use a slow USB flash memory as the backup directory?

If you are sure that you need to wait for the writing process to complete, perhaps you can do polling on mtime of the backup file:

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
...

When the backup process is in the middle of writing into the output file, File.mtime(fullpath) should be within 2 seconds from Time.now Be careful of the FAT filesystem with 2 seconds time resolution. I also used abs because some backup programs modify mtime value as they want.

nodakai
  • 7,773
  • 3
  • 30
  • 60