utilities.cpp 943 B

12345678910111213141516171819202122232425262728293031
  1. #include <jni.h>
  2. #include <sys/stat.h>
  3. #include <climits>
  4. #include <unistd.h>
  5. #include <string>
  6. thread_local static char buf[PATH_MAX + 1];
  7. extern "C" JNIEXPORT jstring Java_org_telegram_messenger_Utilities_readlink(JNIEnv *env, jclass clazz, jstring path) {
  8. const char *fileName = env->GetStringUTFChars(path, NULL);
  9. ssize_t result = readlink(fileName, buf, PATH_MAX);
  10. jstring value = 0;
  11. if (result != -1) {
  12. buf[result] = '\0';
  13. value = env->NewStringUTF(buf);
  14. }
  15. env->ReleaseStringUTFChars(path, fileName);
  16. return value;
  17. }
  18. extern "C" JNIEXPORT jstring Java_org_telegram_messenger_Utilities_readlinkFd(JNIEnv *env, jclass clazz, int fd) {
  19. std::string path = "/proc/self/fd/";
  20. path += fd;
  21. ssize_t result = readlink(path.c_str(), buf, PATH_MAX);
  22. jstring value = 0;
  23. if (result != -1) {
  24. buf[result] = '\0';
  25. value = env->NewStringUTF(buf);
  26. }
  27. return value;
  28. }