Home | History | Annotate | Download | only in evdev
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "TestHelpers"
     18 #define LOG_NDEBUG 0
     19 
     20 #include "TestHelpers.h"
     21 
     22 #include <dirent.h>
     23 #include <fcntl.h>
     24 #include <stdlib.h>
     25 #include <sys/stat.h>
     26 #include <sys/types.h>
     27 #include <unistd.h>
     28 
     29 #include <utils/Log.h>
     30 
     31 namespace android {
     32 
     33 static const char kTmpDirTemplate[] = "/data/local/tmp/XXXXXX";
     34 
     35 TempFile::TempFile(const char* path) {
     36     bool needTrailingSlash = path[strlen(path) - 1] != '/';
     37     // name = path + XXXXXX + \0
     38     size_t nameLen = strlen(path) + 6 + 1;
     39     if (needTrailingSlash) nameLen++;
     40 
     41     mName = new char[nameLen];
     42     strcpy(mName, path);
     43     if (needTrailingSlash) {
     44         strcat(mName, "/");
     45     }
     46     strcat(mName, "XXXXXX");
     47     mName = mktemp(mName);
     48     LOG_FATAL_IF(mName == nullptr, "could not create temp filename %s. errno=%d", mName, errno);
     49 
     50     int result = TEMP_FAILURE_RETRY(mkfifo(mName, S_IRWXU));
     51     LOG_FATAL_IF(result < 0, "could not create fifo %s. errno=%d", mName, errno);
     52 
     53     mFd = TEMP_FAILURE_RETRY(open(mName, O_RDWR | O_NONBLOCK));
     54     LOG_FATAL_IF(mFd < 0, "could not open fifo %s. errno=%d", mName, errno);
     55 }
     56 
     57 TempFile::~TempFile() {
     58     if (unlink(mName) < 0) {
     59         ALOGE("could not unlink %s. errno=%d", mName, errno);
     60     }
     61     if (close(mFd) < 0) {
     62         ALOGE("could not close %d. errno=%d", mFd, errno);
     63     }
     64     delete[] mName;
     65 }
     66 
     67 TempDir::TempDir() {
     68     mName = new char[sizeof(kTmpDirTemplate)];
     69     strcpy(mName, kTmpDirTemplate);
     70     mName = mkdtemp(mName);
     71     LOG_FATAL_IF(mName == nullptr, "could not allocate tempdir %s", mName);
     72 }
     73 
     74 TempDir::~TempDir() {
     75     auto tmpDir = opendir(mName);
     76     while (auto entry = readdir(tmpDir)) {
     77         if (strcmp(entry->d_name, ".") == 0 ||
     78             strcmp(entry->d_name, "..") == 0) {
     79             continue;
     80         }
     81         ALOGD("stale file %s, removing", entry->d_name);
     82         unlink(entry->d_name);
     83     }
     84     closedir(tmpDir);
     85     rmdir(mName);
     86     delete mName;
     87 }
     88 
     89 TempFile* TempDir::newTempFile() {
     90     return new TempFile(mName);
     91 }
     92 
     93 }  // namespace android
     94