2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "FTPDirectory.h"
14 #include "FileItemList.h"
16 #include "filesystem/IFile.h"
17 #include "utils/CharsetConverter.h"
18 #include "utils/StringUtils.h"
19 #include "utils/URIUtils.h"
23 using namespace XFILE
;
25 CFTPDirectory::CFTPDirectory(void) = default;
26 CFTPDirectory::~CFTPDirectory(void) = default;
28 bool CFTPDirectory::GetDirectory(const CURL
& url2
, CFileItemList
&items
)
34 std::string path
= url
.GetFileName();
35 if( !path
.empty() && !StringUtils::EndsWith(path
, "/") )
38 url
.SetFileName(path
);
41 if (!reader
.Open(url
))
44 bool serverNotUseUTF8
= url
.GetProtocolOption("utf8") == "0";
46 char buffer
[MAX_PATH
+ 1024];
47 while (reader
.ReadLine(buffer
, sizeof(buffer
)).code
== IFile::ReadLineResult::OK
)
49 std::string strBuffer
= buffer
;
51 StringUtils::RemoveCRLF(strBuffer
);
54 if (parse
.FTPParse(strBuffer
))
56 if( parse
.getName().length() == 0 )
59 if( parse
.getFlagtrycwd() == 0 && parse
.getFlagtryretr() == 0 )
64 name
.assign(parse
.getName());
66 if( name
== ".." || name
== "." )
69 // server returned filename could in utf8 or non-utf8 encoding
70 // we need utf8, so convert it to utf8 anyway
71 g_charsetConverter
.unknownToUTF8(name
);
73 // convert got empty result, ignore it
77 if (serverNotUseUTF8
|| name
!= parse
.getName())
78 // non-utf8 name path, tag it with protocol option.
79 // then we can talk to server with the same encoding in CurlFile according to this tag.
80 url
.SetProtocolOption("utf8", "0");
82 url
.RemoveProtocolOption("utf8");
84 CFileItemPtr
pItem(new CFileItem(name
));
86 pItem
->m_bIsFolder
= parse
.getFlagtrycwd() != 0;
87 std::string filePath
= path
+ name
;
88 if (pItem
->m_bIsFolder
)
89 URIUtils::AddSlashAtEnd(filePath
);
91 /* qualify the url with host and all */
92 url
.SetFileName(filePath
);
93 pItem
->SetPath(url
.Get());
95 pItem
->m_dwSize
= parse
.getSize();
96 pItem
->m_dateTime
=parse
.getTime();
105 bool CFTPDirectory::Exists(const CURL
& url
)
107 // make sure ftp dir ends with slash,
108 // curl need to known it's a dir to check ftp directory existence.
109 std::string file
= url
.Get();
110 URIUtils::AddSlashAtEnd(file
);
114 return ftp
.Exists(url2
);