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.

268 lines
8.2 KiB

  1. package URI::file;
  2. use strict;
  3. use vars qw(@ISA);
  4. require URI::_generic;
  5. @ISA = qw(URI::_generic);
  6. # Map from $^O values to implementation classes. The Unix
  7. # class is the default.
  8. my %os_class = (
  9. os2 => "OS2",
  10. mac => "Mac",
  11. MacOS => "Mac",
  12. MSWin32 => "Win32",
  13. win32 => "Win32",
  14. msdos => "FAT",
  15. dos => "FAT",
  16. qnx => "QNX",
  17. );
  18. sub os_class
  19. {
  20. my($OS) = shift || $^O;
  21. my $class = "URI::file::" . ($os_class{$OS} || "Unix");
  22. no strict 'refs';
  23. unless (%{"$class\::"}) {
  24. eval "require $class";
  25. die $@ if $@;
  26. }
  27. $class;
  28. }
  29. sub path { shift->path_query(@_) }
  30. sub host { shift->authority(@_) }
  31. sub new
  32. {
  33. my($class, $path, $os) = @_;
  34. os_class($os)->new($path);
  35. }
  36. sub new_abs
  37. {
  38. my $class = shift;
  39. my $file = $class->new(shift);
  40. return $file->abs($class->cwd) unless $$file =~ /^file:/;
  41. $file;
  42. }
  43. sub cwd
  44. {
  45. my $class = shift;
  46. require Cwd;
  47. my $cwd = Cwd::cwd();
  48. $cwd = VMS::Filespec::unixpath($cwd) if $^O eq 'VMS';
  49. $cwd = $class->new($cwd);
  50. $cwd .= "/" unless substr($cwd, -1, 1) eq "/";
  51. $cwd;
  52. }
  53. sub file
  54. {
  55. my($self, $os) = @_;
  56. os_class($os)->file($self);
  57. }
  58. sub dir
  59. {
  60. my($self, $os) = @_;
  61. os_class($os)->dir($self);
  62. }
  63. 1;
  64. __END__
  65. =head1 NAME
  66. URI::file - URI that map to local file names
  67. =head1 SYNOPSIS
  68. use URI::file;
  69. $u1 = URI->new("file:/foo/bar");
  70. $u2 = URI->new("foo/bar", "file");
  71. $u3 = URI::file->new($path);
  72. $u4 = URI::file->new("c:\\windows\\", "win32");
  73. $u1->file;
  74. $u1->file("mac");
  75. =head1 DESCRIPTION
  76. The C<URI::file> class supports C<URI> objects belonging to the I<file>
  77. URI scheme. This scheme allows us to map the conventional file names
  78. found on various computer systems to the URI name space. An old
  79. specification of the I<file> URI scheme is found in RFC 1738. Some
  80. older background information is also in RFC 1630. There are no newer
  81. specifications as far as I know.
  82. If you want simply to construct I<file> URI objects from URI strings,
  83. use the normal C<URI> constructor. If you want to construct I<file>
  84. URI objects from the actual file names used by various systems, then
  85. use one of the following C<URI::file> constructors:
  86. =over 4
  87. =item $u = URI::file->new( $filename, [$os] )
  88. Maps a file name to the I<file:> URI name space, creates an URI object
  89. and returns it. The $filename is interpreted as one belonging to the
  90. indicated operating system ($os), which defaults to the value of the
  91. $^O variable. The $filename can be either absolute or relative, and
  92. the corresponding type of URI object for $os is returned.
  93. =item $u = URI::file->new_abs( $filename, [$os] )
  94. Same as URI::file->new, but will make sure that the URI returned
  95. represents an absolute file name. If the $filename argument is
  96. relative, then the name is resolved relative to the current directory,
  97. i.e. this constructor is really the same as:
  98. URI::file->new($filename)->abs(URI::file->cwd);
  99. =item $u = URI::file->cwd
  100. Returns a I<file> URI that represents the current working directory.
  101. See L<Cwd>.
  102. =back
  103. The following methods are supported for I<file> URI (in addition to
  104. the common and generic methods described in L<URI>):
  105. =over 4
  106. =item $u->file( [$os] )
  107. This method return a file name. It maps from the URI name space
  108. to the file name space of the indicated operating system.
  109. It might return C<undef> if the name can not be represented in the
  110. indicated file system.
  111. =item $u->dir( [$os] )
  112. Some systems use a different form for names of directories than for plain
  113. files. Use this method if you know you want to use the name for
  114. a directory.
  115. =back
  116. The C<URI::file> module can be used to map generic file names to names
  117. suitable for the current system. As such, it can work as a nice
  118. replacement for the C<File::Spec> module. For instance the following
  119. code will translate the Unix style file name F<Foo/Bar.pm> to a name
  120. suitable for the local system.
  121. $file = URI::file->new("Foo/Bar.pm", "unix")->file;
  122. die "Can't map filename Foo/Bar.pm for $^O" unless defined $file;
  123. open(FILE, $file) || die "Can't open '$file': $!";
  124. # do something with FILE
  125. =head1 MAPPING NOTES
  126. Most computer systems today have hierarchically organized file systems.
  127. Mapping the names used in these systems to the generic URI syntax
  128. allows us to work with relative file URIs that behave as they should
  129. when resolved using the generic algorithm for URIs (specified in RFC
  130. 2396). Mapping a file name to the generic URI syntax involves mapping
  131. the path separator character to "/" and encoding of any reserved
  132. characters that appear in the path segments of the file names. If
  133. path segments consisting of the strings "." or ".." have a
  134. different meaning than what is specified for generic URIs, then these
  135. must be encoded as well.
  136. If the file system has device, volume or drive specifications as
  137. the root of the name space, then it makes sense to map them to the
  138. authority field of the generic URI syntax. This makes sure that
  139. relative URI can not be resolved "above" them , i.e. generally how
  140. relative file names work in those systems.
  141. Another common use of the authority field is to encode the host that
  142. this file name is valid on. The host name "localhost" is special and
  143. generally have the same meaning as an missing or empty authority
  144. field. This use will be in conflict with using it as a device
  145. specification, but can often be resolved for device specifications
  146. having characters not legal in plain host names.
  147. File name to URI mapping in normally not one-to-one. There are
  148. usually many URI that map to the same file name. For instance an
  149. authority of "localhost" maps the same as a URI with a missing or empty
  150. authority.
  151. Example 1: The Mac use ":" as path separator, but not in the same way
  152. as generic URI. ":foo" is a relative name. "foo:bar" is an absolute
  153. name. Also path segments can contain the "/" character as well as be
  154. literal "." or "..". It means that we will map like this:
  155. Mac URI
  156. ---------- -------------------
  157. :foo:bar <==> foo/bar
  158. : <==> ./
  159. ::foo:bar <==> ../foo/bar
  160. ::: <==> ../../
  161. foo:bar <==> file:/foo/bar
  162. foo:bar: <==> file:/foo/bar/
  163. .. <==> %2E%2E
  164. <undef> <== /
  165. foo/ <== file:/foo%2F
  166. ./foo.txt <== file:/.%2Ffoo.txt
  167. Note that if you want a relative URL, you *must* begin the path with a :. Any
  168. path that begins with [^:] will be treated as absolute.
  169. Example 2: The Unix file system is easy to map as it use the same path
  170. separator as URIs, have a single root, and segments of "." and ".."
  171. have the same meaning. URIs that have the character "\0" or "/" as
  172. part of any path segment can not be turned into valid Unix file names.
  173. Unix URI
  174. ---------- ------------------
  175. foo/bar <==> foo/bar
  176. /foo/bar <==> file:/foo/bar
  177. /foo/bar <== file://localhost/foo/bar
  178. file: ==> ./file:
  179. <undef> <== file:/fo%00/bar
  180. / <==> file:/
  181. =cut
  182. RFC 1630
  183. [...]
  184. There is clearly a danger of confusion that a link made to a local
  185. file should be followed by someone on a different system, with
  186. unexpected and possibly harmful results. Therefore, the convention
  187. is that even a "file" URL is provided with a host part. This allows
  188. a client on another system to know that it cannot access the file
  189. system, or perhaps to use some other local mechanism to access the
  190. file.
  191. The special value "localhost" is used in the host field to indicate
  192. that the filename should really be used on whatever host one is.
  193. This for example allows links to be made to files which are
  194. distribted on many machines, or to "your unix local password file"
  195. subject of course to consistency across the users of the data.
  196. A void host field is equivalent to "localhost".
  197. =head1 SEE ALSO
  198. L<URI>, L<File::Spec>, L<perlport>
  199. =head1 COPYRIGHT
  200. Copyright 1995-1998 Gisle Aas.
  201. This library is free software; you can redistribute it and/or
  202. modify it under the same terms as Perl itself.
  203. =cut