summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-08-10 14:42:11 +0200
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-08-10 14:45:18 +0200
commit9fcb32ca8058384a4bb91d748fcbd1f81378734d (patch)
tree4eb3b1a25a0c3e2e7d96a863e44649bb55cc5922 /lib
parente4f33c1ea92b56b09c6e58514c73b9a65bf09922 (diff)
irclog core module: rename old logs when switching from dir to file
It may happen that a user changes from an irclog.filename_format where some components are files to a format where they are directories (e.g. from '%%{where}' to '%%{where}/%Y') or conversely. In this case, we rename the existing file/dir by appending '.old.atime' to it (atime is the actual file/dir access time).
Diffstat (limited to 'lib')
-rw-r--r--lib/rbot/core/irclog.rb26
1 files changed, 25 insertions, 1 deletions
diff --git a/lib/rbot/core/irclog.rb b/lib/rbot/core/irclog.rb
index 706a5a7a..5a44995d 100644
--- a/lib/rbot/core/irclog.rb
+++ b/lib/rbot/core/irclog.rb
@@ -273,7 +273,31 @@ class IrcLogModule < CoreBotModule
end
fp = logfilepath(where_str, now)
begin
- FileUtils.mkdir_p File.dirname(fp)
+ dir = File.dirname(fp)
+ # first of all, we check we're not trying to build a directory structure
+ # where one of the components exists already as a file, so we
+ # backtrack along dir until we come across the topmost existing name.
+ # If it's a file, we rename to filename.old.filedate
+ up = dir.dup
+ until File.exist? up
+ up.replace File.dirname up
+ end
+ unless File.directory? up
+ backup = up.dup
+ backup << ".old." << File.atime(up).strftime('%Y%m%d%H%M%S')
+ debug "#{up} is not a directory! renaming to #{backup}"
+ File.rename(up, backup)
+ end
+ FileUtils.mkdir_p(dir)
+ # conversely, it may happen that fp exists and is a directory, in
+ # which case we rename the directory instead
+ if File.directory? fp
+ backup = fp.dup
+ backup << ".old." << File.atime(fp).strftime('%Y%m%d%H%M%S')
+ debug "#{fp} is not a file! renaming to #{backup}"
+ File.rename(fp, backup)
+ end
+ # it should be fine to create the file now
f = File.new(fp, "a")
f.sync = true
f.puts "[#{stamp}] @ Log started by #{@bot.myself.nick}"