1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/*
* Copyright (C) 2011-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#ifdef TARGET_WINDOWS
#include <mutex>
#include <sys\stat.h>
#endif
#include "FileItem.h"
#include "NFSDirectory.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/XTimeUtils.h"
#include "utils/log.h"
#ifdef TARGET_WINDOWS
#include <sys\stat.h>
#endif
using namespace XFILE;
#include <limits.h>
#include <nfsc/libnfs.h>
#include <nfsc/libnfs-raw-nfs.h>
#if defined(TARGET_WINDOWS)
#define S_IFLNK 0120000
#define S_ISBLK(m) (0)
#define S_ISSOCK(m) (0)
#define S_ISLNK(m) ((m & S_IFLNK) != 0)
#define S_ISCHR(m) ((m & _S_IFCHR) != 0)
#define S_ISDIR(m) ((m & _S_IFDIR) != 0)
#define S_ISFIFO(m) ((m & _S_IFIFO) != 0)
#define S_ISREG(m) ((m & _S_IFREG) != 0)
#endif
CNFSDirectory::CNFSDirectory(void)
{
gNfsConnection.AddActiveConnection();
}
CNFSDirectory::~CNFSDirectory(void)
{
gNfsConnection.AddIdleConnection();
}
bool CNFSDirectory::GetDirectoryFromExportList(const std::string& strPath, CFileItemList &items)
{
CURL url(strPath);
std::string nonConstStrPath(strPath);
std::list<std::string> exportList=gNfsConnection.GetExportList(url);
for (const std::string& it : exportList)
{
const std::string& currentExport(it);
URIUtils::RemoveSlashAtEnd(nonConstStrPath);
CFileItemPtr pItem(new CFileItem(currentExport));
std::string path(nonConstStrPath + currentExport);
URIUtils::AddSlashAtEnd(path);
pItem->SetPath(path);
pItem->m_dateTime = 0;
pItem->m_bIsFolder = true;
items.Add(pItem);
}
return exportList.empty() ? false : true;
}
bool CNFSDirectory::GetServerList(CFileItemList &items)
{
struct nfs_server_list *srvrs;
struct nfs_server_list *srv;
bool ret = false;
srvrs = nfs_find_local_servers();
for (srv=srvrs; srv; srv = srv->next)
{
std::string currentExport(srv->addr);
CFileItemPtr pItem(new CFileItem(currentExport));
std::string path("nfs://" + currentExport);
URIUtils::AddSlashAtEnd(path);
pItem->m_dateTime=0;
pItem->SetPath(path);
pItem->m_bIsFolder = true;
items.Add(pItem);
ret = true; //added at least one entry
}
free_nfs_srvr_list(srvrs);
return ret;
}
bool CNFSDirectory::ResolveSymlink( const std::string &dirName, struct nfsdirent *dirent, CURL &resolvedUrl)
{
std::unique_lock<CCriticalSection> lock(gNfsConnection);
int ret = 0;
bool retVal = true;
std::string fullpath = dirName;
char resolvedLink[MAX_PATH];
URIUtils::AddSlashAtEnd(fullpath);
fullpath.append(dirent->name);
resolvedUrl.Reset();
resolvedUrl.SetPort(2049);
resolvedUrl.SetProtocol("nfs");
resolvedUrl.SetHostName(gNfsConnection.GetConnectedIp());
ret = nfs_readlink(gNfsConnection.GetNfsContext(), fullpath.c_str(), resolvedLink, MAX_PATH);
if(ret == 0)
{
nfs_stat_64 tmpBuffer = {};
fullpath = dirName;
URIUtils::AddSlashAtEnd(fullpath);
fullpath.append(resolvedLink);
//special case - if link target is absolute it could be even another export
//intervolume symlinks baby ...
if(resolvedLink[0] == '/')
{
//use the special stat function for using an extra context
//because we are inside of a dir traversal
//and just can't change the global nfs context here
//without destroying something...
fullpath = resolvedLink;
resolvedUrl.SetFileName(fullpath);
ret = gNfsConnection.stat(resolvedUrl, &tmpBuffer);
}
else
{
ret = nfs_stat64(gNfsConnection.GetNfsContext(), fullpath.c_str(), &tmpBuffer);
resolvedUrl.SetFileName(gNfsConnection.GetConnectedExport() + fullpath);
}
if (ret != 0)
{
CLog::Log(LOGERROR, "NFS: Failed to stat({}) on link resolve {}", fullpath,
nfs_get_error(gNfsConnection.GetNfsContext()));
retVal = false;
}
else
{
dirent->inode = tmpBuffer.nfs_ino;
dirent->mode = tmpBuffer.nfs_mode;
dirent->size = tmpBuffer.nfs_size;
dirent->atime.tv_sec = tmpBuffer.nfs_atime;
dirent->mtime.tv_sec = tmpBuffer.nfs_mtime;
dirent->ctime.tv_sec = tmpBuffer.nfs_ctime;
//map stat mode to nf3type
if (S_ISBLK(tmpBuffer.nfs_mode))
{
dirent->type = NF3BLK;
}
else if (S_ISCHR(tmpBuffer.nfs_mode))
{
dirent->type = NF3CHR;
}
else if (S_ISDIR(tmpBuffer.nfs_mode))
{
dirent->type = NF3DIR;
}
else if (S_ISFIFO(tmpBuffer.nfs_mode))
{
dirent->type = NF3FIFO;
}
else if (S_ISREG(tmpBuffer.nfs_mode))
{
dirent->type = NF3REG;
}
else if (S_ISLNK(tmpBuffer.nfs_mode))
{
dirent->type = NF3LNK;
}
else if (S_ISSOCK(tmpBuffer.nfs_mode))
{
dirent->type = NF3SOCK;
}
}
}
else
{
CLog::Log(LOGERROR, "Failed to readlink({}) {}", fullpath,
nfs_get_error(gNfsConnection.GetNfsContext()));
retVal = false;
}
return retVal;
}
bool CNFSDirectory::GetDirectory(const CURL& url, CFileItemList &items)
{
// We accept nfs://server/path[/file]]]]
int ret = 0;
std::unique_lock<CCriticalSection> lock(gNfsConnection);
std::string strDirName="";
std::string myStrPath(url.Get());
URIUtils::AddSlashAtEnd(myStrPath); //be sure the dir ends with a slash
if(!gNfsConnection.Connect(url,strDirName))
{
//connect has failed - so try to get the exported filesystems if no path is given to the url
if(url.GetShareName().empty())
{
if(url.GetHostName().empty())
{
return GetServerList(items);
}
else
{
return GetDirectoryFromExportList(myStrPath, items);
}
}
else
{
return false;
}
}
struct nfsdir *nfsdir = NULL;
struct nfsdirent *nfsdirent = NULL;
ret = nfs_opendir(gNfsConnection.GetNfsContext(), strDirName.c_str(), &nfsdir);
if(ret != 0)
{
CLog::Log(LOGERROR, "Failed to open({}) {}", strDirName,
nfs_get_error(gNfsConnection.GetNfsContext()));
return false;
}
lock.unlock();
while((nfsdirent = nfs_readdir(gNfsConnection.GetNfsContext(), nfsdir)) != NULL)
{
struct nfsdirent tmpDirent = *nfsdirent;
std::string strName = tmpDirent.name;
std::string path(myStrPath + strName);
int64_t iSize = 0;
bool bIsDir = false;
//reslove symlinks
if(tmpDirent.type == NF3LNK)
{
CURL linkUrl;
//resolve symlink changes tmpDirent and strName
if(!ResolveSymlink(strDirName,&tmpDirent,linkUrl))
{
continue;
}
path = linkUrl.Get();
}
iSize = tmpDirent.size;
bIsDir = tmpDirent.type == NF3DIR;
if (!StringUtils::EqualsNoCase(strName,".") && !StringUtils::EqualsNoCase(strName,"..")
&& !StringUtils::EqualsNoCase(strName,"lost+found"))
{
CFileItemPtr pItem(new CFileItem(tmpDirent.name));
pItem->m_dateTime =
tmpDirent.mtime.tv_sec != 0 ? tmpDirent.mtime.tv_sec : tmpDirent.ctime.tv_sec;
pItem->m_dwSize = iSize;
if (bIsDir)
{
URIUtils::AddSlashAtEnd(path);
pItem->m_bIsFolder = true;
}
else
{
pItem->m_bIsFolder = false;
}
if (strName[0] == '.')
{
pItem->SetProperty("file:hidden", true);
}
pItem->SetPath(path);
items.Add(pItem);
}
}
lock.lock();
nfs_closedir(gNfsConnection.GetNfsContext(), nfsdir);//close the dir
lock.unlock();
return true;
}
bool CNFSDirectory::Create(const CURL& url2)
{
int ret = 0;
bool success=true;
std::unique_lock<CCriticalSection> lock(gNfsConnection);
std::string folderName(url2.Get());
URIUtils::RemoveSlashAtEnd(folderName);//mkdir fails if a slash is at the end!!!
CURL url(folderName);
folderName = "";
if(!gNfsConnection.Connect(url,folderName))
return false;
ret = nfs_mkdir(gNfsConnection.GetNfsContext(), folderName.c_str());
success = (ret == 0 || -EEXIST == ret);
if(!success)
CLog::Log(LOGERROR, "NFS: Failed to create({}) {}", folderName,
nfs_get_error(gNfsConnection.GetNfsContext()));
return success;
}
bool CNFSDirectory::Remove(const CURL& url2)
{
int ret = 0;
std::unique_lock<CCriticalSection> lock(gNfsConnection);
std::string folderName(url2.Get());
URIUtils::RemoveSlashAtEnd(folderName);//rmdir fails if a slash is at the end!!!
CURL url(folderName);
folderName = "";
if(!gNfsConnection.Connect(url,folderName))
return false;
ret = nfs_rmdir(gNfsConnection.GetNfsContext(), folderName.c_str());
if(ret != 0 && errno != ENOENT)
{
CLog::Log(LOGERROR, "{} - Error( {} )", __FUNCTION__,
nfs_get_error(gNfsConnection.GetNfsContext()));
return false;
}
return true;
}
bool CNFSDirectory::Exists(const CURL& url2)
{
int ret = 0;
std::unique_lock<CCriticalSection> lock(gNfsConnection);
std::string folderName(url2.Get());
URIUtils::RemoveSlashAtEnd(folderName);//remove slash at end or URIUtils::GetFileName won't return what we want...
CURL url(folderName);
folderName = "";
if(!gNfsConnection.Connect(url,folderName))
return false;
nfs_stat_64 info;
ret = nfs_stat64(gNfsConnection.GetNfsContext(), folderName.c_str(), &info);
if (ret != 0)
{
return false;
}
return S_ISDIR(info.nfs_mode) ? true : false;
}
|