Fix problems with overlong directory names: phase #1
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixGroupInfo.cs
index 9df96e9012ae2bd71a3449f68b939624c78b62fb..50911f7d93536637f7dabc92b6a1ac74c7f47a60 100644 (file)
@@ -4,7 +4,7 @@
 // Authors:
 //   Jonathan Pryor (jonpryor@vt.edu)
 //
-// (C) 2004 Jonathan Pryor
+// (C) 2004-2005 Jonathan Pryor
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -27,6 +27,7 @@
 //
 
 using System;
+using System.Collections;
 using System.Text;
 using Mono.Unix;
 
@@ -34,29 +35,41 @@ namespace Mono.Unix {
 
        public sealed class UnixGroupInfo
        {
-               private Group group;
+               private Native.Group group;
 
                public UnixGroupInfo (string group)
                {
-                       this.group = new Group ();
-                       Group gr;
-                       int r = Syscall.getgrnam_r (group, this.group, out gr);
+                       this.group = new Native.Group ();
+                       Native.Group gr;
+                       int r = Native.Syscall.getgrnam_r (group, this.group, out gr);
                        if (r != 0 || gr == null)
                                throw new ArgumentException (Locale.GetText ("invalid group name"), "group");
                }
 
-               public UnixGroupInfo (uint group)
+               public UnixGroupInfo (long group)
                {
-                       this.group = new Group ();
-                       Group gr;
-                       int r = Syscall.getgrgid_r (group, this.group, out gr);
+                       this.group = new Native.Group ();
+                       Native.Group gr;
+                       int r = Native.Syscall.getgrgid_r (Convert.ToUInt32 (group), this.group, out gr);
                        if (r != 0 || gr == null)
                                throw new ArgumentException (Locale.GetText ("invalid group id"), "group");
                }
 
-               public UnixGroupInfo (Group group)
+               public UnixGroupInfo (Native.Group group)
                {
-                       this.group = group;
+                       this.group = CopyGroup (group);
+               }
+
+               private static Native.Group CopyGroup (Native.Group group)
+               {
+                       Native.Group g = new Native.Group ();
+
+                       g.gr_gid    = group.gr_gid;
+                       g.gr_mem    = group.gr_mem;
+                       g.gr_name   = group.gr_name;
+                       g.gr_passwd = group.gr_passwd;
+
+                       return g;
                }
 
                public string GroupName {
@@ -67,12 +80,21 @@ namespace Mono.Unix {
                        get {return group.gr_passwd;}
                }
 
-               public uint GroupId {
+               public long GroupId {
                        get {return group.gr_gid;}
                }
 
-               public string[] Members {
-                       get {return group.gr_mem;}
+               public UnixUserInfo[] GetMembers ()
+               {
+                       UnixUserInfo[] members = new UnixUserInfo [group.gr_mem.Length];
+                       for (int i = 0; i < members.Length; ++i)
+                               members [i] = new UnixUserInfo (group.gr_mem [i]);
+                       return members;
+               }
+
+               public string[] GetMemberNames ()
+               {
+                       return (string[]) group.gr_mem.Clone ();
                }
 
                public override int GetHashCode ()
@@ -92,9 +114,29 @@ namespace Mono.Unix {
                        return group.ToString();
                }
 
-               public Group ToGroup ()
+               public Native.Group ToGroup ()
+               {
+                       return CopyGroup (group);
+               }
+
+               public static UnixGroupInfo[] GetLocalGroups ()
                {
-                       return group;
+                       ArrayList entries = new ArrayList ();
+                       lock (Native.Syscall.grp_lock) {
+                               if (Native.Syscall.setgrent () != 0)
+                                       UnixMarshal.ThrowExceptionForLastError ();
+                               try {
+                                       Native.Group g;
+                                       while ((g = Native.Syscall.getgrent()) != null)
+                                               entries.Add (new UnixGroupInfo (g));
+                                       if (Native.Syscall.GetLastError() != (Native.Errno) 0)
+                                               UnixMarshal.ThrowExceptionForLastError ();
+                               }
+                               finally {
+                                       Native.Syscall.endgrent ();
+                               }
+                       }
+                       return (UnixGroupInfo[]) entries.ToArray (typeof(UnixGroupInfo));
                }
        }
 }