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,94 +499,100 @@ def ward_sort(a, b)
return 0 return 0
end end
def startup
$stdout.sync=true # line buffered output
@defaults = {'-c' => "#{ENV['HOME']}/.ripnewsrc"}
@defaults = parse_options(@defaults)
@config = parse_config(@defaults)
exit if @config == false
check_config
lock
renice
############################################################################################# trap("HUP") {
print "Rereading config...\n"
$stdout.sync=true # line buffered output config = parse_config(@defaults)
@defaults = {'-c' => "#{ENV['HOME']}/.ripnewsrc"} if config != false
@defaults = parse_options(@defaults) @config = config
@config = parse_config(@defaults) check_config
exit if @config == false print "Done reading config\n"
check_config else
lock print "Keeping old config due to errors\n"
renice end
trap("HUP") {
print "Rereading config...\n"
config = parse_config(@defaults)
if config != false
@config = config
check_config
print "Done reading config\n"
else
print "Keeping old config due to errors\n"
end
}
@maxfilelength = get_max_file_length(@config[@config.keys[0]]["TEMPDIR"])
print "\n$Id$\n"
tstart = Time.now
print "Starting: #{tstart}\n"
if Debuglevel > 2
@config.each_key{|i|
print "Group: #{i}\n"
@config[i].each_key{|j|
print "Opt: #{j} val: #{@config[i][j]}\n"
}
} }
@maxfilelength = get_max_file_length(@config[@config.keys[0]]["TEMPDIR"])
print "\n$Id$\n"
print "Starting: #{@tstart}\n"
if Debuglevel > 2
@config.each_key{|i|
print "Group: #{i}\n"
@config[i].each_key{|j|
print "Opt: #{j} val: #{@config[i][j]}\n"
}
}
end
end end
for group in @config.keys.sort def main
print "\nGetting articles for #{group}\n" for group in @config.keys.sort
@articles = Article.new(@config[group]["NNTPSERVER"], group, @config[group]["NEWSRCNAME"]) print "\nGetting articles for #{group}\n"
@articles.get_articles(@config[group]["CACHEDIR"]) @articles = Article.new(@config[group]["NNTPSERVER"], group, @config[group]["NEWSRCNAME"])
@articles.get_articles(@config[group]["CACHEDIR"])
unless FileTest.directory?("#{@config[group]["DATADIR"]}/#{group}") or unless FileTest.directory?("#{@config[group]["DATADIR"]}/#{group}") or
Dir.mkdir("#{@config[group]["DATADIR"]}/#{group}", @config[group]["PERMISSION"].oct) Dir.mkdir("#{@config[group]["DATADIR"]}/#{group}", @config[group]["PERMISSION"].oct)
print "eeeps, couldn't create dir\n" print "eeeps, couldn't create dir\n"
exit exit
end
for i in @articles.get_group_subjects.sort{|a, b| ward_sort(a, b)}
print "#{i}\n" if Debuglevel > 2
if @config[group].has_key?("-MR") and i =~ /#{@config[group]["-MR"]}/
print "Marking '#{i}' as read\n"
@articles.group_update_newsrc(i)
next
end end
if !(@config[group].has_key?("-X") and i =~ /#{@config[group]["-X"]}/) and for i in @articles.get_group_subjects.sort{|a, b| ward_sort(a, b)}
i =~ /#{@config[group]["-I"]}/ print "#{i}\n" if Debuglevel > 2
print "Match: #{i}\n" if Debuglevel > 0 if @config[group].has_key?("-MR") and i =~ /#{@config[group]["-MR"]}/
if @articles.group_is_complete(i) print "Marking '#{i}' as read\n"
begin @articles.group_update_newsrc(i)
if @articles.group_is_singlepart(i) next
succes = get_single(i, group) end
elsif @articles.group_is_multipart(i) if !(@config[group].has_key?("-X") and i =~ /#{@config[group]["-X"]}/) and
succes = get_multi(i, group) i =~ /#{@config[group]["-I"]}/
end print "Match: #{i}\n" if Debuglevel > 0
if succes == false if @articles.group_is_complete(i)
begin
if @articles.group_is_singlepart(i)
succes = get_single(i, group)
elsif @articles.group_is_multipart(i)
succes = get_multi(i, group)
end
if succes == false
print " Skipping article...\n"
end
rescue Article::TempError, Article::PermError
print "#{$!}\n"
print " Skipping article...\n" print " Skipping article...\n"
next
end end
rescue Article::TempError, Article::PermError else
print "#{$!}\n" print "Not complete: #{i}\n"
print " Skipping article...\n"
next
end end
else
print "Not complete: #{i}\n"
end end
end end
@articles.quit
end end
@articles.quit
end end
tend = Time.now def ending
print "\nFinished: #{tend}\n" tend = Time.now
runtime = (tend - tstart).to_i print "\nFinished: #{tend}\n"
h=runtime/3600 runtime = (tend - @tstart).to_i
m=runtime%3600 h=runtime/3600
s=m%60 m=runtime%3600
m=m/60 s=m%60
printf("Running time: %02d:%02d:%02d\n", h, m, s) m=m/60
unlock printf("Running time: %02d:%02d:%02d\n", h, m, s)
unlock
end
startup
main
ending