annotate lib/redmine/scm/adapters/filesystem_adapter.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 433d4f72a19b
children 4f746d8966dd e248c7af89ec
rev   line source
Chris@1115 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # FileSystem adapter
Chris@0 5 # File written by Paul Rivier, at Demotera.
Chris@0 6 #
Chris@0 7 # This program is free software; you can redistribute it and/or
Chris@0 8 # modify it under the terms of the GNU General Public License
Chris@0 9 # as published by the Free Software Foundation; either version 2
Chris@0 10 # of the License, or (at your option) any later version.
Chris@441 11 #
Chris@0 12 # This program is distributed in the hope that it will be useful,
Chris@0 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 15 # GNU General Public License for more details.
Chris@441 16 #
Chris@0 17 # You should have received a copy of the GNU General Public License
Chris@0 18 # along with this program; if not, write to the Free Software
Chris@0 19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 20
Chris@0 21 require 'redmine/scm/adapters/abstract_adapter'
Chris@0 22 require 'find'
Chris@0 23
Chris@0 24 module Redmine
Chris@0 25 module Scm
Chris@245 26 module Adapters
Chris@0 27 class FilesystemAdapter < AbstractAdapter
Chris@0 28
Chris@245 29 class << self
Chris@245 30 def client_available
Chris@245 31 true
Chris@245 32 end
Chris@245 33 end
Chris@245 34
Chris@245 35 def initialize(url, root_url=nil, login=nil, password=nil,
Chris@245 36 path_encoding=nil)
Chris@0 37 @url = with_trailling_slash(url)
Chris@441 38 @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
Chris@441 39 end
Chris@441 40
Chris@441 41 def path_encoding
Chris@441 42 @path_encoding
Chris@0 43 end
Chris@0 44
Chris@0 45 def format_path_ends(path, leading=true, trailling=true)
Chris@441 46 path = leading ? with_leading_slash(path) :
Chris@0 47 without_leading_slash(path)
Chris@441 48 trailling ? with_trailling_slash(path) :
Chris@441 49 without_trailling_slash(path)
Chris@0 50 end
Chris@0 51
Chris@0 52 def info
Chris@0 53 info = Info.new({:root_url => target(),
Chris@0 54 :lastrev => nil
Chris@0 55 })
Chris@0 56 info
Chris@0 57 rescue CommandFailed
Chris@0 58 return nil
Chris@0 59 end
Chris@245 60
Chris@441 61 def entries(path="", identifier=nil, options={})
Chris@0 62 entries = Entries.new
Chris@245 63 trgt_utf8 = target(path)
Chris@245 64 trgt = scm_iconv(@path_encoding, 'UTF-8', trgt_utf8)
Chris@245 65 Dir.new(trgt).each do |e1|
Chris@245 66 e_utf8 = scm_iconv('UTF-8', @path_encoding, e1)
Chris@441 67 next if e_utf8.blank?
Chris@441 68 relative_path_utf8 = format_path_ends(
Chris@441 69 (format_path_ends(path,false,true) + e_utf8),false,false)
Chris@245 70 t1_utf8 = target(relative_path_utf8)
Chris@245 71 t1 = scm_iconv(@path_encoding, 'UTF-8', t1_utf8)
Chris@245 72 relative_path = scm_iconv(@path_encoding, 'UTF-8', relative_path_utf8)
Chris@245 73 e1 = scm_iconv(@path_encoding, 'UTF-8', e_utf8)
Chris@245 74 if File.exist?(t1) and # paranoid test
Chris@245 75 %w{file directory}.include?(File.ftype(t1)) and # avoid special types
Chris@245 76 not File.basename(e1).match(/^\.+$/) # avoid . and ..
Chris@245 77 p1 = File.readable?(t1) ? relative_path : ""
Chris@245 78 utf_8_path = scm_iconv('UTF-8', @path_encoding, p1)
Chris@245 79 entries <<
Chris@245 80 Entry.new({ :name => scm_iconv('UTF-8', @path_encoding, File.basename(e1)),
Chris@0 81 # below : list unreadable files, but dont link them.
Chris@245 82 :path => utf_8_path,
Chris@245 83 :kind => (File.directory?(t1) ? 'dir' : 'file'),
Chris@245 84 :size => (File.directory?(t1) ? nil : [File.size(t1)].pack('l').unpack('L').first),
Chris@441 85 :lastrev =>
Chris@245 86 Revision.new({:time => (File.mtime(t1)) })
Chris@245 87 })
Chris@245 88 end
Chris@0 89 end
Chris@0 90 entries.sort_by_name
Chris@245 91 rescue => err
Chris@245 92 logger.error "scm: filesystem: error: #{err.message}"
Chris@245 93 raise CommandFailed.new(err.message)
Chris@0 94 end
Chris@245 95
Chris@0 96 def cat(path, identifier=nil)
Chris@245 97 p = scm_iconv(@path_encoding, 'UTF-8', target(path))
Chris@245 98 File.new(p, "rb").read
Chris@245 99 rescue => err
Chris@245 100 logger.error "scm: filesystem: error: #{err.message}"
Chris@245 101 raise CommandFailed.new(err.message)
Chris@0 102 end
Chris@0 103
Chris@0 104 private
Chris@245 105
Chris@0 106 # AbstractAdapter::target is implicitly made to quote paths.
Chris@0 107 # Here we do not shell-out, so we do not want quotes.
Chris@0 108 def target(path=nil)
Chris@245 109 # Prevent the use of ..
Chris@0 110 if path and !path.match(/(^|\/)\.\.(\/|$)/)
Chris@0 111 return "#{self.url}#{without_leading_slash(path)}"
Chris@0 112 end
Chris@0 113 return self.url
Chris@0 114 end
Chris@0 115 end
Chris@0 116 end
Chris@0 117 end
Chris@0 118 end