[Mono-list] [patch] a few fixes for System.IO.Directory
Eduardo Garcia Cebollero
kiwnix@yahoo.es
11 Dec 2002 02:59:37 +0100
--=-z8a9KVt3bORRrK4exadV
Content-Type: text/plain; charset=ISO-8859-15
Content-Transfer-Encoding: quoted-printable
El mi=E9, 11-12-2002 a las 02:32, Gonzalo Paniagua Javier escribi=F3:
> I think that you should first commit the fixes and then, may be, the
> line ending change.
>=20
> This way we all can see what has been fixed and how.
Fixed.
I changed the diff format to unified format too.
--=20
Eduardo Garcia Cebollero <kiwnix@yahoo.es>
--=-z8a9KVt3bORRrK4exadV
Content-Disposition: attachment; filename=1.diff
Content-Type: text/x-patch; name=1.diff; charset=ISO-8859-15
Content-Transfer-Encoding: quoted-printable
Index: ChangeLog
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/mcs/class/corlib/System.IO/ChangeLog,v
retrieving revision 1.115
diff -u -d -r1.115 ChangeLog
--- ChangeLog 8 Dec 2002 04:31:33 -0000 1.115
+++ ChangeLog 11 Dec 2002 01:58:01 -0000
@@ -1,3 +1,14 @@
+2002-12-11 Eduardo Garcia Cebollero <kiwnix@yahoo.es>
+
+ *Directory.cs: Some Exceptions added, fixed GetParent(),
+ CreateDirectory() should work with unix, native windows and
+ windows samba shares. Converted end-lines from dos-mode to unix-mode
+
+2002-12-08 Eduardo Garcia Cebollero <kiwnix@yahoo.es>
+
+ * Directory.cs: CreateDirectory works now with Absolute paths
+ too, not only with relative ones.
+
2002-12-07 Peter Williams <peterw@ximian.com>
=20
* Directory.cs: Don't use the uninitialized pathsumm here.
Index: Directory.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/mcs/class/corlib/System.IO/Directory.cs,v
retrieving revision 1.19
diff -u -d -r1.19 Directory.cs
--- Directory.cs 8 Dec 2002 04:31:33 -0000 1.19
+++ Directory.cs 11 Dec 2002 01:58:02 -0000
@@ -4,7 +4,8 @@
// Authors:
// Jim Richardson (develop@wtfo-guru.com)
// Miguel de Icaza (miguel@ximian.com)
-// Dan Lewis (dihlewis@yahoo.co.uk)
+// Dan Lewis (dihlewis@yahoo.co.uk)
+// Eduardo Garcia (kiwnix@yahoo.es)
//
// Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
// Copyright (C) 2002 Ximian, Inc.
@@ -18,15 +19,14 @@
using System.Collections;
using System.Text;
=20
-namespace System.IO
-{
+namespace System.IO
+{
public sealed class Directory : Object
- {
+ {
private Directory () {}
-
+ =09
public static DirectoryInfo CreateDirectory (string path)
{
- StringBuilder pathsumm =3D new StringBuilder();
DirectoryInfo tmpinfo =3D null;
=09
if (path =3D=3D null) {
@@ -40,27 +40,83 @@
if (path.IndexOfAny (Path.InvalidPathChars) !=3D -1) {
throw new ArgumentException ("Path contains invalid chars");
}
-
+ =09
string[] pathcomponents =3D path.Split(new char[] { Path.DirectorySepar=
atorChar });
-
=09
- if (pathcomponents.Length =3D=3D 1) {
+ =09
+ if (pathcomponents.Length =3D=3D 1){
tmpinfo =3D Directory.RealCreateDirectory(path);
}=20
else {
- foreach(string dir in pathcomponents)
+ if ((path[0]=3D=3D Path.DirectorySeparatorChar) ||
+ ((path[1] =3D=3D ':') && (path[2] =3D=3D Path.DirectorySeparatorCh=
ar))) //Absolute Path =09
{
- if (dir.Length !=3D 0) {
- if (pathsumm.Length =3D=3D 0) {
- pathsumm.Append(dir);
- }=20
- else {
- pathsumm.Append(Path.DirectorySeparatorChar + dir);
+ //Should Work in Unix, Win* native Directoryes and Samba Shares
+ //FIXME: This is not thread safe
+ =20
+ =09
+ string actual_path =3D Directory.GetCurrentDirectory();
+ =09
+ if (Environment.OSVersion.Platform =3D=3D PlatformID.Unix) //Is Unix
+ {
+ StringBuilder pathsumm =3D new StringBuilder(path);
+ Directory.SetCurrentDirectory("/");
+ pathsumm.Remove(0,1);
+ tmpinfo =3D Directory.CreateDirectory(pathsumm.ToString());
+ }
+ else //We asume is Win*
+ {
+ if ((path[1] =3D=3D ':') || (path[0] =3D=3D Path.DirectorySeparatorC=
har)) //Is a regular path
+ {
+ StringBuilder pathsumm =3D new StringBuilder(path);
+ Directory.SetCurrentDirectory(path.Substring(0,2));
+ pathsumm.Remove(0,2);
+ tmpinfo =3D Directory.CreateDirectory(pathsumm.ToString()); =09
+ } =09
+ else if((path[0] =3D=3D '\\') && (path[1] =3D=3D '\\')) //Is a Samba=
Share
+ {
+ if (Directory.Exists(pathcomponents[0] + "\\"
+ + pathcomponents[1] + "\\"
+ + pathcomponents[2]))
+ {
+ StringBuilder pathsumm =3D new StringBuilder();=09
+ Directory.SetCurrentDirectory(pathcomponents[0] +=20
+ "\\" + pathcomponents[1] +
+ "\\" + pathcomponents[2]);
+ pathcomponents[0] =3D ""; pathcomponents[1] =3D ""; pathcomponents=
[2] =3D "";
+ foreach(string dir in pathsumm.ToString())
+ {
+ pathsumm.Append(dir + "\\");
+ } =09
+ Directory.CreateDirectory(pathsumm.ToString());
+ }
+ else
+ {
+ throw new DirectoryNotFoundException("The samba share do not Exist=
s");
+ }
}
+ =09
}
+ Directory.SetCurrentDirectory(actual_path);=09
+ }
+ else //Relative Path
+ {
+ StringBuilder pathsumm =3D new StringBuilder();
=09
- if (pathsumm.Length !=3D 0 && !Directory.Exists (pathsumm.ToString())=
) {
- tmpinfo =3D Directory.RealCreateDirectory (pathsumm.ToString());
+ foreach(string dir in pathcomponents)
+ {
+ if (dir.Length !=3D 0) {
+ if (pathsumm.Length =3D=3D 0) {
+ pathsumm.Append (dir);
+ }=20
+ else {
+ pathsumm.Append (Path.DirectorySeparatorChar + dir);
+ }
+ =09
+ if (!Directory.Exists (pathsumm.ToString())) {
+ tmpinfo =3D Directory.RealCreateDirectory (pathsumm.ToString());
+ }
+ }
}
}
}
@@ -111,9 +167,11 @@
}
=09
public static void Delete (string path, bool recurse)
- {
+ {
if (path =3D=3D null)
- throw new ArgumentNullException ();
+ throw new ArgumentNullException ();
+ if (path =3D=3D "")
+ throw new System.ArgumentException("Path is Empty");
if (path.IndexOfAny (Path.InvalidPathChars) !=3D -1)
throw new ArgumentException ("Path contains invalid characters");
=20
@@ -128,7 +186,6 @@
public static bool Exists (string path)
{
MonoIOError error;
- =09
return MonoIO.ExistsDirectory (path, out error);
}
=20
@@ -169,10 +226,10 @@
return GetFileSystemEntries (path, pattern, FileAttributes.Directory, F=
ileAttributes.Directory);
}
=09
- public static string GetDirectoryRoot (string path)
- {
- return "" + Path.DirectorySeparatorChar;
- }
+ public static string GetDirectoryRoot (string path)
+ {
+ return new String(Path.DirectorySeparatorChar,1);
+ }
=09
public static string [] GetFiles (string path)
{
@@ -185,29 +242,44 @@
}
=20
public static string [] GetFileSystemEntries (string path)
- {=09
- return GetFileSystemEntries (path, "*");
- }
+ {
+ return GetFileSystemEntries (path, "*");
+ }
=20
public static string [] GetFileSystemEntries (string path, string patter=
n)
- {
- return GetFileSystemEntries (path, pattern, 0, 0);
- }
+ {
+ if (path =3D=3D null)
+ throw new ArgumentNullException ();
+ if (path.IndexOfAny (Path.InvalidPathChars) !=3D -1)
+ throw new ArgumentException ("Path contains invalid characters");
+ if (path =3D=3D "")
+ throw new ArgumentException ("The Path do not have a valid format");
+
+ return GetFileSystemEntries (path, pattern, 0, 0);
+ }
=09
public static string[] GetLogicalDrives ()
- {=09
- return new string [] { "A:\\", "C:\\" };
- }
+ {=20
+ //FIXME: Hardcoded Paths
+ return new string [] { "A:\\", "C:\\" };
+ }
=20
- public static DirectoryInfo GetParent (string path)
- {
- return new DirectoryInfo (Path.GetDirectoryName (path));
- }
+ public static DirectoryInfo GetParent (string path)
+ {
+ if (path =3D=3D null)
+ throw new ArgumentNullException ();
+ if (path.IndexOfAny (Path.InvalidPathChars) !=3D -1)
+ throw new ArgumentException ("Path contains invalid characters");
+ if (path =3D=3D "")
+ throw new ArgumentException ("The Path do not have a valid format");
+ =09
+ return new DirectoryInfo (Path.GetDirectoryName (path + Path.DirectoryS=
eparatorChar + ".."));
+ }
=20
- public static void Move (string src, string dest)
- {
- File.Move (src, dest);
- }
+ public static void Move (string src, string dest)
+ {
+ File.Move (src, dest);
+ }
=20
public static void SetCreationTime (string path, DateTime creation_time)
{
--=-z8a9KVt3bORRrK4exadV--