cbalint13 commented on code in PR #19840:
URL: https://github.com/apache/tvm/pull/19840#discussion_r3441739458


##########
apps/cpp_rpc/rpc_env.cc:
##########
@@ -69,38 +57,41 @@ namespace tvm {
 namespace runtime {
 
 RPCEnv::RPCEnv(const std::string& wd) {
-  if (wd != "") {
+  if (!wd.empty()) {
     base_ = wd + "/.cache";
-    mkdir(wd.c_str(), 0777);
-    mkdir(base_.c_str(), 0777);
+    std::filesystem::create_directories(base_);
   } else {
 #if defined(ANDROID) || defined(__ANDROID__)
-    char cwd[PATH_MAX];
-    auto cmdline = fopen("/proc/self/cmdline", "r");
-    fread(cwd, 1, sizeof(cwd), cmdline);
-    fclose(cmdline);
-    std::string android_base_ = "/data/data/" + std::string(cwd) + "/cache";
-    struct stat statbuf;
+    std::string pkg_name;
+    if (std::ifstream cmdline("/proc/self/cmdline"); cmdline) {
+      std::getline(cmdline, pkg_name, '\0');
+    }
+    std::string android_base_ = "/data/data/" + pkg_name + "/cache";
     // Check if application data directory exist. If not exist, usually means 
we run tvm_rpc from
     // adb shell terminal.
-    if (stat(android_base_.data(), &statbuf) == -1 || 
!S_ISDIR(statbuf.st_mode)) {
+    if (!std::filesystem::is_directory(android_base_)) {
       // Tmp directory is always writable for 'shell' user.
       android_base_ = "/data/local/tmp";
     }
     base_ = android_base_ + "/rpc";
-
 #elif !defined(_WIN32)
-    char cwd[PATH_MAX];
-    if (getcwd(cwd, sizeof(cwd))) {
-      base_ = std::string(cwd) + "/rpc";
-    } else {
+    std::error_code ec;
+    base_ = std::filesystem::current_path(ec).string() + "/rpc";
+    if (ec) {
       base_ = "./rpc";
     }
 #else
     base_ = "./rpc";
 #endif
-    mkdir(base_.c_str(), 0777);
+    std::filesystem::create_directories(base_);
   }
+  std::error_code ec;
+  std::filesystem::permissions(
+    base_,
+    std::filesystem::perms::all,
+    std::filesystem::perm_options::replace,
+    ec
+  );

Review Comment:
   Original code used 0777 permission (not portable), so its fine.
   We want to make sure nothing blocks, so full rights R/W/X for these 
temporaries is fine.
   Security is secondary in this case.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to