Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # FileSystem adapter Chris@1296: # File written by Paul Rivier, at Demotera. Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: require 'redmine/scm/adapters/abstract_adapter' Chris@1296: require 'find' Chris@1296: Chris@1296: module Redmine Chris@1296: module Scm Chris@1296: module Adapters Chris@1296: class FilesystemAdapter < AbstractAdapter Chris@1296: Chris@1296: class << self Chris@1296: def client_available Chris@1296: true Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def initialize(url, root_url=nil, login=nil, password=nil, Chris@1296: path_encoding=nil) Chris@1296: @url = with_trailling_slash(url) Chris@1296: @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding Chris@1296: end Chris@1296: Chris@1296: def path_encoding Chris@1296: @path_encoding Chris@1296: end Chris@1296: Chris@1296: def format_path_ends(path, leading=true, trailling=true) Chris@1296: path = leading ? with_leading_slash(path) : Chris@1296: without_leading_slash(path) Chris@1296: trailling ? with_trailling_slash(path) : Chris@1296: without_trailling_slash(path) Chris@1296: end Chris@1296: Chris@1296: def info Chris@1296: info = Info.new({:root_url => target(), Chris@1296: :lastrev => nil Chris@1296: }) Chris@1296: info Chris@1296: rescue CommandFailed Chris@1296: return nil Chris@1296: end Chris@1296: Chris@1296: def entries(path="", identifier=nil, options={}) Chris@1296: entries = Entries.new Chris@1296: trgt_utf8 = target(path) Chris@1296: trgt = scm_iconv(@path_encoding, 'UTF-8', trgt_utf8) Chris@1296: Dir.new(trgt).each do |e1| Chris@1296: e_utf8 = scm_iconv('UTF-8', @path_encoding, e1) Chris@1296: next if e_utf8.blank? Chris@1296: relative_path_utf8 = format_path_ends( Chris@1296: (format_path_ends(path,false,true) + e_utf8),false,false) Chris@1296: t1_utf8 = target(relative_path_utf8) Chris@1296: t1 = scm_iconv(@path_encoding, 'UTF-8', t1_utf8) Chris@1296: relative_path = scm_iconv(@path_encoding, 'UTF-8', relative_path_utf8) Chris@1296: e1 = scm_iconv(@path_encoding, 'UTF-8', e_utf8) Chris@1296: if File.exist?(t1) and # paranoid test Chris@1296: %w{file directory}.include?(File.ftype(t1)) and # avoid special types Chris@1296: not File.basename(e1).match(/^\.+$/) # avoid . and .. Chris@1296: p1 = File.readable?(t1) ? relative_path : "" Chris@1296: utf_8_path = scm_iconv('UTF-8', @path_encoding, p1) Chris@1296: entries << Chris@1296: Entry.new({ :name => scm_iconv('UTF-8', @path_encoding, File.basename(e1)), Chris@1296: # below : list unreadable files, but dont link them. Chris@1296: :path => utf_8_path, Chris@1296: :kind => (File.directory?(t1) ? 'dir' : 'file'), Chris@1296: :size => (File.directory?(t1) ? nil : [File.size(t1)].pack('l').unpack('L').first), Chris@1296: :lastrev => Chris@1296: Revision.new({:time => (File.mtime(t1)) }) Chris@1296: }) Chris@1296: end Chris@1296: end Chris@1296: entries.sort_by_name Chris@1296: rescue => err Chris@1296: logger.error "scm: filesystem: error: #{err.message}" Chris@1296: raise CommandFailed.new(err.message) Chris@1296: end Chris@1296: Chris@1296: def cat(path, identifier=nil) Chris@1296: p = scm_iconv(@path_encoding, 'UTF-8', target(path)) Chris@1296: File.new(p, "rb").read Chris@1296: rescue => err Chris@1296: logger.error "scm: filesystem: error: #{err.message}" Chris@1296: raise CommandFailed.new(err.message) Chris@1296: end Chris@1296: Chris@1296: private Chris@1296: Chris@1296: # AbstractAdapter::target is implicitly made to quote paths. Chris@1296: # Here we do not shell-out, so we do not want quotes. Chris@1296: def target(path=nil) Chris@1296: # Prevent the use of .. Chris@1296: if path and !path.match(/(^|\/)\.\.(\/|$)/) Chris@1296: return "#{self.url}#{without_leading_slash(path)}" Chris@1296: end Chris@1296: return self.url Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end