GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/mmap_file.cc
Date: 2026-09-27 02:40:09
Exec Total Coverage
Lines: 33 49 67.3%
Branches: 13 39 33.3%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Some common functions.
5 */
6
7 #include "mmap_file.h"
8
9 #include <cstddef>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sys/mman.h>
13 #include <unistd.h>
14 #include <string>
15
16 #include <cassert>
17
18 #include "util/logging.h"
19 #include "util/platform.h"
20
21 using namespace std; // NOLINT
22
23 #ifdef CVMFS_NAMESPACE_GUARD
24 namespace CVMFS_NAMESPACE_GUARD {
25 #endif
26
27
28 1368 MemoryMappedFile::MemoryMappedFile(const std::string &file_path)
29 1368 : file_path_(file_path)
30 1368 , file_descriptor_(-1)
31 1368 , mapped_file_(NULL)
32 1368 , mapped_size_(0)
33 1368 , mapped_(false) { }
34
35 2734 MemoryMappedFile::~MemoryMappedFile() {
36
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1367 times.
1367 if (IsMapped()) {
37 ✗ Unmap();
38 }
39 1367 }
40
41 1367 bool MemoryMappedFile::Map() {
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1367 times.
1367 assert(!mapped_);
43
44 // open the file
45 int fd;
46
2/4
✓ Branch 2 taken 1367 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1367 times.
1367 if ((fd = open(file_path_.c_str(), O_RDONLY, 0)) == -1) {
47 ✗ LogCvmfs(kLogUtility, kLogStderr, "failed to open %s (%d)",
48 ✗ file_path_.c_str(), errno);
49 ✗ return false;
50 }
51
52 // get file size
53 platform_stat64 filesize;
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1367 times.
1367 if (platform_fstat(fd, &filesize) != 0) {
55 ✗ LogCvmfs(kLogUtility, kLogStderr, "failed to fstat %s (%d)",
56 ✗ file_path_.c_str(), errno);
57 ✗ close(fd);
58 ✗ return false;
59 }
60
61 // check if the file is empty and 'pretend' that the file is mapped
62 // --> buffer will then look like a buffer without any size...
63 1367 void *mapping = NULL;
64
1/2
✓ Branch 0 taken 1367 times.
✗ Branch 1 not taken.
1367 if (filesize.st_size > 0) {
65 // map the given file into memory
66 1367 mapping = mmap(NULL, filesize.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1367 times.
1367 if (mapping == MAP_FAILED) { // NOLINT(performance-no-int-to-ptr)
68 ✗ LogCvmfs(kLogUtility, kLogStderr,
69 "failed to mmap %s (file size: %ld) "
70 "(errno: %d)",
71 ✗ file_path_.c_str(), filesize.st_size, errno);
72 ✗ close(fd);
73 ✗ return false;
74 }
75 }
76
77 // save results
78 1367 mapped_file_ = static_cast<unsigned char *>(mapping);
79 1367 file_descriptor_ = fd;
80 1367 mapped_size_ = filesize.st_size;
81 1367 mapped_ = true;
82
1/2
✓ Branch 2 taken 1367 times.
✗ Branch 3 not taken.
1367 LogCvmfs(kLogUtility, kLogVerboseMsg, "mmap'ed %s", file_path_.c_str());
83 1367 return true;
84 }
85
86 1367 void MemoryMappedFile::Unmap() {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1367 times.
1367 assert(mapped_);
88
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1367 times.
1367 if (mapped_file_ == NULL) {
90 ✗ return;
91 }
92
93 // unmap the previously mapped file
94 1367 if ((munmap(static_cast<void *>(mapped_file_), mapped_size_) != 0)
95
3/6
✓ Branch 0 taken 1367 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1367 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1367 times.
1367 || (close(file_descriptor_) != 0)) {
96 ✗ LogCvmfs(kLogUtility, kLogStderr, "failed to unmap %s", file_path_.c_str());
97 ✗ const bool munmap_failed = false;
98 ✗ assert(munmap_failed);
99 }
100
101 // reset (resettable) data
102 1367 mapped_file_ = NULL;
103 1367 file_descriptor_ = -1;
104 1367 mapped_size_ = 0;
105 1367 mapped_ = false;
106 1367 LogCvmfs(kLogUtility, kLogVerboseMsg, "munmap'ed %s", file_path_.c_str());
107 }
108
109 #ifdef CVMFS_NAMESPACE_GUARD
110 } // namespace CVMFS_NAMESPACE_GUARD
111 #endif
112