GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/mmap_file.cc
Date: 2026-05-03 02:36:16
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 863 MemoryMappedFile::MemoryMappedFile(const std::string &file_path)
29 863 : file_path_(file_path)
30 863 , file_descriptor_(-1)
31 863 , mapped_file_(NULL)
32 863 , mapped_size_(0)
33 863 , mapped_(false) { }
34
35 1724 MemoryMappedFile::~MemoryMappedFile() {
36
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 862 times.
862 if (IsMapped()) {
37 Unmap();
38 }
39 862 }
40
41 862 bool MemoryMappedFile::Map() {
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 862 times.
862 assert(!mapped_);
43
44 // open the file
45 int fd;
46
2/4
✓ Branch 2 taken 862 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 862 times.
862 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 862 times.
862 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 862 void *mapping = NULL;
64
1/2
✓ Branch 0 taken 862 times.
✗ Branch 1 not taken.
862 if (filesize.st_size > 0) {
65 // map the given file into memory
66 862 mapping = mmap(NULL, filesize.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 862 times.
862 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 862 mapped_file_ = static_cast<unsigned char *>(mapping);
79 862 file_descriptor_ = fd;
80 862 mapped_size_ = filesize.st_size;
81 862 mapped_ = true;
82
1/2
✓ Branch 2 taken 862 times.
✗ Branch 3 not taken.
862 LogCvmfs(kLogUtility, kLogVerboseMsg, "mmap'ed %s", file_path_.c_str());
83 862 return true;
84 }
85
86 862 void MemoryMappedFile::Unmap() {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 862 times.
862 assert(mapped_);
88
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 862 times.
862 if (mapped_file_ == NULL) {
90 return;
91 }
92
93 // unmap the previously mapped file
94 862 if ((munmap(static_cast<void *>(mapped_file_), mapped_size_) != 0)
95
3/6
✓ Branch 0 taken 862 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 862 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 862 times.
862 || (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 862 mapped_file_ = NULL;
103 862 file_descriptor_ = -1;
104 862 mapped_size_ = 0;
105 862 mapped_ = false;
106 862 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