#include "com_macmillan_nmeyers_FileStatus.h" #include #include #include #include #include #include #include /* FileStatus.c: Implement some POSIX-specific file system access. Copyright (c) 1999 Nathan Meyers $Id: FileStatus.c,v 1.3 1999/11/08 05:03:42 nathanm Exp $ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ void Java_com_macmillan_nmeyers_FileStatus_getExtendedInformation (JNIEnv *env, jobject obj) { struct stat statbuf; /* Get our class */ jclass cls = (*env)->GetObjectClass(env, obj); /* Get a method ID for toString() */ jmethodID mid = (*env)->GetMethodID(env, cls, "toString", "()Ljava/lang/String;"); /* Get the full file path */ jstring filename = (*env)->CallObjectMethod(env, obj, mid); /* Extract the name into a buffer */ const char *filename_str = (*env)->GetStringUTFChars(env, filename, 0); /* Get file status */ int result = lstat(filename_str, &statbuf); /* Did the lstat succeed? */ if (!result) { /* Yes. Get the name for the UID */ struct passwd *passwd_entry = getpwuid(statbuf.st_uid); char pwbuf[64], *pwname; jstring uid_string = 0; jfieldID uid_fieldid; /* Yes. And the name for the GID */ struct group *group_entry = getgrgid(statbuf.st_gid); char grbuf[64], *grname; jstring gid_string = 0; jfieldID gid_fieldid; /* And the target of the link */ char *linktarget = 0; char linkbuf[NAME_MAX + 1]; jstring target_string = 0; jfieldID mode_fieldid; /* Did we find a password entry? */ if (passwd_entry) { /* Yes. point to it. */ pwname = passwd_entry->pw_name; } else { /* No. print out the number to a tempbuf and use that */ sprintf(pwbuf, "%ld", (long)statbuf.st_uid); pwname = pwbuf; } /* Did we find a group entry? */ if (group_entry) { /* Yes. point to it. */ grname = group_entry->gr_name; } else { /* No. print out the number to a tempbuf and use that */ sprintf(grbuf, "%ld", (long)statbuf.st_gid); grname = grbuf; } /* Do we have a link? */ if (S_ISLNK(statbuf.st_mode)) { /* Yes. Resolve the name. */ int namelen = readlink(filename_str, linkbuf, NAME_MAX); /* If success, null-terminate the string and point at it */ if (namelen > 0) { linkbuf[namelen] = 0; linktarget = linkbuf; } } /* We have everything. Time to start filling in our object. First create the strings */ uid_string = (*env)->NewStringUTF(env, pwname); gid_string = (*env)->NewStringUTF(env, grname); if (linktarget) target_string = (*env)->NewStringUTF(env, linktarget); /* Find the field ID for the uid */ uid_fieldid = (*env)->GetFieldID(env, cls, "uid", "Ljava/lang/String;"); /* Set the string */ (*env)->SetObjectField(env, obj, uid_fieldid, uid_string); /* Find the field ID for the gid */ gid_fieldid = (*env)->GetFieldID(env, cls, "gid", "Ljava/lang/String;"); /* Set the string */ (*env)->SetObjectField(env, obj, gid_fieldid, gid_string); if (linktarget) { jfieldID target_fieldid = (*env)->GetFieldID(env, cls, "target", "Ljava/lang/String;"); (*env)->SetObjectField(env, obj, target_fieldid, target_string); } /* Finally, find the field ID for the mode */ mode_fieldid = (*env)->GetFieldID(env, cls, "mode", "I"); /* And set it */ (*env)->SetIntField(env, obj, mode_fieldid, (jint)statbuf.st_mode); } /* Release the name buffer */ (*env)->ReleaseStringUTFChars(env, filename, filename_str); }