/
NFSDirectory.cpp
206 lines (165 loc) · 5.85 KB
/
NFSDirectory.cpp
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
/*
* Copyright (C) 2011 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "system.h"
#ifdef HAS_FILESYSTEM_NFS
#include "DllLibNfs.h"
#include "NFSDirectory.h"
#include "FileItem.h"
#include "utils/log.h"
#include "utils/URIUtils.h"
#include "threads/SingleLock.h"
using namespace XFILE;
using namespace std;
CNFSDirectory::CNFSDirectory(void)
{
gNfsConnection.AddActiveConnection();
}
CNFSDirectory::~CNFSDirectory(void)
{
gNfsConnection.AddIdleConnection();
}
bool CNFSDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
{
// We accept nfs://server/path[/file]]]]
int ret = 0;
FILETIME fileTime, localTime;
CSingleLock lock(gNfsConnection);
CURL url(strPath);
if(!gNfsConnection.Connect(url))
{
return false;
}
CStdString strDirName="//";//relative to the strPath we connected - we want to get the "/" directory then
vector<CStdString> vecEntries;
struct nfsdir *nfsdir = NULL;
struct nfsdirent *nfsdirent = NULL;
ret = gNfsConnection.GetImpl()->nfs_opendir_sync(gNfsConnection.GetNfsContext(), strDirName.c_str(), &nfsdir);
if(ret != 0)
{
CLog::Log(LOGERROR, "Failed to open(%s) %s\n", strDirName.c_str(), gNfsConnection.GetImpl()->nfs_get_error(gNfsConnection.GetNfsContext()));
return false;
}
lock.Leave();
while((nfsdirent = gNfsConnection.GetImpl()->nfs_readdir(gNfsConnection.GetNfsContext(), nfsdir)) != NULL)
{
vecEntries.push_back(nfsdirent->name);
}
lock.Enter();
gNfsConnection.GetImpl()->nfs_closedir(gNfsConnection.GetNfsContext(), nfsdir);//close the dir
lock.Leave();
for (size_t i=0; i<vecEntries.size(); i++)
{
CStdString strName = vecEntries[i];
if (!strName.Equals(".") && !strName.Equals("..")
&& !strName.Equals("lost+found"))
{
int64_t iSize = 0;
bool bIsDir = false;
int64_t lTimeDate = 0;
struct stat info = {0};
CStdString strFullName = strDirName + strName;
lock.Enter();
ret = gNfsConnection.GetImpl()->nfs_stat_sync(gNfsConnection.GetNfsContext(), strFullName.c_str(), &info);
lock.Leave();
if( ret == 0 )
{
bIsDir = (info.st_mode & S_IFDIR) ? true : false;
lTimeDate = info.st_mtime;
if(lTimeDate == 0) // if modification date is missing, use create date
lTimeDate = info.st_ctime;
iSize = info.st_size;
}
else
CLog::Log(LOGERROR, "NFS; Failed to stat(%s) %s\n", strFullName.c_str(), gNfsConnection.GetImpl()->nfs_get_error(gNfsConnection.GetNfsContext()));
LONGLONG ll = Int32x32To64(lTimeDate & 0xffffffff, 10000000) + 116444736000000000ll;
fileTime.dwLowDateTime = (DWORD) (ll & 0xffffffff);
fileTime.dwHighDateTime = (DWORD)(ll >> 32);
FileTimeToLocalFileTime(&fileTime, &localTime);
CFileItemPtr pItem(new CFileItem(strName));
pItem->m_strPath = strPath + strName;
pItem->m_dateTime=localTime;
if (bIsDir)
{
URIUtils::AddSlashAtEnd(pItem->m_strPath);
pItem->m_bIsFolder = true;
}
else
{
pItem->m_bIsFolder = false;
pItem->m_dwSize = iSize;
}
items.Add(pItem);
}
}
return true;
}
bool CNFSDirectory::Create(const char* strPath)
{
int ret = 0;
CSingleLock lock(gNfsConnection);
CStdString folderName(strPath);
URIUtils::RemoveSlashAtEnd(folderName);//mkdir fails if a slash is at the end!!!
CURL url(folderName);
folderName = "//" + URIUtils::GetFileName(folderName);
if(!gNfsConnection.Connect(url))
return false;
ret = gNfsConnection.GetImpl()->nfs_mkdir_sync(gNfsConnection.GetNfsContext(), folderName.c_str());
if(ret != 0)
CLog::Log(LOGERROR, "NFS: Failed to create(%s) %s\n", folderName.c_str(), gNfsConnection.GetImpl()->nfs_get_error(gNfsConnection.GetNfsContext()));
return (ret == 0 || EEXIST == ret);
}
bool CNFSDirectory::Remove(const char* strPath)
{
int ret = 0;
CSingleLock lock(gNfsConnection);
CStdString folderName(strPath);
URIUtils::RemoveSlashAtEnd(folderName);//rmdir fails if a slash is at the end!!!
CURL url(folderName);
folderName = "//" + URIUtils::GetFileName(folderName);
if(!gNfsConnection.Connect(url))
return false;
ret = gNfsConnection.GetImpl()->nfs_rmdir_sync(gNfsConnection.GetNfsContext(), folderName.c_str());
if(ret != 0 && errno != ENOENT)
{
CLog::Log(LOGERROR, "%s - Error( %s )", __FUNCTION__, gNfsConnection.GetImpl()->nfs_get_error(gNfsConnection.GetNfsContext()));
return false;
}
return true;
}
bool CNFSDirectory::Exists(const char* strPath)
{
int ret = 0;
CSingleLock lock(gNfsConnection);
CStdString folderName(strPath);
URIUtils::RemoveSlashAtEnd(folderName);//remove slash at end or URIUtils::GetFileName won't return what we want...
CURL url(folderName);
folderName = "//" + URIUtils::GetFileName(folderName);
if(!gNfsConnection.Connect(url))
return false;
struct stat info;
ret = gNfsConnection.GetImpl()->nfs_stat_sync(gNfsConnection.GetNfsContext(), folderName.c_str(), &info);
if (ret != 0)
{
return false;
}
return (info.st_mode & S_IFDIR) ? true : false;
}
#endif