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