Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
10 KiB

  1. package File::Spec::Epoc;
  2. use strict;
  3. use Cwd;
  4. use vars qw(@ISA);
  5. require File::Spec::Unix;
  6. @ISA = qw(File::Spec::Unix);
  7. =head1 NAME
  8. File::Spec::Epoc - methods for Epoc file specs
  9. =head1 SYNOPSIS
  10. require File::Spec::Epoc; # Done internally by File::Spec if needed
  11. =head1 DESCRIPTION
  12. See File::Spec::Unix for a documentation of the methods provided
  13. there. This package overrides the implementation of these methods, not
  14. the semantics.
  15. This package is still work in progress ;-)
  16. o.flebbe@gmx.de
  17. =over
  18. =item devnull
  19. Returns a string representation of the null device.
  20. =cut
  21. sub devnull {
  22. return "nul:";
  23. }
  24. =item tmpdir
  25. Returns a string representation of a temporay directory:
  26. =cut
  27. my $tmpdir;
  28. sub tmpdir {
  29. return "C:/System/temp";
  30. }
  31. sub case_tolerant {
  32. return 1;
  33. }
  34. sub file_name_is_absolute {
  35. my ($self,$file) = @_;
  36. return scalar($file =~ m{^([a-z?]:)?[\\/]}is);
  37. }
  38. =item path
  39. Takes no argument, returns the environment variable PATH as an array. Since
  40. there is no search path supported, it returns undef, sorry.
  41. =cut
  42. sub path {
  43. return undef;
  44. }
  45. =item canonpath
  46. No physical check on the filesystem, but a logical cleanup of a
  47. path. On UNIX eliminated successive slashes and successive "/.".
  48. =cut
  49. sub canonpath {
  50. my ($self,$path) = @_;
  51. $path =~ s/^([a-z]:)/\u$1/s;
  52. $path =~ s|/+|/|g unless($^O eq 'cygwin'); # xx////xx -> xx/xx
  53. $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx
  54. $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
  55. $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
  56. $path =~ s|/\z|| unless $path eq "/"; # xx/ -> xx
  57. return $path;
  58. }
  59. =item splitpath
  60. ($volume,$directories,$file) = File::Spec->splitpath( $path );
  61. ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
  62. Splits a path in to volume, directory, and filename portions. Assumes that
  63. the last file is a path unless the path ends in '\\', '\\.', '\\..'
  64. or $no_file is true. On Win32 this means that $no_file true makes this return
  65. ( $volume, $path, undef ).
  66. Separators accepted are \ and /.
  67. The results can be passed to L</catpath> to get back a path equivalent to
  68. (usually identical to) the original path.
  69. =cut
  70. sub splitpath {
  71. my ($self,$path, $nofile) = @_;
  72. my ($volume,$directory,$file) = ('','','');
  73. if ( $nofile ) {
  74. $path =~
  75. m{^( (?:[a-zA-Z?]:|(?:\\\\|//)[^\\/]+[\\/][^\\/]+)? )
  76. (.*)
  77. }xs;
  78. $volume = $1;
  79. $directory = $2;
  80. }
  81. else {
  82. $path =~
  83. m{^ ( (?: [a-zA-Z?]: |
  84. (?:\\\\|//)[^\\/]+[\\/][^\\/]+
  85. )?
  86. )
  87. ( (?:.*[\\\\/](?:\.\.?\z)?)? )
  88. (.*)
  89. }xs;
  90. $volume = $1;
  91. $directory = $2;
  92. $file = $3;
  93. }
  94. return ($volume,$directory,$file);
  95. }
  96. =item splitdir
  97. The opposite of L</catdir()>.
  98. @dirs = File::Spec->splitdir( $directories );
  99. $directories must be only the directory portion of the path on systems
  100. that have the concept of a volume or that have path syntax that differentiates
  101. files from directories.
  102. Unlike just splitting the directories on the separator, leading empty and
  103. trailing directory entries can be returned, because these are significant
  104. on some OSs. So,
  105. File::Spec->splitdir( "/a/b/c" );
  106. Yields:
  107. ( '', 'a', 'b', '', 'c', '' )
  108. =cut
  109. sub splitdir {
  110. my ($self,$directories) = @_ ;
  111. #
  112. # split() likes to forget about trailing null fields, so here we
  113. # check to be sure that there will not be any before handling the
  114. # simple case.
  115. #
  116. if ( $directories !~ m|[\\/]\z| ) {
  117. return split( m|[\\/]|, $directories );
  118. }
  119. else {
  120. #
  121. # since there was a trailing separator, add a file name to the end,
  122. # then do the split, then replace it with ''.
  123. #
  124. my( @directories )= split( m|[\\/]|, "${directories}dummy" ) ;
  125. $directories[ $#directories ]= '' ;
  126. return @directories ;
  127. }
  128. }
  129. =item catpath
  130. Takes volume, directory and file portions and returns an entire path. Under
  131. Unix, $volume is ignored, and this is just like catfile(). On other OSs,
  132. the $volume become significant.
  133. =cut
  134. sub catpath {
  135. my ($self,$volume,$directory,$file) = @_;
  136. # If it's UNC, make sure the glue separator is there, reusing
  137. # whatever separator is first in the $volume
  138. $volume .= $1
  139. if ( $volume =~ m@^([\\/])[\\/][^\\/]+[\\/][^\\/]+\z@s &&
  140. $directory =~ m@^[^\\/]@s
  141. ) ;
  142. $volume .= $directory ;
  143. # If the volume is not just A:, make sure the glue separator is
  144. # there, reusing whatever separator is first in the $volume if possible.
  145. if ( $volume !~ m@^[a-zA-Z]:\z@s &&
  146. $volume =~ m@[^\\/]\z@ &&
  147. $file =~ m@[^\\/]@
  148. ) {
  149. $volume =~ m@([\\/])@ ;
  150. my $sep = $1 ? $1 : '\\' ;
  151. $volume .= $sep ;
  152. }
  153. $volume .= $file ;
  154. return $volume ;
  155. }
  156. =item abs2rel
  157. Takes a destination path and an optional base path returns a relative path
  158. from the base path to the destination path:
  159. $rel_path = File::Spec->abs2rel( $destination ) ;
  160. $rel_path = File::Spec->abs2rel( $destination, $base ) ;
  161. If $base is not present or '', then L</cwd()> is used. If $base is relative,
  162. then it is converted to absolute form using L</rel2abs()>. This means that it
  163. is taken to be relative to L<cwd()>.
  164. On systems with the concept of a volume, this assumes that both paths
  165. are on the $destination volume, and ignores the $base volume.
  166. On systems that have a grammar that indicates filenames, this ignores the
  167. $base filename as well. Otherwise all path components are assumed to be
  168. directories.
  169. If $path is relative, it is converted to absolute form using L</rel2abs()>.
  170. This means that it is taken to be relative to L</cwd()>.
  171. Based on code written by Shigio Yamaguchi.
  172. No checks against the filesystem are made.
  173. =cut
  174. sub abs2rel {
  175. my($self,$path,$base) = @_;
  176. # Clean up $path
  177. if ( ! $self->file_name_is_absolute( $path ) ) {
  178. $path = $self->rel2abs( $path ) ;
  179. }
  180. else {
  181. $path = $self->canonpath( $path ) ;
  182. }
  183. # Figure out the effective $base and clean it up.
  184. if ( ! $self->file_name_is_absolute( $base ) ) {
  185. $base = $self->rel2abs( $base ) ;
  186. }
  187. elsif ( !defined( $base ) || $base eq '' ) {
  188. $base = cwd() ;
  189. }
  190. else {
  191. $base = $self->canonpath( $base ) ;
  192. }
  193. # Split up paths
  194. my ( $path_volume, $path_directories, $path_file ) =
  195. $self->splitpath( $path, 1 ) ;
  196. my ( undef, $base_directories, undef ) =
  197. $self->splitpath( $base, 1 ) ;
  198. # Now, remove all leading components that are the same
  199. my @pathchunks = $self->splitdir( $path_directories );
  200. my @basechunks = $self->splitdir( $base_directories );
  201. while ( @pathchunks &&
  202. @basechunks &&
  203. lc( $pathchunks[0] ) eq lc( $basechunks[0] )
  204. ) {
  205. shift @pathchunks ;
  206. shift @basechunks ;
  207. }
  208. # No need to catdir, we know these are well formed.
  209. $path_directories = CORE::join( '\\', @pathchunks );
  210. $base_directories = CORE::join( '\\', @basechunks );
  211. # $base_directories now contains the directories the resulting relative
  212. # path must ascend out of before it can descend to $path_directory. So,
  213. # replace all names with $parentDir
  214. #FA Need to replace between backslashes...
  215. $base_directories =~ s|[^\\]+|..|g ;
  216. # Glue the two together, using a separator if necessary, and preventing an
  217. # empty result.
  218. #FA Must check that new directories are not empty.
  219. if ( $path_directories ne '' && $base_directories ne '' ) {
  220. $path_directories = "$base_directories\\$path_directories" ;
  221. } else {
  222. $path_directories = "$base_directories$path_directories" ;
  223. }
  224. # It makes no sense to add a relative path to a UNC volume
  225. $path_volume = '' unless $path_volume =~ m{^[A-Z]:}is ;
  226. return $self->canonpath(
  227. $self->catpath($path_volume, $path_directories, $path_file )
  228. ) ;
  229. }
  230. =item rel2abs
  231. Converts a relative path to an absolute path.
  232. $abs_path = File::Spec->rel2abs( $destination ) ;
  233. $abs_path = File::Spec->rel2abs( $destination, $base ) ;
  234. If $base is not present or '', then L<cwd()> is used. If $base is relative,
  235. then it is converted to absolute form using L</rel2abs()>. This means that it
  236. is taken to be relative to L</cwd()>.
  237. Assumes that both paths are on the $base volume, and ignores the
  238. $destination volume.
  239. On systems that have a grammar that indicates filenames, this ignores the
  240. $base filename as well. Otherwise all path components are assumed to be
  241. directories.
  242. If $path is absolute, it is cleaned up and returned using L</canonpath()>.
  243. Based on code written by Shigio Yamaguchi.
  244. No checks against the filesystem are made.
  245. =cut
  246. sub rel2abs($;$;) {
  247. my ($self,$path,$base ) = @_;
  248. if ( ! $self->file_name_is_absolute( $path ) ) {
  249. if ( !defined( $base ) || $base eq '' ) {
  250. $base = cwd() ;
  251. }
  252. elsif ( ! $self->file_name_is_absolute( $base ) ) {
  253. $base = $self->rel2abs( $base ) ;
  254. }
  255. else {
  256. $base = $self->canonpath( $base ) ;
  257. }
  258. my ( undef, $path_directories, $path_file ) =
  259. $self->splitpath( $path, 1 ) ;
  260. my ( $base_volume, $base_directories, undef ) =
  261. $self->splitpath( $base, 1 ) ;
  262. $path = $self->catpath(
  263. $base_volume,
  264. $self->catdir( $base_directories, $path_directories ),
  265. $path_file
  266. ) ;
  267. }
  268. return $self->canonpath( $path ) ;
  269. }
  270. =back
  271. =head1 SEE ALSO
  272. L<File::Spec>
  273. =cut
  274. 1;