Merge pull request #26362 from ksooo/estuary-rework-pvr-info-dialog
[xbmc.git] / xbmc / filesystem / FTPDirectory.cpp
blob51af9e5a649ecc096cbd1b1ccfdac8231ccaee07
1 /*
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.
7 */
9 #include "FTPDirectory.h"
11 #include "CurlFile.h"
12 #include "FTPParse.h"
13 #include "FileItem.h"
14 #include "FileItemList.h"
15 #include "URL.h"
16 #include "filesystem/IFile.h"
17 #include "utils/CharsetConverter.h"
18 #include "utils/StringUtils.h"
19 #include "utils/URIUtils.h"
21 #include <climits>
23 using namespace XFILE;
25 CFTPDirectory::CFTPDirectory(void) = default;
26 CFTPDirectory::~CFTPDirectory(void) = default;
28 bool CFTPDirectory::GetDirectory(const CURL& url2, CFileItemList &items)
30 CCurlFile reader;
32 CURL url(url2);
34 std::string path = url.GetFileName();
35 if( !path.empty() && !StringUtils::EndsWith(path, "/") )
37 path += "/";
38 url.SetFileName(path);
41 if (!reader.Open(url))
42 return false;
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);
53 CFTPParse parse;
54 if (parse.FTPParse(strBuffer))
56 if( parse.getName().length() == 0 )
57 continue;
59 if( parse.getFlagtrycwd() == 0 && parse.getFlagtryretr() == 0 )
60 continue;
62 /* buffer name */
63 std::string name;
64 name.assign(parse.getName());
66 if( name == ".." || name == "." )
67 continue;
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
74 if (name.empty())
75 continue;
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");
81 else
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();
98 items.Add(pItem);
102 return true;
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);
112 CCurlFile ftp;
113 CURL url2(file);
114 return ftp.Exists(url2);