Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 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@0: # 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@0: # 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@0: module Adapters Chris@0: class FilesystemAdapter < AbstractAdapter Chris@0: Chris@0: Chris@0: def initialize(url, root_url=nil, login=nil, password=nil) Chris@0: @url = with_trailling_slash(url) Chris@0: end Chris@0: Chris@0: def format_path_ends(path, leading=true, trailling=true) Chris@0: path = leading ? with_leading_slash(path) : Chris@0: without_leading_slash(path) Chris@0: trailling ? with_trailling_slash(path) : Chris@0: 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@0: Chris@0: def entries(path="", identifier=nil) Chris@0: entries = Entries.new Chris@0: Dir.new(target(path)).each do |e| Chris@0: relative_path = format_path_ends((format_path_ends(path, Chris@0: false, Chris@0: true) + e), Chris@0: false,false) Chris@0: target = target(relative_path) Chris@0: entries << Chris@0: Entry.new({ :name => File.basename(e), Chris@0: # below : list unreadable files, but dont link them. Chris@0: :path => File.readable?(target) ? relative_path : "", Chris@0: :kind => (File.directory?(target) ? 'dir' : 'file'), Chris@0: :size => (File.directory?(target) ? nil : [File.size(target)].pack('l').unpack('L').first), Chris@0: :lastrev => Chris@0: Revision.new({:time => (File.mtime(target)).localtime, Chris@0: }) Chris@0: }) if File.exist?(target) and # paranoid test Chris@0: %w{file directory}.include?(File.ftype(target)) and # avoid special types Chris@0: not File.basename(e).match(/^\.+$/) # avoid . and .. Chris@0: end Chris@0: entries.sort_by_name Chris@0: end Chris@0: Chris@0: def cat(path, identifier=nil) Chris@0: File.new(target(path), "rb").read Chris@0: end Chris@0: Chris@0: private Chris@0: 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@0: #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: Chris@0: end Chris@0: end Chris@0: end Chris@0: end