1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
5 # File written by Paul Rivier, at Demotera.
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 require 'redmine/scm/adapters/abstract_adapter'
27 class FilesystemAdapter < AbstractAdapter
35 def initialize(url, root_url=nil, login=nil, password=nil,
37 @url = with_trailling_slash(url)
38 @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
41 def format_path_ends(path, leading=true, trailling=true)
42 path = leading ? with_leading_slash(path) :
43 without_leading_slash(path)
44 trailling ? with_trailling_slash(path) :
45 without_trailling_slash(path)
49 info = Info.new({:root_url => target(),
57 def entries(path="", identifier=nil, options={})
59 trgt_utf8 = target(path)
60 trgt = scm_iconv(@path_encoding, 'UTF-8', trgt_utf8)
61 Dir.new(trgt).each do |e1|
62 e_utf8 = scm_iconv('UTF-8', @path_encoding, e1)
64 relative_path_utf8 = format_path_ends(
65 (format_path_ends(path,false,true) + e_utf8),false,false)
66 t1_utf8 = target(relative_path_utf8)
67 t1 = scm_iconv(@path_encoding, 'UTF-8', t1_utf8)
68 relative_path = scm_iconv(@path_encoding, 'UTF-8', relative_path_utf8)
69 e1 = scm_iconv(@path_encoding, 'UTF-8', e_utf8)
70 if File.exist?(t1) and # paranoid test
71 %w{file directory}.include?(File.ftype(t1)) and # avoid special types
72 not File.basename(e1).match(/^\.+$/) # avoid . and ..
73 p1 = File.readable?(t1) ? relative_path : ""
74 utf_8_path = scm_iconv('UTF-8', @path_encoding, p1)
76 Entry.new({ :name => scm_iconv('UTF-8', @path_encoding, File.basename(e1)),
77 # below : list unreadable files, but dont link them.
79 :kind => (File.directory?(t1) ? 'dir' : 'file'),
80 :size => (File.directory?(t1) ? nil : [File.size(t1)].pack('l').unpack('L').first),
82 Revision.new({:time => (File.mtime(t1)) })
88 logger.error "scm: filesystem: error: #{err.message}"
89 raise CommandFailed.new(err.message)
92 def cat(path, identifier=nil)
93 p = scm_iconv(@path_encoding, 'UTF-8', target(path))
94 File.new(p, "rb").read
96 logger.error "scm: filesystem: error: #{err.message}"
97 raise CommandFailed.new(err.message)
102 # AbstractAdapter::target is implicitly made to quote paths.
103 # Here we do not shell-out, so we do not want quotes.
105 # Prevent the use of ..
106 if path and !path.match(/(^|\/)\.\.(\/|$)/)
107 return "#{self.url}#{without_leading_slash(path)}"