Author: mturk Date: Mon Apr 27 18:00:03 2009 New Revision: 769072 URL: http://svn.apache.org/viewvc?rev=769072&view=rev Log: Add user and group skeleton
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Group.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/User.java (with props) commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c (with props) commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c (with props) commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java (with props) commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java (with props) Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java commons/sandbox/runtime/trunk/src/main/native/Makefile.in commons/sandbox/runtime/trunk/src/main/native/include/acr.h commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c commons/sandbox/runtime/trunk/src/main/native/shared/memory.c commons/sandbox/runtime/trunk/src/main/native/shared/string.c commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java Mon Apr 27 18:00:03 2009 @@ -39,7 +39,7 @@ * If the operation is recoverable the clearerr() should be * used after the recovery. */ - private int IERRNUM; + private int IERRNUM; /* * Descriptor can be only created from native code. Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java Mon Apr 27 18:00:03 2009 @@ -70,7 +70,7 @@ { if (PHANDLE != 0) { String h = Integer.toHexString(PHANDLE); - return "0x" + ("00000000" + h).substring(h.length()); + return ("00000000" + h).substring(h.length()); } else if (IHANDLE >= 0) { return Integer.toString(IHANDLE); Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java Mon Apr 27 18:00:03 2009 @@ -70,7 +70,7 @@ { if (PHANDLE != 0) { String h = Long.toHexString(PHANDLE); - return "0x" + ("0000000000000000" + h).substring(h.length()); + return ("0000000000000000" + h).substring(h.length()); } else if (IHANDLE >= 0) { return Integer.toString(IHANDLE); Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java?rev=769072&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java Mon Apr 27 18:00:03 2009 @@ -0,0 +1,38 @@ +/* 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.exception; +import java.io.IOException; + +/** + * ClosedDescriptorException thrown when an attempt is made to + * invoke an operation on closed {...@code Descriptor}. + * + * @since Runtime 1.0 + */ + +public class ClosedDescriptorException extends IOException { + + public ClosedDescriptorException() + { + super(); + } + + public ClosedDescriptorException(String msg) + { + super(msg); + } +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java?rev=769072&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java Mon Apr 27 18:00:03 2009 @@ -0,0 +1,38 @@ +/* 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.exception; + +/** + * NoSuchObjectException thrown when an attempt is made to + * invoke an operation on unexisting operating system {...@code object}. + * This class mimics the os {...@code ENOENT} error. + * + * @since Runtime 1.0 + */ + +public class NoSuchObjectException extends Exception { + + public NoSuchObjectException() + { + super(); + } + + public NoSuchObjectException(String msg) + { + super(msg); + } +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Group.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Group.java?rev=769072&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Group.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Group.java Mon Apr 27 18:00:03 2009 @@ -0,0 +1,137 @@ +/* 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.util; + +import org.apache.commons.runtime.Descriptor; +import org.apache.commons.runtime.exception.NoSuchObjectException; + +import java.io.IOException; +import java.util.Iterator; + +/** + * Group Information. + */ +public final class Group +{ + + private Group() + { + // No instance. + Id = null; + } + + private Group(Descriptor id) + { + Id = id; + } + + private static native Group get0(String name); + private static native boolean equals0(Descriptor a, Descriptor b); + + /** + * 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 IOException in case of I/O error. + * @throws NoSuchObjectException if the group {...@code name} doesn't exist. + */ + public static Group get(String name) + throws IOException, SecurityException, NoSuchObjectException + { + Group g = get0(name); + if (g == null) + throw new NoSuchObjectException("Group '" + name + "' not found."); + return g; + } + + /** + * 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() + { + 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 Descriptor Id; + + /** + * Compares this {...@code 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. + */ + 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 GroupId; + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Group.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/User.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/User.java?rev=769072&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/User.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/User.java Mon Apr 27 18:00:03 2009 @@ -0,0 +1,156 @@ +/* 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.util; + +import org.apache.commons.runtime.Descriptor; +import org.apache.commons.runtime.exception.NoSuchObjectException; + +import java.io.IOException; +import java.util.Iterator; + +/** + * User Information. + * + */ +public final class User +{ + + private User() + { + // No instance. + Id = null; + } + + private User(Descriptor id) + { + Id = id; + } + + private static native User get0(String name); + private static native boolean equals0(Descriptor a, Descriptor b); + + /** + * Create the {...@code User} object from the {...@code name}. + * + * @return {...@code User} obect. + * @throws SecurityException if access to an internal user database + * is forbidden. + * @throws IOException in case of I/O error. + * @throws NoSuchObjectException if the user {...@code name} doesn't exist. + */ + public static User get(String name) + throws IOException, SecurityException, NoSuchObjectException + { + User u = get0(name); + if (u == null) + throw new NoSuchObjectException("User '" + name + "' not found."); + return u; + } + + /** + * String that specifies the name of the user account. + */ + public String getName() + { + return Name; + } + private String Name; + + /** + * String that contains the full name of the user. This string can be + * a null string. + */ + public String getFullName() + { + return FullName; + } + private String FullName; + + /** + * String that contains a comment associated with the user. This string can + * be a null string. + */ + public String getComment() + { + return Comment; + } + private String Comment; + + /** + * String that contains a comment users Home directory. + */ + public String getHome() + { + return Home; + } + private String Home; + + /** + * String that contains a user shell. + */ + public String getShell() + { + return Shell; + } + private String Shell; + + /** + * Returns a string representation of the User. + */ + public String getUserId() + { + return UserId; + } + private String UserId; + + /** + * Specifies the <em>uid_t</em> or <em>PSID</em> + * {...@code Descriptor} identifier of the user. + */ + public final Descriptor Id; + + /** + * Compares this {...@code User} to the specified object. + * + * @param other a {...@code User} + * @return true if the class of this {...@code User} object and the + * class of {...@code other} are exactly equal, and they point + * to the same system user id. + */ + public boolean equals(Object other) + { + if (other == null) + return false; + if (other == this) + return true; + if (User.class != other.getClass()) + return false; + return equals0(Id, ((User)other).Id); + } + + /** + * Returns a string representation of the User. + * The returned string is native representation of the underlying + * user descriptor. + * @return a string representation of the user id. + */ + @Override + public String toString() + { + return UserId; + } +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/User.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Mon Apr 27 18:00:03 2009 @@ -80,6 +80,8 @@ LINUX_OBJS= \ $(SRCDIR)/os/unix/file.$(OBJ) \ $(SRCDIR)/os/unix/main.$(OBJ) \ + $(SRCDIR)/os/unix/group.$(OBJ) \ + $(SRCDIR)/os/unix/user.$(OBJ) \ $(SRCDIR)/os/unix/uuid.$(OBJ) \ $(SRCDIR)/os/unix/uutils.$(OBJ) \ $(SRCDIR)/os/linux/platform.$(OBJ) \ @@ -88,6 +90,8 @@ SOLARIS_OBJS= \ $(SRCDIR)/os/unix/file.$(OBJ) \ $(SRCDIR)/os/unix/main.$(OBJ) \ + $(SRCDIR)/os/unix/group.$(OBJ) \ + $(SRCDIR)/os/unix/user.$(OBJ) \ $(SRCDIR)/os/unix/uuid.$(OBJ) \ $(SRCDIR)/os/unix/uutils.$(OBJ) \ $(SRCDIR)/os/solaris/platform.$(OBJ) \ @@ -96,6 +100,8 @@ DARWIN_OBJS= \ $(SRCDIR)/os/unix/file.$(OBJ) \ $(SRCDIR)/os/unix/main.$(OBJ) \ + $(SRCDIR)/os/unix/group.$(OBJ) \ + $(SRCDIR)/os/unix/user.$(OBJ) \ $(SRCDIR)/os/unix/uuid.$(OBJ) \ $(SRCDIR)/os/unix/uutils.$(OBJ) \ $(SRCDIR)/os/darwin/platform.$(OBJ) \ Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr.h?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr.h Mon Apr 27 18:00:03 2009 @@ -208,7 +208,9 @@ typedef int acr_socklen_t; /* API char type on windows is always WCHAR */ typedef wchar_t acr_pchar_t; -#else +typedef PSID acr_uid_t; +typedef PSID acr_gid_t; +#else /* !_MSC_VER */ typedef ssize_t acr_ssize_t; #ifdef _LP64 /* 64-bit Solaris */ typedef long acr_int64_t; @@ -225,7 +227,9 @@ typedef socklen_t acr_socklen_t; /* API char type on POSIX is always char (UTF-8) */ typedef char acr_pchar_t; -#endif +typedef uid_t acr_uid_t; +typedef gid_t acr_gid_t; +#endif /* _MSC_VER */ #if CC_SIZEOF_VOIDP == 8 typedef acr_uint64_t acr_ptr_t; Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h Mon Apr 27 18:00:03 2009 @@ -341,21 +341,21 @@ #define SET_IFIELD_S(I, O, V) \ if (_f##I##n.i && (V)) { \ - jstring _str = (*_E)->NewStringUTF(_E, (V)); \ + jstring _str = ACR_NewJavaStringA(_E, (V)); \ (*_E)->SetObjectField(_E, (O), _f##I##n.i, _str); \ (*_E)->DeleteLocalRef(_E, _str); \ } else (void)(0) #define SET_IFIELD_N(I, O, V) \ if (_f##I##n.i && (V) && *(V)) { \ - jstring _str = (*_E)->NewStringUTF(_E, (V)); \ + jstring _str = ACR_NewJavaStringA(_E, (V)); \ (*_E)->SetObjectField(_E, (O), _f##I##n.i, _str); \ (*_E)->DeleteLocalRef(_E, _str); \ } else (void)(0) #define SET_IFIELD_W(I, O, V) \ if (_f##I##n.i && (V)) { \ - jstring _str = (*_E)->NewString(_E, (V), (jsize)wcslen((V))); \ + jstring _str = ACR_NewJavaStringW(_E, (V)); \ (*_E)->SetObjectField(_E, (O), _f##I##n.i, _str); \ (*_E)->DeleteLocalRef(_E, _str); \ } else (void)(0) @@ -420,6 +420,21 @@ #define CALL_METHODN(T, I, O, ARGS) \ (*_E)->Call##T##Method(_E, (O), _m##I##n.i, ARGS) +#define SET_SARRAY_A(A, I, V) \ + if ((A) && (V)) { \ + jstring _str = ACR_NewJavaStringA(_E, (V)); \ + (*_E)->SetObjectArrayElement(_E, (A), (I), _str); \ + (*_E)->DeleteLocalRef(_E, _str); \ + } else (void)(0) + +#define SET_SARRAY_W(A, I, V) \ + if ((A) && (V)) { \ + jstring _str = ACR_NewJavaStringW(_E, (V)); \ + (*_E)->SetObjectArrayElement(_E, (A), (I), _str); \ + (*_E)->DeleteLocalRef(_E, _str); \ + } else (void)(0) + + #if defined(DEBUG) || defined(_DEBUG) /* In DEBUG mode always use statistics */ #ifndef ACR_DO_STATS 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=769072&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 Apr 27 18:00:03 2009 @@ -0,0 +1,157 @@ +/* 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.h" +#include "acr_private.h" +#include "acr_arch.h" +#include "acr_clazz.h" +#include "acr_error.h" +#include "acr_memory.h" +#include "acr_string.h" +#include "acr_descriptor.h" + +#include <pwd.h> +#include <grp.h> + +J_DECLARE_CLAZZ = { + NULL, + NULL, + ACR_CLASS_PATH "util/Group" +}; + +J_DECLARE_M_ID(0000) = { + NULL, + "<init>", + "(L" ACR_CLASS_PATH "Descriptor;)V" +}; + +J_DECLARE_F_ID(0000) = { + NULL, + "Name", + "Ljava/lang/String;" +}; + +J_DECLARE_F_ID(0001) = { + NULL, + "Comment", + "Ljava/lang/String;" +}; + +J_DECLARE_F_ID(0002) = { + NULL, + "GroupId", + "Ljava/lang/String;" +}; + + +J_DECLARE_F_ID(0003) = { + NULL, + "isLocal", + "Z" +}; + +ACR_CLASS_LDEF(util_Group) +{ + int rv; + + if ((rv = ACR_LoadClass(_E, &_clazzn, 0)) != ACR_SUCCESS) + return rv; + J_LOAD_METHOD(0000); + J_LOAD_IFIELD(0000); + J_LOAD_IFIELD(0001); + J_LOAD_IFIELD(0002); + J_LOAD_IFIELD(0003); + + return ACR_SUCCESS; +} + +ACR_CLASS_UDEF(util_Group) +{ + ACR_UnloadClass(_E, &_clazzn); +} + +ACR_DECLARE(jobjectArray) ACR_NewGroupArray(JNIEnv *_E, jsize len) +{ + if (_clazzn.i) + return (*_E)->NewObjectArray(_E, len, _clazzn.i, NULL); + else + return NULL; +} + +static jobject grp_object_create(JNIEnv *_E, struct group *gr) +{ + jobject gid; + jobject grp; + char *sid; + + gid = ACR_DescriptorCreate(_E, 0, gr->gr_gid, NULL, NULL); + if (!gid) { + + return NULL; + } + grp = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), gid); + if (!grp) { + + return NULL; + } + + SET_IFIELD_S(0000, grp, gr->gr_name); + /* Comment is unknown */ + sid = ACR_Ltoa((acr_long_t)gr->gr_gid); + SET_IFIELD_S(0002, grp, sid); + SET_IFIELD_Z(0003, grp, JNI_TRUE); + ACR_Free(_E, THROW_FMARK, sid); + + return grp; +} + +ACR_JNI_EXPORT_DECLARE(jobject, util_Group, get0)(ACR_JNISTDARGS, + jstring name) +{ + jobject grp = NULL; + UNREFERENCED_O; + + WITH_CSTR(name) { + int rc; + struct group grb; + struct group *pp; + char buffer[4096]; + + rc = getgrnam_r(J2S(name), &grb, buffer, sizeof(buffer), &pp); + if (rc) { + if (ACR_STATUS_IS_EACCES(ACR_FROM_OS_ERROR(rc))) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, + ACR_FROM_OS_ERROR(rc)); + } + else { + if (pp) + grp = grp_object_create(_E, pp); + } + } END_WITH_CSTR(name); + + return grp; +} + +ACR_JNI_EXPORT_DECLARE(jboolean, util_Group, equals0)(ACR_JNISTDARGS, + jobject a, jobject b) +{ + gid_t gida = ACR_DescriptorGetInt(_E, a); + gid_t gidb = ACR_DescriptorGetInt(_E, b); + + return V2Z(gida == gidb); +} Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c ------------------------------------------------------------------------------ svn:eol-style = native Added: 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=769072&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c (added) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c Mon Apr 27 18:00:03 2009 @@ -0,0 +1,172 @@ +/* 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.h" +#include "acr_private.h" +#include "acr_arch.h" +#include "acr_clazz.h" +#include "acr_error.h" +#include "acr_memory.h" +#include "acr_string.h" +#include "acr_descriptor.h" + +#include <pwd.h> +#include <grp.h> + +J_DECLARE_CLAZZ = { + NULL, + NULL, + ACR_CLASS_PATH "util/User" +}; + +J_DECLARE_M_ID(0000) = { + NULL, + "<init>", + "(L" ACR_CLASS_PATH "Descriptor;)V" +}; + +J_DECLARE_F_ID(0000) = { + NULL, + "Name", + "Ljava/lang/String;" +}; + +J_DECLARE_F_ID(0001) = { + NULL, + "FullName", + "Ljava/lang/String;" +}; + +J_DECLARE_F_ID(0002) = { + NULL, + "Comment", + "Ljava/lang/String;" +}; + +J_DECLARE_F_ID(0003) = { + NULL, + "Home", + "Ljava/lang/String;" +}; + +J_DECLARE_F_ID(0004) = { + NULL, + "Shell", + "Ljava/lang/String;" +}; + +J_DECLARE_F_ID(0005) = { + NULL, + "UserId", + "Ljava/lang/String;" +}; + +ACR_CLASS_LDEF(util_User) +{ + int rv; + + if ((rv = ACR_LoadClass(_E, &_clazzn, 0)) != ACR_SUCCESS) + return rv; + J_LOAD_METHOD(0000); + J_LOAD_IFIELD(0000); + J_LOAD_IFIELD(0001); + J_LOAD_IFIELD(0002); + J_LOAD_IFIELD(0003); + J_LOAD_IFIELD(0004); + J_LOAD_IFIELD(0005); + + return ACR_SUCCESS; +} + +ACR_CLASS_UDEF(util_User) +{ + ACR_UnloadClass(_E, &_clazzn); +} + +ACR_DECLARE(jobjectArray) ACR_NewUserArray(JNIEnv *_E, jsize len) +{ + if (_clazzn.i) + return (*_E)->NewObjectArray(_E, len, _clazzn.i, NULL); + else + return NULL; +} + +static jobject usr_object_create(JNIEnv *_E, struct passwd *pw) +{ + jobject uid; + jobject usr; + char *sid; + + uid = ACR_DescriptorCreate(_E, 0, pw->pw_uid, NULL, NULL); + if (!uid) { + + return NULL; + } + usr = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), uid); + if (!usr) { + + return NULL; + } + + SET_IFIELD_S(0000, usr, pw->pw_name); + SET_IFIELD_S(0001, usr, pw->pw_gecos); + /* Comment is unknown */ + SET_IFIELD_S(0003, usr, pw->pw_dir); + SET_IFIELD_S(0004, usr, pw->pw_shell); + sid = ACR_Ltoa((acr_long_t)pw->pw_uid); + SET_IFIELD_S(0005, usr, sid); + ACR_Free(_E, THROW_FMARK, sid); + + return usr; +} + +ACR_JNI_EXPORT_DECLARE(jobject, util_User, get0)(ACR_JNISTDARGS, + jstring name) +{ + jobject usr = NULL; + UNREFERENCED_O; + + WITH_CSTR(name) { + int rc; + struct passwd pwb; + struct passwd *pp; + char buffer[4096]; + + rc = getpwnam_r(J2S(name), &pwb, buffer, sizeof(buffer), &pp); + if (rc) { + if (ACR_STATUS_IS_EACCES(ACR_FROM_OS_ERROR(rc))) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, + ACR_FROM_OS_ERROR(rc)); + } + else { + if (pp) + usr = usr_object_create(_E, pp); + } + } END_WITH_CSTR(name); + + return usr; +} + +ACR_JNI_EXPORT_DECLARE(jboolean, util_User, equals0)(ACR_JNISTDARGS, + jobject a, jobject b) +{ + uid_t uida = ACR_DescriptorGetInt(_E, a); + uid_t uidb = ACR_DescriptorGetInt(_E, b); + + return V2Z(uida == uidb); +} Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/file.c Mon Apr 27 18:00:03 2009 @@ -341,22 +341,20 @@ int protection = 0; PSID user = NULL, group = NULL; PACL dacl = NULL; - SECURITY_INFORMATION sinf = 0; PSECURITY_DESCRIPTOR pdesc = NULL; int fix = 0; int rc; if (wcsncmp(fname, L"\\\\?\\", 4) == 0) { - fix = 4; - if (wcsncmp(fname + fix, L"UNC\\", 4) == 0) { - fname[6] = L'\\'; - fix = 6; + fname += 4; + if (wcsncmp(fname, L"UNC\\", 4) == 0) { + fname += 2; + *fname = L'\\'; } } - sinf = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | - DACL_SECURITY_INFORMATION; - rc = GetNamedSecurityInfoW(fname + fix, - SE_FILE_OBJECT, sinf, + rc = GetNamedSecurityInfoW(fname, + SE_FILE_OBJECT, + OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, &user, &group, &dacl, 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=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Mon Apr 27 18:00:03 2009 @@ -247,6 +247,8 @@ ACR_CLASS_LDEC(Descriptor); ACR_CLASS_LDEC(Pointer); ACR_CLASS_LDEC(io_File); +ACR_CLASS_LDEC(util_Group); +ACR_CLASS_LDEC(util_User); ACR_DECLARE(int) ACR_LoadRuntimeClasses(JNIEnv *_E) { @@ -254,6 +256,8 @@ ACR_CLASS_LRUN(Descriptor); ACR_CLASS_LRUN(Pointer); ACR_CLASS_LRUN(io_File); + ACR_CLASS_LRUN(util_Group); + ACR_CLASS_LRUN(util_User); #ifdef WIN32 #endif @@ -267,6 +271,8 @@ ACR_CLASS_URUN(Descriptor); ACR_CLASS_URUN(Pointer); ACR_CLASS_URUN(io_File); + ACR_CLASS_URUN(util_Group); + ACR_CLASS_URUN(util_User); #ifdef WIN32 #endif Modified: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c Mon Apr 27 18:00:03 2009 @@ -23,6 +23,7 @@ #include "acr_private.h" #include "acr_error.h" #include "acr_clazz.h" +#include "acr_string.h" #include "acr_descriptor.h" /** Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memory.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memory.c?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Mon Apr 27 18:00:03 2009 @@ -37,11 +37,11 @@ void *mem = malloc(size); if (!mem) { + int err = ACR_GET_OS_ERROR(); if (_E == NULL) _E = ACR_GetJNIEnv(); if (_E != NULL) - ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, - ACR_GET_OS_ERROR()); + ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, err); } return mem; } @@ -52,11 +52,11 @@ void *mem = calloc(1, size); if (!mem) { + int err = ACR_GET_OS_ERROR(); if (_E == NULL) _E = ACR_GetJNIEnv(); if (_E != NULL) - ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, - ACR_GET_OS_ERROR()); + ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, err); } return mem; } @@ -67,11 +67,11 @@ void *mem = realloc(org, size); if (!mem) { + int err = ACR_GET_OS_ERROR(); if (_E == NULL) _E = ACR_GetJNIEnv(); if (_E != NULL) - ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, - ACR_GET_OS_ERROR()); + ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, err); } return mem; } Modified: commons/sandbox/runtime/trunk/src/main/native/shared/string.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/string.c?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Mon Apr 27 18:00:03 2009 @@ -21,6 +21,7 @@ #include "acr_error.h" #include "acr_types.h" #include "acr_clazz.h" +#include "acr_vm.h" J_DECLARE_CLAZZ = { NULL, @@ -120,12 +121,29 @@ return s ? ++s : pathname; } +ACR_DECLARE(char *) ACR_Strdup(JNIEnv *_E, const char *file, int line, + const char *s) +{ + char *d = NULL; + if (s) { + d = strdup(s); + if (!d) { + int err = ACR_GET_OS_ERROR(); + if (_E == NULL) + _E = ACR_GetJNIEnv(); + if (_E != NULL) + ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, err); + } + } + return d; +} + ACR_DECLARE(char *) ACR_Itoa(acr_int_t n) { const int BUFFER_SIZE = sizeof(jint) * 3 + 2; char *start; int negative; - char *buf = ACR_Malloc(NULL, THROW_FMARK, BUFFER_SIZE); + char buf[BUFFER_SIZE]; if (!buf) return NULL; @@ -145,7 +163,7 @@ if (negative) { *--start = '-'; } - return start; + return ACR_Strdup(NULL, THROW_FMARK, start); } ACR_DECLARE(char *) ACR_Ltoa(acr_long_t n) @@ -153,7 +171,7 @@ const int BUFFER_SIZE = sizeof(jlong) * 3 + 2; char *start; int negative; - char *buf = ACR_Malloc(NULL, THROW_FMARK, BUFFER_SIZE); + char buf[BUFFER_SIZE]; if (!buf) return NULL; @@ -173,7 +191,7 @@ if (negative) { *--start = '-'; } - return start; + return ACR_Strdup(NULL, THROW_FMARK, start); } ACR_DECLARE(char *) ACR_StrLwr(char *src) Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java (original) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java Mon Apr 27 18:00:03 2009 @@ -35,6 +35,8 @@ suite.addTest(TestProperties.suite()); suite.addTest(TestOS.suite()); suite.addTest(TestUUID.suite()); + suite.addTest(TestUser.suite()); + suite.addTest(TestGroup.suite()); suite.addTest(TestStatus.suite()); suite.addTest(TestMemory.suite()); suite.addTest(TestDirectByteBuffer.suite()); Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java?rev=769072&r1=769071&r2=769072&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java (original) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java Mon Apr 27 18:00:03 2009 @@ -173,6 +173,7 @@ File f = new File("ffoo"); f.createNewFile(); EnumSet <FileProtection> fp = f.getFileProtection(); + System.out.println("Protection " + fp); assertTrue("GWRITE", fp.contains(FileProtection.GREAD)); f.delete(); } Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java?rev=769072&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java (added) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java Mon Apr 27 18:00:03 2009 @@ -0,0 +1,64 @@ +/* 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 org.apache.commons.runtime.util.Group; +import org.apache.commons.runtime.exception.*; + +import java.lang.System; +import junit.framework.*; + +/** + * Group Test. + * + */ +public class TestGroup extends TestCase +{ + + public static Test suite() { + TestSuite suite = new TestSuite(TestGroup.class); + return suite; + } + + protected void setUp() + throws Exception + { + System.loadLibrary("acr"); + } + + public void testGroup() + throws Exception + { + Group g; + + g = Group.get("bin"); + assertNotNull("Group" + g); + assertTrue("Local", g.IsLocal()); + } + + public void testUnknownGroup() + throws Exception + { + try { + Group g = Group.get("the_group_that_do_not_exist"); + fail("NoSuchObjectException not thrown"); + } catch (NoSuchObjectException ex) { + // OK. + } + } + +} Propchange: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java?rev=769072&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java (added) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java Mon Apr 27 18:00:03 2009 @@ -0,0 +1,69 @@ +/* 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 org.apache.commons.runtime.util.User; +import org.apache.commons.runtime.exception.*; + +import java.lang.System; +import junit.framework.*; + +/** + * User Test. + * + */ +public class TestUser extends TestCase +{ + + public static Test suite() { + TestSuite suite = new TestSuite(TestUser.class); + return suite; + } + + protected void setUp() + throws Exception + { + System.loadLibrary("acr"); + } + + public void testUser() + throws Exception + { + User u; + + u = User.get("root"); + assertNotNull("User" + u); + System.out.println(); + System.out.println("User " + u.getFullName()); + System.out.println("Home " + u.getHome()); + System.out.println("Comment " + u.getComment()); + System.out.println("Shell " + u.getShell()); + System.out.println("Id " + u); + } + + public void testUnknownUser() + throws Exception + { + try { + User u = User.get("the_user_that_do_not_exist"); + fail("NoSuchObjectException not thrown"); + } catch (NoSuchObjectException ex) { + // OK. + } + } + +} Propchange: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java ------------------------------------------------------------------------------ svn:eol-style = native