432: def secure_open(file,must_be_w,&block)
433: raise Puppet::DevError,"secure_open only works with mode 'w'" unless must_be_w == 'w'
434: raise Puppet::DevError,"secure_open only requires a block" unless block_given?
435: Puppet.warning "#{file} was a symlink to #{File.readlink(file)}" if File.symlink?(file)
436: if File.exists?(file) or File.symlink?(file)
437: wait = File.symlink?(file) ? 5.0 : 0.1
438: File.delete(file)
439: sleep wait
440: end
441: begin
442: File.open(file,File::CREAT|File::EXCL|File::TRUNC|File::WRONLY,&block)
443: rescue Errno::EEXIST
444: desc = File.symlink?(file) ? "symlink to #{File.readlink(file)}" : File.stat(file).ftype
445: puts "Warning: #{file} was apparently created by another process (as"
446: puts "a #{desc}) as soon as it was deleted by this process. Someone may be trying"
447: puts "to do something objectionable (such as tricking you into overwriting system"
448: puts "files if you are running as root)."
449: raise
450: end
451: end