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.

458 lines
11 KiB

  1. package File::Spec::Unix;
  2. use strict;
  3. use vars qw($VERSION);
  4. $VERSION = '1.2';
  5. use Cwd;
  6. =head1 NAME
  7. File::Spec::Unix - methods used by File::Spec
  8. =head1 SYNOPSIS
  9. require File::Spec::Unix; # Done automatically by File::Spec
  10. =head1 DESCRIPTION
  11. Methods for manipulating file specifications.
  12. =head1 METHODS
  13. =over 2
  14. =item canonpath
  15. No physical check on the filesystem, but a logical cleanup of a
  16. path. On UNIX eliminated successive slashes and successive "/.".
  17. $cpath = File::Spec->canonpath( $path ) ;
  18. =cut
  19. sub canonpath {
  20. my ($self,$path) = @_;
  21. $path =~ s|/+|/|g unless($^O eq 'cygwin'); # xx////xx -> xx/xx
  22. $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx
  23. $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
  24. $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
  25. $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
  26. return $path;
  27. }
  28. =item catdir
  29. Concatenate two or more directory names to form a complete path ending
  30. with a directory. But remove the trailing slash from the resulting
  31. string, because it doesn't look good, isn't necessary and confuses
  32. OS2. Of course, if this is the root directory, don't cut off the
  33. trailing slash :-)
  34. =cut
  35. sub catdir {
  36. my $self = shift;
  37. my @args = @_;
  38. foreach (@args) {
  39. # append a slash to each argument unless it has one there
  40. $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
  41. }
  42. return $self->canonpath(join('', @args));
  43. }
  44. =item catfile
  45. Concatenate one or more directory names and a filename to form a
  46. complete path ending with a filename
  47. =cut
  48. sub catfile {
  49. my $self = shift;
  50. my $file = pop @_;
  51. return $file unless @_;
  52. my $dir = $self->catdir(@_);
  53. $dir .= "/" unless substr($dir,-1) eq "/";
  54. return $dir.$file;
  55. }
  56. =item curdir
  57. Returns a string representation of the current directory. "." on UNIX.
  58. =cut
  59. sub curdir {
  60. return ".";
  61. }
  62. =item devnull
  63. Returns a string representation of the null device. "/dev/null" on UNIX.
  64. =cut
  65. sub devnull {
  66. return "/dev/null";
  67. }
  68. =item rootdir
  69. Returns a string representation of the root directory. "/" on UNIX.
  70. =cut
  71. sub rootdir {
  72. return "/";
  73. }
  74. =item tmpdir
  75. Returns a string representation of the first writable directory
  76. from the following list or "" if none are writable:
  77. $ENV{TMPDIR}
  78. /tmp
  79. =cut
  80. my $tmpdir;
  81. sub tmpdir {
  82. return $tmpdir if defined $tmpdir;
  83. foreach ($ENV{TMPDIR}, "/tmp") {
  84. next unless defined && -d && -w _;
  85. $tmpdir = $_;
  86. last;
  87. }
  88. $tmpdir = '' unless defined $tmpdir;
  89. return $tmpdir;
  90. }
  91. =item updir
  92. Returns a string representation of the parent directory. ".." on UNIX.
  93. =cut
  94. sub updir {
  95. return "..";
  96. }
  97. =item no_upwards
  98. Given a list of file names, strip out those that refer to a parent
  99. directory. (Does not strip symlinks, only '.', '..', and equivalents.)
  100. =cut
  101. sub no_upwards {
  102. my $self = shift;
  103. return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
  104. }
  105. =item case_tolerant
  106. Returns a true or false value indicating, respectively, that alphabetic
  107. is not or is significant when comparing file specifications.
  108. =cut
  109. sub case_tolerant {
  110. return 0;
  111. }
  112. =item file_name_is_absolute
  113. Takes as argument a path and returns true if it is an absolute path.
  114. This does not consult the local filesystem on Unix, Win32, or OS/2. It
  115. does sometimes on MacOS (see L<File::Spec::MacOS/file_name_is_absolute>).
  116. It does consult the working environment for VMS (see
  117. L<File::Spec::VMS/file_name_is_absolute>).
  118. =cut
  119. sub file_name_is_absolute {
  120. my ($self,$file) = @_;
  121. return scalar($file =~ m:^/:s);
  122. }
  123. =item path
  124. Takes no argument, returns the environment variable PATH as an array.
  125. =cut
  126. sub path {
  127. my @path = split(':', $ENV{PATH});
  128. foreach (@path) { $_ = '.' if $_ eq '' }
  129. return @path;
  130. }
  131. =item join
  132. join is the same as catfile.
  133. =cut
  134. sub join {
  135. my $self = shift;
  136. return $self->catfile(@_);
  137. }
  138. =item splitpath
  139. ($volume,$directories,$file) = File::Spec->splitpath( $path );
  140. ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
  141. Splits a path in to volume, directory, and filename portions. On systems
  142. with no concept of volume, returns undef for volume.
  143. For systems with no syntax differentiating filenames from directories,
  144. assumes that the last file is a path unless $no_file is true or a
  145. trailing separator or /. or /.. is present. On Unix this means that $no_file
  146. true makes this return ( '', $path, '' ).
  147. The directory portion may or may not be returned with a trailing '/'.
  148. The results can be passed to L</catpath()> to get back a path equivalent to
  149. (usually identical to) the original path.
  150. =cut
  151. sub splitpath {
  152. my ($self,$path, $nofile) = @_;
  153. my ($volume,$directory,$file) = ('','','');
  154. if ( $nofile ) {
  155. $directory = $path;
  156. }
  157. else {
  158. $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
  159. $directory = $1;
  160. $file = $2;
  161. }
  162. return ($volume,$directory,$file);
  163. }
  164. =item splitdir
  165. The opposite of L</catdir()>.
  166. @dirs = File::Spec->splitdir( $directories );
  167. $directories must be only the directory portion of the path on systems
  168. that have the concept of a volume or that have path syntax that differentiates
  169. files from directories.
  170. Unlike just splitting the directories on the separator, empty
  171. directory names (C<''>) can be returned, because these are significant
  172. on some OSs (e.g. MacOS).
  173. On Unix,
  174. File::Spec->splitdir( "/a/b//c/" );
  175. Yields:
  176. ( '', 'a', 'b', '', 'c', '' )
  177. =cut
  178. sub splitdir {
  179. my ($self,$directories) = @_ ;
  180. #
  181. # split() likes to forget about trailing null fields, so here we
  182. # check to be sure that there will not be any before handling the
  183. # simple case.
  184. #
  185. if ( $directories !~ m|/\Z(?!\n)| ) {
  186. return split( m|/|, $directories );
  187. }
  188. else {
  189. #
  190. # since there was a trailing separator, add a file name to the end,
  191. # then do the split, then replace it with ''.
  192. #
  193. my( @directories )= split( m|/|, "${directories}dummy" ) ;
  194. $directories[ $#directories ]= '' ;
  195. return @directories ;
  196. }
  197. }
  198. =item catpath
  199. Takes volume, directory and file portions and returns an entire path. Under
  200. Unix, $volume is ignored, and directory and file are catenated. A '/' is
  201. inserted if need be. On other OSs, $volume is significant.
  202. =cut
  203. sub catpath {
  204. my ($self,$volume,$directory,$file) = @_;
  205. if ( $directory ne '' &&
  206. $file ne '' &&
  207. substr( $directory, -1 ) ne '/' &&
  208. substr( $file, 0, 1 ) ne '/'
  209. ) {
  210. $directory .= "/$file" ;
  211. }
  212. else {
  213. $directory .= $file ;
  214. }
  215. return $directory ;
  216. }
  217. =item abs2rel
  218. Takes a destination path and an optional base path returns a relative path
  219. from the base path to the destination path:
  220. $rel_path = File::Spec->abs2rel( $path ) ;
  221. $rel_path = File::Spec->abs2rel( $path, $base ) ;
  222. If $base is not present or '', then L<cwd()> is used. If $base is relative,
  223. then it is converted to absolute form using L</rel2abs()>. This means that it
  224. is taken to be relative to L<cwd()>.
  225. On systems with the concept of a volume, this assumes that both paths
  226. are on the $destination volume, and ignores the $base volume.
  227. On systems that have a grammar that indicates filenames, this ignores the
  228. $base filename as well. Otherwise all path components are assumed to be
  229. directories.
  230. If $path is relative, it is converted to absolute form using L</rel2abs()>.
  231. This means that it is taken to be relative to L<cwd()>.
  232. No checks against the filesystem are made on most systems. On MacOS,
  233. the filesystem may be consulted (see
  234. L<File::Spec::MacOS/file_name_is_absolute>). On VMS, there is
  235. interaction with the working environment, as logicals and
  236. macros are expanded.
  237. Based on code written by Shigio Yamaguchi.
  238. =cut
  239. sub abs2rel {
  240. my($self,$path,$base) = @_;
  241. # Clean up $path
  242. if ( ! $self->file_name_is_absolute( $path ) ) {
  243. $path = $self->rel2abs( $path ) ;
  244. }
  245. else {
  246. $path = $self->canonpath( $path ) ;
  247. }
  248. # Figure out the effective $base and clean it up.
  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. # Now, remove all leading components that are the same
  259. my @pathchunks = $self->splitdir( $path);
  260. my @basechunks = $self->splitdir( $base);
  261. while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
  262. shift @pathchunks ;
  263. shift @basechunks ;
  264. }
  265. $path = CORE::join( '/', @pathchunks );
  266. $base = CORE::join( '/', @basechunks );
  267. # $base now contains the directories the resulting relative path
  268. # must ascend out of before it can descend to $path_directory. So,
  269. # replace all names with $parentDir
  270. $base =~ s|[^/]+|..|g ;
  271. # Glue the two together, using a separator if necessary, and preventing an
  272. # empty result.
  273. if ( $path ne '' && $base ne '' ) {
  274. $path = "$base/$path" ;
  275. } else {
  276. $path = "$base$path" ;
  277. }
  278. return $self->canonpath( $path ) ;
  279. }
  280. =item rel2abs
  281. Converts a relative path to an absolute path.
  282. $abs_path = File::Spec->rel2abs( $path ) ;
  283. $abs_path = File::Spec->rel2abs( $path, $base ) ;
  284. If $base is not present or '', then L<cwd()> is used. If $base is relative,
  285. then it is converted to absolute form using L</rel2abs()>. This means that it
  286. is taken to be relative to L<cwd()>.
  287. On systems with the concept of a volume, this assumes that both paths
  288. are on the $base volume, and ignores the $path volume.
  289. On systems that have a grammar that indicates filenames, this ignores the
  290. $base filename as well. Otherwise all path components are assumed to be
  291. directories.
  292. If $path is absolute, it is cleaned up and returned using L</canonpath()>.
  293. No checks against the filesystem are made on most systems. On MacOS,
  294. the filesystem may be consulted (see
  295. L<File::Spec::MacOS/file_name_is_absolute>). On VMS, there is
  296. interaction with the working environment, as logicals and
  297. macros are expanded.
  298. Based on code written by Shigio Yamaguchi.
  299. =cut
  300. sub rel2abs {
  301. my ($self,$path,$base ) = @_;
  302. # Clean up $path
  303. if ( ! $self->file_name_is_absolute( $path ) ) {
  304. # Figure out the effective $base and clean it up.
  305. if ( !defined( $base ) || $base eq '' ) {
  306. $base = cwd() ;
  307. }
  308. elsif ( ! $self->file_name_is_absolute( $base ) ) {
  309. $base = $self->rel2abs( $base ) ;
  310. }
  311. else {
  312. $base = $self->canonpath( $base ) ;
  313. }
  314. # Glom them together
  315. $path = $self->catdir( $base, $path ) ;
  316. }
  317. return $self->canonpath( $path ) ;
  318. }
  319. =back
  320. =head1 SEE ALSO
  321. L<File::Spec>
  322. =cut
  323. 1;