scoped_flock.cc revision a59dd80f9f48cb750d329d4d4af2d99d72b484d1
1/*
2 * Copyright (C) 2011 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#include "scoped_flock.h"
18
19#include <sys/file.h>
20#include <sys/stat.h>
21
22#include "base/logging.h"
23#include "base/stringprintf.h"
24#include "base/unix_file/fd_file.h"
25
26namespace art {
27
28bool ScopedFlock::Init(const char* filename, std::string* error_msg) {
29  while (true) {
30    file_.reset(OS::OpenFileWithFlags(filename, O_CREAT | O_RDWR));
31    if (file_.get() == NULL) {
32      *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno));
33      return false;
34    }
35    int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_EX));
36    if (flock_result != 0) {
37      *error_msg = StringPrintf("Failed to lock file '%s': %s", filename, strerror(errno));
38      return false;
39    }
40    struct stat fstat_stat;
41    int fstat_result = TEMP_FAILURE_RETRY(fstat(file_->Fd(), &fstat_stat));
42    if (fstat_result != 0) {
43      *error_msg = StringPrintf("Failed to fstat file '%s': %s", filename, strerror(errno));
44      return false;
45    }
46    struct stat stat_stat;
47    int stat_result = TEMP_FAILURE_RETRY(stat(filename, &stat_stat));
48    if (stat_result != 0) {
49      PLOG(WARNING) << "Failed to stat, will retry: " << filename;
50      // ENOENT can happen if someone racing with us unlinks the file we created so just retry.
51      continue;
52    }
53    if (fstat_stat.st_dev != stat_stat.st_dev || fstat_stat.st_ino != stat_stat.st_ino) {
54      LOG(WARNING) << "File changed while locking, will retry: " << filename;
55      continue;
56    }
57    return true;
58  }
59}
60
61bool ScopedFlock::Init(File* file, std::string* error_msg) {
62  file_.reset(new File(dup(file->Fd())));
63  if (file_->Fd() == -1) {
64    file_.reset();
65    *error_msg = StringPrintf("Failed to duplicate open file '%s': %s",
66                              file->GetPath().c_str(), strerror(errno));
67    return false;
68  }
69  if (0 != TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_EX))) {
70    file_.reset();
71    *error_msg = StringPrintf("Failed to lock file '%s': %s", file->GetPath().c_str(), strerror(errno));
72    return false;
73  }
74  return true;
75}
76
77File* ScopedFlock::GetFile() {
78  CHECK(file_.get() != NULL);
79  return file_.get();
80}
81
82bool ScopedFlock::HasFile() {
83  return file_.get() != nullptr;
84}
85
86ScopedFlock::ScopedFlock() { }
87
88ScopedFlock::~ScopedFlock() {
89  if (file_.get() != NULL) {
90    int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN));
91    CHECK_EQ(0, flock_result);
92  }
93}
94
95}  // namespace art
96