M
Mikey
I think I must have a bug somewhere in this code. Sometimes, it will
return -1. While I realize that I've programmed it to do so if the
lstat fails, I'm curious as to why it fails. I've also seen what I
believe to be the wrong file permissions returned on one occasion (it
returned 755, but from everything I can see, the permissions have
never been anything but 644, and it usually returns that). Any help
would be appreciated.
#include <jni.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "FileStat.h"
/*
* compile with:
* gcc -G -I/usr/java/include/ -I/usr/java/include/solaris \
* FileStatImp.c -o libjstat.so
*/
JNIEXPORT jint JNICALL Java_ssm_FileStat_getOwner
(JNIEnv *env, jobject obj, jstring file) {
int retval;
char buf[128];
const char *str = (*env)->GetStringUTFChars(env, file, 0);
struct stat *buffer;
buffer = (struct stat *) malloc(sizeof(struct stat));
(*env)->ReleaseStringUTFChars(env, file, str);
if(lstat(str, buffer) == 0) {
retval = buffer->st_uid;
}
else {
retval = -1;
}
return retval;
}
JNIEXPORT jint JNICALL Java_ssm_FileStat_getGroup
(JNIEnv *env, jobject obj, jstring file) {
int retval;
char buf[128];
const char *str = (*env)->GetStringUTFChars(env, file, 0);
struct stat *buffer;
buffer = (struct stat *) malloc(sizeof(struct stat));
(*env)->ReleaseStringUTFChars(env, file, str);
if(lstat(str, buffer) == 0) {
retval = buffer->st_gid;
}
else {
retval = -1;
}
return retval;
}
JNIEXPORT jint JNICALL Java_ssm_FileStat_getMode
(JNIEnv *env, jobject obj, jstring file) {
int retval;
char mode[5];
char buf[128];
const char *str = (*env)->GetStringUTFChars(env, file, 0);
struct stat *buffer;
buffer = (struct stat *) malloc(sizeof(struct stat));
(*env)->ReleaseStringUTFChars(env, file, str);
if(lstat(str, buffer) == 0) {
sprintf(mode, "%04o", buffer->st_mode & 07777);
retval = atoi(mode);
}
else {
retval = -1;
}
return retval;
}
return -1. While I realize that I've programmed it to do so if the
lstat fails, I'm curious as to why it fails. I've also seen what I
believe to be the wrong file permissions returned on one occasion (it
returned 755, but from everything I can see, the permissions have
never been anything but 644, and it usually returns that). Any help
would be appreciated.
#include <jni.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "FileStat.h"
/*
* compile with:
* gcc -G -I/usr/java/include/ -I/usr/java/include/solaris \
* FileStatImp.c -o libjstat.so
*/
JNIEXPORT jint JNICALL Java_ssm_FileStat_getOwner
(JNIEnv *env, jobject obj, jstring file) {
int retval;
char buf[128];
const char *str = (*env)->GetStringUTFChars(env, file, 0);
struct stat *buffer;
buffer = (struct stat *) malloc(sizeof(struct stat));
(*env)->ReleaseStringUTFChars(env, file, str);
if(lstat(str, buffer) == 0) {
retval = buffer->st_uid;
}
else {
retval = -1;
}
return retval;
}
JNIEXPORT jint JNICALL Java_ssm_FileStat_getGroup
(JNIEnv *env, jobject obj, jstring file) {
int retval;
char buf[128];
const char *str = (*env)->GetStringUTFChars(env, file, 0);
struct stat *buffer;
buffer = (struct stat *) malloc(sizeof(struct stat));
(*env)->ReleaseStringUTFChars(env, file, str);
if(lstat(str, buffer) == 0) {
retval = buffer->st_gid;
}
else {
retval = -1;
}
return retval;
}
JNIEXPORT jint JNICALL Java_ssm_FileStat_getMode
(JNIEnv *env, jobject obj, jstring file) {
int retval;
char mode[5];
char buf[128];
const char *str = (*env)->GetStringUTFChars(env, file, 0);
struct stat *buffer;
buffer = (struct stat *) malloc(sizeof(struct stat));
(*env)->ReleaseStringUTFChars(env, file, str);
if(lstat(str, buffer) == 0) {
sprintf(mode, "%04o", buffer->st_mode & 07777);
retval = atoi(mode);
}
else {
retval = -1;
}
return retval;
}