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