bit of a cleanup and make a race condition in get_max_file_length less likely to occur

This commit is contained in:
Ward Wouts 2004-05-19 09:25:40 +00:00
parent ca9fbd8029
commit d72ad30661

View file

@ -1,10 +1,10 @@
#!/usr/local/bin/ruby -w #!/usr/local/bin/ruby -w
# $Dwarf: ripnews.rb,v 1.78 2003/09/23 14:43:26 ward Exp $ # $Dwarf: ripnews.rb,v 1.79 2004/03/03 21:18:50 ward Exp $
# $Source$ # $Source$
# #
# Copyright (c) 2002, 2003 Ward Wouts <ward@wouts.nl> # Copyright (c) 2002, 2003, 2004 Ward Wouts <ward@wouts.nl>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above # purpose with or without fee is hereby granted, provided that the above
@ -33,6 +33,7 @@ require 'encode/yenc'
########################################################################### ###########################################################################
Debuglevel = 0 Debuglevel = 0
@tstart = Time.now
def save_file(dir, name, data) def save_file(dir, name, data)
print "savename: #{name}\n" if Debuglevel > 1 print "savename: #{name}\n" if Debuglevel > 1
@ -460,13 +461,20 @@ def get_max_file_length(tempdir=".")
print "Tempdir '#{tempdir}' is not a writable directory\n" print "Tempdir '#{tempdir}' is not a writable directory\n"
exit exit
end end
# this is quite stupid, there is no guarantee at all the generated file names
# don't already exist
name = "a"*500 name = "a"*500
name = "#$$#{name}"
begin begin
file = File.new("#{tempdir}/#{name}", "w", "0644").close file = File.new("#{tempdir}/#{name}", "w", "0644").close
File.delete("#{tempdir}/#{name}") File.delete("#{tempdir}/#{name}")
rescue Errno::ENAMETOOLONG rescue Errno::ENAMETOOLONG
name = name[0...-1] name = name[0...-1]
retry retry
rescue Errno::ENOENT
print "#{$!}\n"
print "raar hoor\n"
retry
end end
# this is how many characters are still likely to be appended # this is how many characters are still likely to be appended
# is the filename already exists '-<#{date}.#{count}>' in save_file # is the filename already exists '-<#{date}.#{count}>' in save_file
@ -491,9 +499,7 @@ def ward_sort(a, b)
return 0 return 0
end end
def startup
#############################################################################################
$stdout.sync=true # line buffered output $stdout.sync=true # line buffered output
@defaults = {'-c' => "#{ENV['HOME']}/.ripnewsrc"} @defaults = {'-c' => "#{ENV['HOME']}/.ripnewsrc"}
@defaults = parse_options(@defaults) @defaults = parse_options(@defaults)
@ -518,8 +524,7 @@ trap("HUP") {
@maxfilelength = get_max_file_length(@config[@config.keys[0]]["TEMPDIR"]) @maxfilelength = get_max_file_length(@config[@config.keys[0]]["TEMPDIR"])
print "\n$Id$\n" print "\n$Id$\n"
tstart = Time.now print "Starting: #{@tstart}\n"
print "Starting: #{tstart}\n"
if Debuglevel > 2 if Debuglevel > 2
@config.each_key{|i| @config.each_key{|i|
@ -529,7 +534,9 @@ if Debuglevel > 2
} }
} }
end end
end
def main
for group in @config.keys.sort for group in @config.keys.sort
print "\nGetting articles for #{group}\n" print "\nGetting articles for #{group}\n"
@articles = Article.new(@config[group]["NNTPSERVER"], group, @config[group]["NEWSRCNAME"]) @articles = Article.new(@config[group]["NNTPSERVER"], group, @config[group]["NEWSRCNAME"])
@ -572,13 +579,20 @@ for group in @config.keys.sort
end end
@articles.quit @articles.quit
end end
end
def ending
tend = Time.now tend = Time.now
print "\nFinished: #{tend}\n" print "\nFinished: #{tend}\n"
runtime = (tend - tstart).to_i runtime = (tend - @tstart).to_i
h=runtime/3600 h=runtime/3600
m=runtime%3600 m=runtime%3600
s=m%60 s=m%60
m=m/60 m=m/60
printf("Running time: %02d:%02d:%02d\n", h, m, s) printf("Running time: %02d:%02d:%02d\n", h, m, s)
unlock unlock
end
startup
main
ending