]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
irclog core module: rename old logs when switching from dir to file
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 10 Aug 2008 12:42:11 +0000 (14:42 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 10 Aug 2008 12:45:18 +0000 (14:45 +0200)
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

index 706a5a7a103b517239ee86fa9b312d16659c93ea..5a44995de29bcd9758cbd276f23a72a72f9260ec 100644 (file)
@@ -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}"