blob: 6cd3bd6cdc87fbd6820997c9833f85329948383e [file] [log] [blame]
Vadim Ioseviche76832c2017-11-05 09:09:14 +02001/*
2* Copyright (c) 2017, The Linux Foundation. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#ifdef __linux
31#include <sys/ioctl.h> // for struct ifreq
32#include <net/if.h> // for struct ifreq
33#include <arpa/inet.h> // for the declaration of inet_ntoa
34#include <netinet/in.h> // for struct sockaddr_in
35#include <stdlib.h>
36#include <string.h>
37#include <cerrno>
38#include <sys/stat.h> //added for config- template path
39#include <unistd.h>
40#include <sys/types.h>
41#include <dirent.h>
42#include <stdio.h>
43
44#elif _WINDOWS
45#include <windows.h>
46#include <KnownFolders.h>
47#include <ShlObj.h>
48#include <Shlwapi.h>
49#include <comutil.h> //for _bstr_t (used in the string conversion)
50#pragma comment(lib, "comsuppw")
51
52#else
53#include <dirent.h>
54#endif
55
56#ifdef __ANDROID__
57#include <sys/stat.h> //added for config- template path
58#include <unistd.h> //added for config- template path
59#include <sys/types.h>
60#endif
61
62#include <iostream>
63#include <fstream>
64#include <sstream>
65#include "DebugLogger.h"
66#include "FileSystemOsAbstraction.h"
67
68const std::string FileSystemOsAbstraction::LOCAL_HOST_IP = "127.0.0.1";
69
70bool FileSystemOsAbstraction::FindEthernetInterface(struct ifreq& ifr, int& fd)
71{
72#ifdef __linux
73 fd = socket(AF_INET, SOCK_DGRAM, 0);
74 if (fd < 0)
75 {
76 LOG_WARNING << "Failed to get host's IP address and broadcast IP address" << std::endl;
77 return false;
78 }
79
80 for (int i = 0; i < 100; i++)
81 {
82 snprintf(ifr.ifr_name, IFNAMSIZ - 1, "eth%d", i);
83
84 if (ioctl(fd, SIOCGIFADDR, &ifr) >= 0)
85 {
86 return true;
87 }
88 }
89#endif
90 return false;
91}
92
93HostIps FileSystemOsAbstraction::GetHostIps()
94{
95#ifdef __linux
96 HostIps hostIps;
97 int fd;
98 struct ifreq ifr;
99
100 ifr.ifr_addr.sa_family = AF_INET; // IP4V
101
102 // Get IP address according to OS
103 if (FindEthernetInterface(ifr, fd))
104 {
105 LOG_INFO << "Linux OS" << std::endl;
106 }
107 else
108 {
109 snprintf(ifr.ifr_name, IFNAMSIZ - 1, "br-lan");
110 if (ioctl(fd, SIOCGIFADDR, &ifr) >= 0)
111 {
112 LOG_INFO << "OpenWRT OS" << std::endl;
113 }
114 else
115 {
116 // Probably Android OS
117 LOG_INFO << "Android OS (no external IP Adress)" << std::endl;
118 hostIps.m_ip = FileSystemOsAbstraction::LOCAL_HOST_IP;
119 hostIps.m_broadcastIp = FileSystemOsAbstraction::LOCAL_HOST_IP;
120 return hostIps;
121 }
122 }
123
124 hostIps.m_ip = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
125
126 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
127 {
128 LOG_WARNING << "Failed to get broadcast IP" << std::endl;
129 return hostIps;
130 }
131 hostIps.m_broadcastIp = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
132 LOG_DEBUG << "Host's IP address is " << hostIps.m_ip << std::endl;
133 LOG_DEBUG << "Broadcast IP address is " << hostIps.m_broadcastIp << std::endl;
134
135 close(fd);
136 return hostIps;
137#else
138 HostIps empty;
139 empty.m_broadcastIp = "10.18.172.155";
140 return empty;
141#endif
142}
143
144bool FileSystemOsAbstraction::ReadFile(string fileName, string& data)
145{
146 ifstream fd(fileName.c_str());
147 if (!fd.good()) // file doesn't exist
148 {
149 data = "";
150 return false;
151 }
152
153 fd.open(fileName.c_str());
154 stringstream content;
155 content << fd.rdbuf();
156 fd.close();
157 data = content.str();
158 return true;
159}
160
161bool FileSystemOsAbstraction::WriteFile(const string& fileName, const string& content)
162{
163 std::ofstream fd(fileName.c_str());
164 if (!fd.is_open())
165 {
166 LOG_WARNING << "Failed to open file: " << fileName << std::endl;
167 return false;
168 }
169 fd << content;
170 if (fd.bad())
171 {
172 LOG_WARNING << "Failed to write to file: " << fileName << std::endl;
173 fd.close();
174 return false;
175 }
176 fd.close();
177 return true;
178}
179
180bool FileExist(const std::string& Name)
181{
182#ifdef _WINDOWS
183 struct _stat buf;
184 int Result = _stat(Name.c_str(), &buf);
185#else
186 struct stat buf;
187 int Result = stat(Name.c_str(), &buf);
188#endif
189 return Result == 0;
190}
191
192
193
194string FileSystemOsAbstraction::GetConfigurationFilesLocation()
195{
196 stringstream path;
197
198 //should check __ANDROID__ first since __LINUX flag also set in Android
199#ifdef __ANDROID__
200 std::string t_path = "/data/vendor/wifi/host_manager_11ad/";
201 if (!FileExist(t_path))
202 {
203 path << "/data/host_manager_11ad/";
204 }
205 else
206 {
207 path << t_path;
208 }
209#elif __linux
210 return "/etc/host_manager_11ad/";
211#elif _WINDOWS //windows
212 LPWSTR lpwstrPath = NULL;
213 // Get the ProgramData folder path of windows
214 HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramData, 0, NULL, &lpwstrPath);
215 if (SUCCEEDED(result))
216 {
217 // Convert the path to string
218 std::wstring wpath(lpwstrPath);
219 std::string strPath = std::string(wpath.cbegin(), wpath.cend());
220 CoTaskMemFree(lpwstrPath);
221 path << strPath << "\\Wilocity\\host_manager_11ad\\";
222 }
223#else //OS3
224 return "/etc/host_manager_11ad/";
225#endif // __linux
226 return path.str();
227}
228
229std::string FileSystemOsAbstraction::GetTemplateFilesLocation()
230{
231 stringstream path;
232
233 //should check __ANDROID__ first since __LINUX flag also set in Android
234
235
236#ifdef __ANDROID__
237 std::string t_path = "/vendor/etc/wifi/host_manager_11ad/";
238 if (!FileExist(t_path))
239 {
240 path << "/data/host_manager_11ad/";
241 }
242 else
243 {
244 path << t_path;
245 }
246
247#elif __linux
248 return "/etc/host_manager_11ad/";
249#elif _WINDOWS
250 path << "..\\OnTargetUI\\";
251#else //OS3
252 return "/etc/host_manager_11ad/";
253#endif // __linux
254 return path.str();
255}
256
257string FileSystemOsAbstraction::GetDirectoriesDilimeter()
258{
259#ifndef _WINDOWS
260 return "/";
261#else
262 return "\\";
263#endif
264}
265
266bool FileSystemOsAbstraction::ReadHostOsAlias(string& alias)
267{
268#ifdef __linux
269 if (!ReadFile("/etc/hostname", alias))
270 {
271 alias = "";
272 return false;
273 }
274 return true;
275#else
276 alias = "";
277 return false;
278#endif // __linux
279}
280
281bool FileSystemOsAbstraction::DoesFolderExist(string path)
282{
283#ifndef _WINDOWS
284 DIR* pDir = opendir(path.c_str());
285 if (pDir != NULL)
286 {
287 (void)closedir(pDir);
288 return true;
289 }
290 return false;
291#else
292 DWORD fileAttributes = GetFileAttributesA(path.c_str());
293 if (INVALID_FILE_ATTRIBUTES == fileAttributes) // no such path
294 {
295 return false;
296 }
297 if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY)
298 {
299 return true; // given path is a directory
300 }
301 return false; // given path isn't a directory
302#endif
303}
304
305bool FileSystemOsAbstraction::IsFileExists(string path)
306{
307 ifstream f(path.c_str());
308 return f.good();
309}
310
311bool FileSystemOsAbstraction::CreateFolder(string path)
312{
313#ifndef _WINDOWS
314 system(("mkdir " + path).c_str());
315 return true;
316#else
317 std::wstring wpath = std::wstring(path.cbegin(), path.cend());
318 return CreateDirectory(wpath.c_str(), nullptr) == TRUE;
319#endif
320}
321
322bool FileSystemOsAbstraction::MoveFileToNewLocation(string oldFileLocation, string newFileLocation)
323{
324#ifndef _WINDOWS
325 system(("mv " + oldFileLocation + " " + newFileLocation).c_str());
326 return true;
327#else
328 std::wstring wOldPath = std::wstring(oldFileLocation.cbegin(), oldFileLocation.cend());
329 std::wstring wNewPath = std::wstring(newFileLocation.cbegin(), newFileLocation.cend());
330 return MoveFile(wOldPath.c_str(), wNewPath.c_str()) == TRUE;
331#endif
332}