OSDN Git Service

baeb182d9616279f5afe6e353ee667811ec3ad0e
[redminele/redmine.git] / lib / redmine / scm / adapters / filesystem_adapter.rb
1 # redMine - project management software
2 # Copyright (C) 2006-2007  Jean-Philippe Lang
3 #
4 # FileSystem adapter
5 # File written by Paul Rivier, at Demotera.
6 #
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.
11
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.
16
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.
20
21 require 'redmine/scm/adapters/abstract_adapter'
22 require 'find'
23
24 module Redmine
25   module Scm
26     module Adapters
27       class FilesystemAdapter < AbstractAdapter
28
29         class << self
30           def client_available
31             true
32           end
33         end
34
35         def initialize(url, root_url=nil, login=nil, password=nil,
36                        path_encoding=nil)
37           @url = with_trailling_slash(url)
38           @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
39         end
40
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) 
46         end
47
48         def info
49           info = Info.new({:root_url => target(),
50                             :lastrev => nil
51                           })
52           info
53         rescue CommandFailed
54           return nil
55         end
56
57         def entries(path="", identifier=nil, options={})
58           entries = Entries.new
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)
63             next if e_utf8.blank? 
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)
75               entries <<
76                 Entry.new({ :name => scm_iconv('UTF-8', @path_encoding, File.basename(e1)),
77                           # below : list unreadable files, but dont link them.
78                           :path => utf_8_path,
79                           :kind => (File.directory?(t1) ? 'dir' : 'file'),
80                           :size => (File.directory?(t1) ? nil : [File.size(t1)].pack('l').unpack('L').first),
81                           :lastrev => 
82                               Revision.new({:time => (File.mtime(t1)) })
83                         })
84             end
85           end
86           entries.sort_by_name
87         rescue  => err
88           logger.error "scm: filesystem: error: #{err.message}"
89           raise CommandFailed.new(err.message)
90         end
91
92         def cat(path, identifier=nil)
93           p = scm_iconv(@path_encoding, 'UTF-8', target(path))
94           File.new(p, "rb").read
95         rescue  => err
96           logger.error "scm: filesystem: error: #{err.message}"
97           raise CommandFailed.new(err.message)
98         end
99
100         private
101
102         # AbstractAdapter::target is implicitly made to quote paths.
103         # Here we do not shell-out, so we do not want quotes.
104         def target(path=nil)
105           # Prevent the use of ..
106           if path and !path.match(/(^|\/)\.\.(\/|$)/)
107             return "#{self.url}#{without_leading_slash(path)}"
108           end
109           return self.url
110         end
111       end
112     end
113   end
114 end