commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1165370 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ native/ native/os/unix/ native/shared/ test/org/apache/commons/runtime/
Date Mon, 05 Sep 2011 17:41:03 GMT
Author: mturk
Date: Mon Sep  5 17:41:02 2011
New Revision: 1165370

URL: http://svn.apache.org/viewvc?rev=1165370&view=rev
Log:
Add posix Group implementation

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java   (with
props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIterator.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIteratorImpl.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
    commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c
    commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java?rev=1165370&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java Mon
Sep  5 17:41:02 2011
@@ -0,0 +1,264 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+
+/**
+ * Group Information.
+ */
+public final class Group
+{
+
+    private byte[]  bb;
+    private static final int BSIZE;
+    // Serialize access to group database
+    private static Object lock;
+    static {
+        BSIZE = init0();
+        lock  = new Object();
+    }
+
+    private Group()
+    {
+        bb = null;
+        Id = 0L;
+    }
+
+    private Group(long id, byte[] b)
+    {
+        bb = b;
+        Id = id;
+    }
+
+    private static native int     init0();
+    private static native Group   get0(String name, byte[] buff)
+        throws SystemException, SecurityException;
+    private static native Group   get1(byte[] buff)
+        throws SystemException, SecurityException;
+    private static native Group   get2(byte[] buff)
+        throws SystemException, SecurityException;
+    private static native boolean equals0(long a, long b);
+    private static native void    enum0(ArrayList<String> gset)
+        throws SystemException, SecurityException;
+    private static native void    enum1(ArrayList<String> gset)
+        throws SystemException, SecurityException;
+    private static native void    enum2(ArrayList<String> uset, long gid)
+        throws SystemException, SecurityException;
+
+    /**
+     * Create the {@code Group} object from the {@code name}.
+     *
+     * @return {@code Group} obect.
+     * @throws SecurityException if access to an internal group database
+     *         is forbidden.
+     * @throws OperatingSystemException in case of error.
+     * @throws NoSuchObjectException if the group {@code name} doesn't exist.
+     */
+    public static Group get(String name)
+        throws SystemException, SecurityException, NoSuchObjectException
+    {
+        byte[] b = new byte[BSIZE];
+        Group g = get0(name, b);
+        if (g == null)
+            throw new NoSuchObjectException(Local.sm.get("group.ENOTFOUND", name));
+        return g;
+    }
+
+    /**
+     * Get the current users primary {@code Group}.
+     *
+     * @return Current users primary {@code Group} obect.
+     * @throws SecurityException if access to an internal group database
+     *         is forbidden.
+     * @throws OperatingSystemException in case of error.
+     * @throws NoSuchObjectException if the primary group doesn't exist.
+     */
+    public static Group get()
+        throws SystemException, SecurityException, NoSuchObjectException
+    {
+        byte[] b = new byte[BSIZE];
+        Group g = get1(b);
+        if (g == null)
+            throw new NoSuchObjectException(Local.sm.get("group.ENOPRIMARY"));
+        return g;
+    }
+
+    /**
+     * Get the current users effective {@code Group}.
+     *
+     * @return Current users effective {@code Group} obect.
+     * @throws SecurityException if access to an internal group database
+     *         is forbidden.
+     * @throws OperatingSystemException in case of error.
+     * @throws NoSuchObjectException if the effective group doesn't exist.
+     */
+    public static Group getEffective()
+        throws SystemException, SecurityException, NoSuchObjectException
+    {
+        byte[] b = new byte[BSIZE];
+        Group g = get2(b);
+        if (g == null)
+            throw new NoSuchObjectException(Local.sm.get("group.ENOEFF"));
+        return g;
+    }
+
+    /**
+     * Get the {@link GroupIterator} of all {@code local} groups defined
+     * on this system.
+     *
+     * @return GroupIterator containing all {@code local} groups.
+     * @throws OperatingSystemException in case of error.
+     * @throws SecurityException if the current user is not allowed to
+     *         access the system group database.
+     */
+    public static GroupIterator getLocalGroups()
+        throws SystemException, SecurityException
+    {
+        GroupIterator iter;
+        synchronized(lock) {
+            ArrayList<String> groups = new ArrayList<String>();
+            enum0(groups);
+            iter = new GroupIteratorImpl(groups.iterator());
+        }
+        return iter;
+    }
+
+    /**
+     * Get the {@link GroupIterator} of all {@code local} groups defined
+     * on this system.
+     *
+     * @return GroupIterator containing all {@code local} groups.
+     * @throws OperatingSystemException in case of error.
+     * @throws SecurityException if the current user is not allowed to
+     *         access the system group database.
+     * @throws OperationNotImplementedException if the operating system does not
+     *         support the global groups concept.
+     */
+    public static GroupIterator getGlobalGroups()
+        throws SystemException, SecurityException, OperationNotImplementedException
+    {
+        GroupIterator iter;
+        synchronized(lock) {
+            ArrayList<String> groups = new ArrayList<String>();
+            enum1(groups);
+            iter = new GroupIteratorImpl(groups.iterator());
+        }
+        return iter;
+    }
+
+    /**
+     * Get the {@link UserIterator} of all users defined that are member
+     * of {@code this} group.
+     *
+     * @return UserIterator containing all user members.
+     * @throws OperatingSystemException in case of error.
+     * @throws SecurityException if the current user is not allowed to
+     *         access the system group database.
+     */
+    public UserIterator getMembers()
+        throws SystemException, SecurityException
+    {
+        UserIterator iter;
+        synchronized(lock) {
+            ArrayList<String> users = new ArrayList<String>();
+            enum2(users, Id);
+            iter = new UserIteratorImpl(users.iterator());
+        }
+        return iter;
+    }
+
+    /**
+     * String that specifies the name of the group account.
+     */
+    public  String       getName()
+    {
+        return Name;
+    }
+    private String       Name;
+
+    /**
+     * String that contains a comment associated with the group. This string can
+     * be a null string.
+     */
+    public  String       getComment()
+    {
+        return Comment;
+    }
+    private String       Comment;
+
+    /**
+     * Returns a string representation of the Group.
+     */
+    public  String       getGroupId()
+    {
+        if (GroupId == null)
+            GroupId = Long.toString(Id);
+        return GroupId;
+    }
+    private String       GroupId;
+
+    /**
+     * Set to true if the group is Local system group.
+     */
+    public  boolean      IsLocal()
+    {
+        return isLocal;
+    }
+    private boolean      isLocal;
+
+    /**
+     * Specifies the <em>gid_t</em> or <em>PSID</em>
+     * {@code Descriptor} identifier of the group.
+     */
+    public final long    Id;
+
+    /**
+     * Compares {@code this} Group to the specified object.
+     *
+     * @param other a {@code Group}
+     * @return  true if the class of this {@code Group} object and the
+     *      class of {@code other} are exactly equal, and they point
+     *      to the same system group id.
+     */
+    @Override
+    public boolean equals(Object other)
+    {
+        if (other == null)
+            return false;
+        if (other == this)
+            return true;
+        if (Group.class != other.getClass())
+            return false;
+        return equals0(Id, ((Group)other).Id);
+    }
+
+    /**
+     * Returns a string representation of the Group.
+     * The returned string is native representation of the underlying
+     * user descriptor.
+     * @return a string representation of the group id.
+     */
+    @Override
+    public String toString()
+    {
+        return getGroupId();
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIterator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIterator.java?rev=1165370&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIterator.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIterator.java
Mon Sep  5 17:41:02 2011
@@ -0,0 +1,72 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * Group Iterator
+ *
+ * @since Runtime 1.0
+ */
+public abstract class GroupIterator
+    implements Iterator<Group>, Iterable<Group>
+{
+
+    /**
+     * Returns {@code true} if the iteration has more elements.
+     * It returns {@code true} if the {@code next} would return
+     * an group rather then throwing and exception.
+     *
+     * @return {@code true} if the iteration has more elements.
+     */
+    public abstract boolean hasNext();
+
+    /**
+     * Returns an iterator over a set of {@link Group} elements.
+     *
+     * return Iterator of type {@code Group}.
+     */
+    public Iterator<Group> iterator()
+    {
+         return this;
+    }
+
+    /**
+     * Returns the next {@link Group} in the iteration.
+     *
+     * @return the next {@code Group} in the iteration.
+     */
+    public abstract Group next()
+        throws NoSuchElementException;
+
+    /**
+     * Removes from the underlying collection the last element returned
+     * by the iterator. This method is unsupported.
+     *
+     * @throws UnsupportedOperationException is always thrown.
+     * @throws IllegalStateException is the {@code next} method has not yet
+     *         been called.
+     */
+    public void remove()
+        throws UnsupportedOperationException, IllegalStateException
+    {
+        throw new UnsupportedOperationException();
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIteratorImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIteratorImpl.java?rev=1165370&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIteratorImpl.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIteratorImpl.java
Mon Sep  5 17:41:02 2011
@@ -0,0 +1,50 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import java.util.NoSuchElementException;
+import java.util.Iterator;
+
+/**
+ * Group Iterator implementation.
+ *
+ * Package private
+ */
+class GroupIteratorImpl extends GroupIterator
+{
+
+    private Iterator<String> groups;
+
+    protected GroupIteratorImpl(Iterator<String> groups)
+    {
+        this.groups = groups;
+    }
+
+    public boolean hasNext()
+    {
+        return groups.hasNext();
+    }
+
+    public Group next()
+        throws NoSuchElementException
+    {
+        if (groups.hasNext())
+            return Group.get(groups.next());
+        else
+            throw new NoSuchElementException();
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GroupIteratorImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties?rev=1165370&r1=1165369&r2=1165370&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
Mon Sep  5 17:41:02 2011
@@ -23,3 +23,5 @@ execmem.ENOTIMPL=Apache Commons Runtime 
 user.ENOTFOUND=User '{0}' not found
 user.ENOCURRENT=Current user not found
 group.ENOTFOUND=Group '{0}' not found
+group.ENOPRIMARY=Primary group not found
+group.ENOEFF=Effective group not found

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java?rev=1165370&r1=1165369&r2=1165370&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java Mon Sep
 5 17:41:02 2011
@@ -214,9 +214,8 @@ public final class User
     public  String       getUserId()
     {
         if (UserId == null)
-            return Long.toString(Id);
-        else
-            return UserId;
+            UserId = Long.toString(Id);
+        return UserId;
     }
     private String       UserId;
 
@@ -224,7 +223,7 @@ public final class User
      * Specifies the <em>uid_t</em> or <em>PSID</em>
      * {@code Descriptor} identifier of the user.
      */
-    public final long Id;
+    public final long    Id;
 
     /**
      * Compares {@code this} User to the specified object.
@@ -255,7 +254,7 @@ public final class User
     @Override
     public String toString()
     {
-        return UserId;
+        return getUserId();
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java?rev=1165370&r1=1165369&r2=1165370&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/UserIteratorImpl.java
Mon Sep  5 17:41:02 2011
@@ -20,16 +20,14 @@ import java.util.NoSuchElementException;
 import java.util.Iterator;
 
 /**
- * User Iterator implementation
+ * User Iterator implementation.
  *
  * Package private
- *
  */
 class UserIteratorImpl extends UserIterator
 {
 
     private Iterator<String> users;
-    private int pos = 0;
 
     protected UserIteratorImpl(Iterator<String> users)
     {

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1165370&r1=1165369&r2=1165370&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Mon Sep  5 17:41:02 2011
@@ -64,6 +64,7 @@ UNIX_SOURCES=\
 	$(TOPDIR)/os/unix/dso.c \
 	$(TOPDIR)/os/unix/exec.c \
 	$(TOPDIR)/os/unix/execmem.c \
+	$(TOPDIR)/os/unix/group.c \
 	$(TOPDIR)/os/unix/inetsock.c \
 	$(TOPDIR)/os/unix/init.c \
 	$(TOPDIR)/os/unix/localsock.c \

Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c?rev=1165370&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c Mon Sep  5 17:41:02 2011
@@ -0,0 +1,328 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/string.h"
+#include "acr/clazz.h"
+#include "acr/jniapi.h"
+#include "acr/port.h"
+#include "acr/misc.h"
+#include "acr/users.h"
+#include "arch_opts.h"
+
+#include <pwd.h>
+#include <grp.h>
+#include <utmpx.h>
+
+J_DECLARE_CLAZZ = {
+    INVALID_FIELD_OFFSET,
+    0,
+    0,
+    0,
+    ACR_CLASS_PATH "Group"
+};
+
+J_DECLARE_M_ID(0000) = {
+    0,
+    "<init>",
+    "(J[B)V"
+};
+
+J_DECLARE_F_ID(0000) = {
+    INVALID_FIELD_OFFSET,
+    INVALID_FIELD_OFFSET,
+    0,
+    "Name",
+    "Ljava/lang/String;"
+};
+
+J_DECLARE_F_ID(0001) = {
+    INVALID_FIELD_OFFSET,
+    INVALID_FIELD_OFFSET,
+    0,
+    "Comment",
+    "Ljava/lang/String;"
+};
+
+J_DECLARE_F_ID(0002) = {
+    INVALID_FIELD_OFFSET,
+    INVALID_FIELD_OFFSET,
+    0,
+    "GroupId",
+    "Ljava/lang/String;"
+};
+
+J_DECLARE_F_ID(0003) = {
+    INVALID_FIELD_OFFSET,
+    INVALID_FIELD_OFFSET,
+    0,
+    "isLocal",
+    "Z"
+};
+
+ACR_CLASS_CTOR(Group)
+{
+    if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+        return JNI_FALSE;
+    J_LOAD_METHOD(0000);
+    J_LOAD_IFIELD(0000);
+    J_LOAD_IFIELD(0001);
+    J_LOAD_IFIELD(0002);
+    J_LOAD_IFIELD(0003);
+
+    _clazzn.u = 1;
+    return JNI_TRUE;
+}
+
+ACR_CLASS_DTOR(Group)
+{
+    AcrUnloadClass(env, &_clazzn);
+}
+
+jobjectArray AcrNewGroupArray(JNI_STDENV, jsize len)
+{
+    if (_clazzn.i)
+        return (*env)->NewObjectArray(env, len, _clazzn.i, NULL);
+    else
+        return 0;
+}
+
+#define GRBUF_SIZE 8192
+static int getgrnam_s(const char *grpname,
+                      struct group *gr,
+                      char *grbuf)
+{
+    struct group *grptr;
+#if HAVE_GETGRNAM_R
+    int rc;
+
+    /* POSIX defines getpwnam_r() et al to return the error number
+     * rather than set errno, and requires pwptr to be set to NULL if
+     * the entry is not found, imply that "not found" is not an error
+     * condition; some implementations do return 0 with pwptr set to
+     * NULL.
+    */
+    rc = getgrnam_r(grpname, gr, grbuf, GRBUF_SIZE, &grptr);
+    if (rc != 0)
+        return rc;
+    if (grptr == 0)
+        return ACR_ENOENT;
+#else
+    /* Some platforms (e.g. FreeBSD 4.x) do not set errno on NULL "not
+     * found" return values for the non-threadsafe function either.
+    */
+    errno = 0;
+    if ((grptr = getgrnam(grpname)) != 0)
+        memcpy(gr, grptr, sizeof *gr);
+    else
+        return errno ? errno : ACR_ENOENT;
+#endif
+    return 0;
+}
+
+static int getgrgid_s(gid_t grpid,
+                      struct group *gr,
+                      char *grbuf)
+{
+    struct group *grptr;
+#if HAVE_GETGRGID_R
+    int rc;
+
+    /* POSIX defines getpwnam_r() et al to return the error number
+     * rather than set errno, and requires pwptr to be set to NULL if
+     * the entry is not found, imply that "not found" is not an error
+     * condition; some implementations do return 0 with pwptr set to
+     * NULL.
+    */
+    rc = getgrgid_r(grpid, gr, grbuf, GRBUF_SIZE, &grptr);
+    if (rc != 0)
+        return rc;
+    if (grptr == 0)
+        return ACR_ENOENT;
+#else
+    /* Some platforms (e.g. FreeBSD 4.x) do not set errno on NULL "not
+     * found" return values for the non-threadsafe function either.
+    */
+    errno = 0;
+    if ((grptr = getgrgid(grpid)) != 0)
+        memcpy(gr, grptr, sizeof *gr);
+    else
+        return errno ? errno : ACR_ENOENT;
+#endif
+    return 0;
+}
+
+static int getgrent_s(struct group *gr,
+                      char *grbuf)
+{
+    struct group *grptr;
+#if HAVE_GETGRENT_R
+# if defined(_SOLARIS)
+    errno = 0;
+    if ((grptr = getgrent_r(gr, grbuf, GRBUF_SIZE)) == 0)
+        return errno ? errno : ACR_ENOENT;
+# else
+    int rc;
+
+    /* POSIX defines getpwnam_r() et al to return the error number
+     * rather than set errno, and requires pwptr to be set to NULL if
+     * the entry is not found, imply that "not found" is not an error
+     * condition; some implementations do return 0 with pwptr set to
+     * NULL.
+    */
+    rc = getgrent_r(gr, grbuf, GRBUF_SIZE, &grptr);
+    if (rc != 0)
+        return rc;
+    if (grptr == 0)
+        return ACR_ENOENT;
+# endif
+#else
+    /* Some platforms (e.g. FreeBSD 4.x) do not set errno on NULL "not
+     * found" return values for the non-threadsafe function either.
+    */
+    errno = 0;
+    if ((grptr = getgrent()) != 0)
+        memcpy(gr, grptr, sizeof *gr);
+    else
+        return errno ? errno : ACR_ENOENT;
+#endif
+    return 0;
+}
+
+static jobject
+group_byname(JNI_STDARGS, const char *name)
+{
+    jlong   gid;
+    jobject grp;
+    int rc;
+    struct group gr;
+    char buffer[GRBUF_SIZE];
+
+    rc = getgrnam_s(name, &gr, buffer);
+    if (rc != 0) {
+        ACR_THROW_SYS_ERROR(rc);
+        return 0;
+    }
+    if (obj == 0)
+        obj = (*env)->NewByteArray(env, 4);
+    gid = gr.gr_gid;
+    grp = (*env)->NewObject(env, _clazzn.i, J4MID(0000), gid, obj);
+    if (grp == 0)
+        return 0;
+
+    SET_IFIELD_S(0000, grp, gr.gr_name);
+    SET_IFIELD_Z(0003, grp, JNI_TRUE);
+    return grp;
+}
+
+static jobject
+group_fromgid(JNI_STDARGS, jlong id)
+{
+    gid_t   gid = (gid_t)id;
+    jobject grp;
+    int rc;
+    struct group gr;
+    char buffer[GRBUF_SIZE];
+    
+    rc = getgrgid_s(gid, &gr, buffer);
+    if (rc != 0) {
+        ACR_THROW_SYS_ERROR(rc);
+        return 0;
+    }
+    if (obj == 0)
+        obj = (*env)->NewByteArray(env, 4); 
+    grp = (*env)->NewObject(env, _clazzn.i, J4MID(0000), id, obj);
+    if (grp == 0)
+        return 0;
+
+    SET_IFIELD_S(0000, grp, gr.gr_name);
+    SET_IFIELD_Z(0003, grp, JNI_TRUE);
+    return grp;
+}
+
+ACR_JNI_EXPORT(int, Group, init0)(JNI_STDARGS)
+{
+    return 4;
+}
+
+ACR_JNI_EXPORT(jobject, Group, get0)(JNI_STDARGS, jstring name,
+                                    jbyteArray buf)
+{
+    jobject grp = 0;
+    WITH_CSTR(name) {
+        grp = group_byname(env, buf, J2S(name));
+    } DONE_WITH_STR(name);
+    return grp;
+}
+
+ACR_JNI_EXPORT(jobject, Group, get1)(JNI_STDARGS, jbyteArray buf)
+{
+    return group_fromgid(env, buf, getgid());
+}
+
+ACR_JNI_EXPORT(jobject, Group, get2)(JNI_STDARGS, jbyteArray buf)
+{
+    return group_fromgid(env, buf, getegid());
+}
+
+ACR_JNI_EXPORT(void, Group, enum0)(JNI_STDARGS, jobject ua)
+{
+    struct group gr;
+    char grbuf[GRBUF_SIZE];
+
+    setgrent();
+    for (;;) {
+        jstring s;
+        if (getgrent_s(&gr, grbuf) != 0)
+            break;
+        s = AcrNewJavaStringA(env, gr.gr_name);
+        if (s == 0)
+            break;
+        AcrArrayListAdd(env, ua, s);
+        if ((*env)->ExceptionCheck(env))
+            break;
+        (*env)->DeleteLocalRef(env, s);
+    }
+    endgrent();
+}
+
+ACR_JNI_EXPORT(void, Group, enum1)(JNI_STDARGS, jobject ua)
+{
+    ACR_THROW_SYS_ERROR(ACR_ENOTIMPL);
+}
+
+ACR_JNI_EXPORT(void, Group, enum2)(JNI_STDARGS, jobject ua, jlong id)
+{
+    int rc, i = 0;
+    gid_t  gid = (gid_t)id;
+    struct group gr;
+    char buffer[GRBUF_SIZE];
+
+    rc = getgrgid_s(gid, &gr, buffer);
+    if (rc != 0) {
+        ACR_THROW_SYS_ERROR(rc);
+        return;
+    }    
+    while (gr.gr_mem[i] != 0) {
+        jstring s;
+        s = AcrNewJavaStringA(env, gr.gr_mem[i]);
+        if (s == 0)
+            break;
+        AcrArrayListAdd(env, ua, s);
+        if ((*env)->ExceptionCheck(env))
+            break;
+        (*env)->DeleteLocalRef(env, s);
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c?rev=1165370&r1=1165369&r2=1165370&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c Mon Sep  5 17:41:02 2011
@@ -235,9 +235,7 @@ user_byname(JNI_STDARGS, const char *nam
 
     rc = getpwnam_s(name, &pw, buffer);
     if (rc != 0) {
-        rc = ACR_GET_OS_ERROR();
-        if (!ACR_STATUS_IS_ENOENT(rc))
-            ACR_THROW_SYS_ERROR(rc);
+        ACR_THROW_SYS_ERROR(rc);
         return 0;
     }
     if (obj == 0)
@@ -269,9 +267,7 @@ user_fromuid(JNI_STDARGS, jlong id)
     
     rc = getpwuid_s(uid, &pw, buffer);
     if (rc != 0) {
-        rc = ACR_GET_OS_ERROR();
-        if (!ACR_STATUS_IS_ENOENT(rc))
-            ACR_THROW_SYS_ERROR(rc);
+        ACR_THROW_SYS_ERROR(rc);
         return 0;
     }
     if (obj == 0)

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=1165370&r1=1165369&r2=1165370&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Mon Sep  5 17:41:02 2011
@@ -231,6 +231,7 @@ AcrLoadRuntimeClasses(JNI_STDENV)
     ACR_CLASS_LOAD(HeapPointer);
     ACR_CLASS_LOAD(SlicePointer);
     ACR_CLASS_LOAD(FileDescriptor);
+    ACR_CLASS_LOAD(Group);
     ACR_CLASS_LOAD(User);
 
 #if defined(WINDOWS)
@@ -244,6 +245,7 @@ AcrUnloadRuntimeClasses(JNI_STDENV)
 {
 
     ACR_CLASS_UNLOAD(User);
+    ACR_CLASS_UNLOAD(Group);
     ACR_CLASS_UNLOAD(ConstPointer);
     ACR_CLASS_UNLOAD(HeapPointer);
     ACR_CLASS_UNLOAD(SlicePointer);

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java?rev=1165370&r1=1165369&r2=1165370&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUser.java Mon
Sep  5 17:41:02 2011
@@ -56,4 +56,34 @@ public class TestUser extends Assert
         assertTrue(i > 0);
     }
 
+    @Test(groups = { "core" })
+    public void getLocalGroups()
+    {
+        GroupIterator groups = Group.getLocalGroups();
+        assertNotNull(groups);
+        int i = 0;
+        while (groups.hasNext()) {
+            Group g = groups.next();
+            i++;
+        }
+        assertTrue(i > 0);
+    }
+
+    @Test(groups = { "core" })
+    public void getGlobalGroups()
+    {
+        try {
+            GroupIterator groups = Group.getGlobalGroups();
+            assertNotNull(groups);
+            int i = 0;
+            while (groups.hasNext()) {
+                Group g = groups.next();
+                i++;
+            }
+            assertTrue(i > 0);
+        } catch (OperationNotImplementedException x) {
+            // Skip test
+        }
+    }
+
 }



Mime
View raw message