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"
15 #include "utils/CharsetConverter.h"
16 #include "utils/StringUtils.h"
17 #include "utils/URIUtils.h"
21 using namespace XFILE
;
23 CFTPDirectory::CFTPDirectory(void) = default;
24 CFTPDirectory::~CFTPDirectory(void) = default;
26 bool CFTPDirectory::GetDirectory(const CURL
& url2
, CFileItemList
&items
)
32 std::string path
= url
.GetFileName();
33 if( !path
.empty() && !StringUtils::EndsWith(path
, "/") )
36 url
.SetFileName(path
);
39 if (!reader
.Open(url
))
42 bool serverNotUseUTF8
= url
.GetProtocolOption("utf8") == "0";
44 char buffer
[MAX_PATH
+ 1024];
45 while( reader
.ReadString(buffer
, sizeof(buffer
)) )
47 std::string strBuffer
= buffer
;
49 StringUtils::RemoveCRLF(strBuffer
);
52 if (parse
.FTPParse(strBuffer
))
54 if( parse
.getName().length() == 0 )
57 if( parse
.getFlagtrycwd() == 0 && parse
.getFlagtryretr() == 0 )
62 name
.assign(parse
.getName());
64 if( name
== ".." || name
== "." )
67 // server returned filename could in utf8 or non-utf8 encoding
68 // we need utf8, so convert it to utf8 anyway
69 g_charsetConverter
.unknownToUTF8(name
);
71 // convert got empty result, ignore it
75 if (serverNotUseUTF8
|| name
!= parse
.getName())
76 // non-utf8 name path, tag it with protocol option.
77 // then we can talk to server with the same encoding in CurlFile according to this tag.
78 url
.SetProtocolOption("utf8", "0");
80 url
.RemoveProtocolOption("utf8");
82 CFileItemPtr
pItem(new CFileItem(name
));
84 pItem
->m_bIsFolder
= parse
.getFlagtrycwd() != 0;
85 std::string filePath
= path
+ name
;
86 if (pItem
->m_bIsFolder
)
87 URIUtils::AddSlashAtEnd(filePath
);
89 /* qualify the url with host and all */
90 url
.SetFileName(filePath
);
91 pItem
->SetPath(url
.Get());
93 pItem
->m_dwSize
= parse
.getSize();
94 pItem
->m_dateTime
=parse
.getTime();
103 bool CFTPDirectory::Exists(const CURL
& url
)
105 // make sure ftp dir ends with slash,
106 // curl need to known it's a dir to check ftp directory existence.
107 std::string file
= url
.Get();
108 URIUtils::AddSlashAtEnd(file
);
112 return ftp
.Exists(url2
);