adbncfs  0.9.1
mountInfo.cpp
Go to the documentation of this file.
1 /*
2  * $Id: mountInfo.cpp 4 2015-12-04 13:55:56Z wejaeger $
3  *
4  * File: mountInfo.cpp
5  * Author: Werner Jaeger
6  *
7  * Created on November 28, 2015, 10:38 AM
8  *
9  * Copyright 2015 Werner Jaeger.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 #include <string.h>
25 #include "mountInfo.h"
26 
49 MountInfo::Entry::Entry(const char* pcMountLine)
50 {
51  parseMountInfo(pcMountLine);
52 }
53 
60 {
61  return(m_mountOptions.find("ro") != m_mountOptions.cend());
62 }
63 
70 {
71  return(m_mountOptions.find("noexec") != m_mountOptions.cend());
72 }
73 
94 void MountInfo::Entry::parseMountInfo(const char* pcMountLine)
95 {
96  if (pcMountLine)
97  {
98  const int iLen(::strlen(pcMountLine) + 1);
99 
100  char acTokens[iLen];
101  ::strncpy(acTokens, pcMountLine, iLen);
102 
103  char* pcSave;
104  char* pch = ::strtok_r(acTokens, " ", &pcSave);
105  if (pch)
106  m_strFileSystem = pch;
107 
108  pch = ::strtok_r(NULL, " ", &pcSave);
109  if (pch && ::strcmp(pch, "on") == 0)
110  {
111  pch = ::strtok_r(NULL, " ", &pcSave);
112  if (pch)
113  {
114  m_strMountPoint = pch;
115  pch = ::strtok_r(NULL, " ", &pcSave);
116  if (pch && ::strcmp(pch, "type") == 0)
117  {
118  pch = ::strtok_r(NULL, " ", &pcSave);
119  if (pch)
120  {
121  m_strDevType = pch;
122  pch = ::strtok_r(NULL, "(", &pcSave);
123  }
124  }
125  }
126  }
127 
128  if (pch)
129  {
130  pch = ::strtok_r(pch, ",)", &pcSave);
131  while (pch != NULL)
132  {
133  m_mountOptions.insert(pch);
134  pch = ::strtok_r(NULL, ",)", &pcSave);
135  }
136  }
137  }
138 }
139 
163 MountInfo::MountInfo(const deque<string>& mountInfo)
164 {
165  for(auto it = mountInfo.begin(); it != mountInfo.end(); ++it)
166  {
167  Entry entry(it->c_str());
168  m_Entries.insert(make_pair(entry.mountPoint(), entry));
169  }
170 }
171 
180 const MountInfo::Entry* MountInfo::mountPoint(const char* pcPath) const
181 {
182  string strParent(pcPath);
183  const Entry* pMountPointEntry(findMountPointEntry(strParent));
184 
185  while (!pMountPointEntry && strParent != "/")
186  {
187  strParent = parent(strParent);
188  pMountPointEntry = findMountPointEntry(strParent);
189  }
190 
191  return(pMountPointEntry);
192 }
193 
203 bool MountInfo::isMountedRo(const char* pcPath) const
204 {
205  const Entry* pEntry(mountPoint(pcPath));
206 
207  return(pEntry ? pEntry->isMountedRo() : false);
208 }
209 
218 bool MountInfo::isMountedNoexec(const char* pcPath) const
219 {
220  const MountInfo::Entry* pEntry(mountPoint(pcPath));
221 
222  return(pEntry ? pEntry->isMountedNoexec() : false);
223 }
224 
233 const MountInfo::Entry* MountInfo::findMountPointEntry(const string& strPath) const
234 {
235  const Entry* pMountPointEntry(NULL);
236 
237  for (auto it = m_Entries.begin(); !pMountPointEntry && it != m_Entries.end(); ++it)
238  {
239  if (it->first == strPath)
240  pMountPointEntry = &it->second;
241  }
242 
243  return(pMountPointEntry);
244 }
245 
256 const string MountInfo::parent(const string& strPath)
257 {
258  string strParent(strPath);
259  const size_t uiLen(strParent.length());
260 
261  /* if strPath == / we are done*/
262  if (uiLen != 1 || strParent[0] != '/')
263  {
264  if (uiLen > 0)
265  {
267  if (strParent[uiLen - 1] == '/')
268  strParent = strParent.substr(0, uiLen - 1);
269 
270  const std::size_t uiPos(strParent.rfind('/'));
271  if (uiPos != string::npos)
272  strParent = strParent.substr(0, uiPos == 0 ? 1 : uiPos);
273 
274  if (strParent == strPath)
275  strParent = ".";
276  }
277  else
278  strParent = ".";
279  }
280 
281  return(strParent);
282 }
283 
284 
285 
const Entry * mountPoint(const char *pcPath) const
Returns the mount point of a given path.
Definition: mountInfo.cpp:180
bool isMountedNoexec(const char *pcPath) const
Test if the given path is located on a mount point that is mounted with the noexec option...
Definition: mountInfo.cpp:218
void parseMountInfo(const char *pcMountLine)
Parse the given mount line and store the results in the corresponding member variables.
Definition: mountInfo.cpp:94
A class to manage a line obtained from the output of the busybox applet mount command.
Definition: mountInfo.h:48
static const string parent(const string &strPath)
Returns the parent pathname string of the given strPath.
Definition: mountInfo.cpp:256
bool isMountedNoexec() const
Test this mount point is mounted wit no execution option.
Definition: mountInfo.cpp:69
bool isMountedRo() const
Test this mount point is mounted read only.
Definition: mountInfo.cpp:59
bool isMountedRo(const char *pcPath) const
Test if the given path is located on a mount point that is mounted read only.
Definition: mountInfo.cpp:203
const Entry * findMountPointEntry(const string &strPath) const
Lookup if there is a mount point entry with the given path.
Definition: mountInfo.cpp:233
map< string, Entry > m_Entries
Definition: mountInfo.h:104