From 9fcb32ca8058384a4bb91d748fcbd1f81378734d Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sun, 10 Aug 2008 14:42:11 +0200 Subject: [PATCH] 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). --- lib/rbot/core/irclog.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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}" -- 2.39.5