xref: /aoo41x/main/ucb/source/ucp/ftp/ftpurl.cxx (revision 2f86921c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ucb.hxx"
26 /**************************************************************************
27                                 TODO
28  **************************************************************************
29 
30  *************************************************************************/
31 
32 #include <memory>
33 #include <rtl/ustrbuf.hxx>
34 #include <com/sun/star/ucb/OpenMode.hpp>
35 #include <string.h>
36 #include <rtl/uri.hxx>
37 
38 #include "ftpstrcont.hxx"
39 #include "ftpurl.hxx"
40 #include "ftphandleprovider.hxx"
41 #include "ftpinpstr.hxx"
42 #include "ftpcfunc.hxx"
43 #include "ftpcontainer.hxx"
44 
45 using namespace ftp;
46 using namespace com::sun::star::ucb;
47 using namespace com::sun::star::uno;
48 using namespace com::sun::star::io;
49 
50 namespace {
51 
52 rtl::OUString encodePathSegment(rtl::OUString const & decoded) {
53     return rtl::Uri::encode(
54         decoded, rtl_UriCharClassPchar, rtl_UriEncodeIgnoreEscapes,
55         RTL_TEXTENCODING_UTF8);
56 }
57 
58 rtl::OUString decodePathSegment(rtl::OUString const & encoded) {
59     return rtl::Uri::decode(
60         encoded, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8);
61 }
62 
63 }
64 
65 MemoryContainer::MemoryContainer()
66     : m_nLen(0),
67       m_nWritePos(0),
68       m_pBuffer(0)
69 {
70 }
71 
72 MemoryContainer::~MemoryContainer()
73 {
74     rtl_freeMemory(m_pBuffer);
75 }
76 
77 
78 int MemoryContainer::append(
79     const void* pBuffer,
80     size_t size,
81     size_t nmemb
82 ) throw()
83 {
84     sal_uInt32 nLen = size*nmemb;
85     sal_uInt32 tmp(nLen + m_nWritePos);
86 
87     if(m_nLen < tmp) { // enlarge in steps of multiples of 1K
88         do {
89             m_nLen+=1024;
90         } while(m_nLen < tmp);
91 
92         m_pBuffer = rtl_reallocateMemory(m_pBuffer,m_nLen);
93     }
94 
95     rtl_copyMemory(static_cast<sal_Int8*>(m_pBuffer)+m_nWritePos,
96                    pBuffer,nLen);
97     m_nWritePos = tmp;
98     return nLen;
99 }
100 
101 
102 extern "C" {
103 
104     int memory_write(void *buffer,size_t size,size_t nmemb,void *stream)
105     {
106         MemoryContainer *_stream =
107             reinterpret_cast<MemoryContainer*>(stream);
108 
109         if(!_stream)
110             return 0;
111 
112         return _stream->append(buffer,size,nmemb);
113     }
114 
115 }
116 
117 
118 FTPURL::FTPURL(const FTPURL& r)
119     : m_mutex(),
120       m_pFCP(r.m_pFCP),
121       m_aUsername(r.m_aUsername),
122       m_bShowPassword(r.m_bShowPassword),
123       m_aHost(r.m_aHost),
124       m_aPort(r.m_aPort),
125       m_aPathSegmentVec(r.m_aPathSegmentVec)
126 
127 {
128 }
129 
130 
131 FTPURL::FTPURL(const rtl::OUString& url,
132                FTPHandleProvider* pFCP)
133     throw(
134         malformed_exception
135     )
136     : m_pFCP(pFCP),
137       m_aUsername(rtl::OUString::createFromAscii("anonymous")),
138       m_bShowPassword(false),
139       m_aPort(rtl::OUString::createFromAscii("21"))
140 {
141     parse(url);  // can reset m_bShowPassword
142 }
143 
144 
145 FTPURL::~FTPURL()
146 {
147 }
148 
149 
150 void FTPURL::parse(const rtl::OUString& url)
151     throw(
152         malformed_exception
153     )
154 {
155     rtl::OUString aPassword,aAccount;
156     rtl::OString aIdent(url.getStr(),
157                         url.getLength(),
158                         RTL_TEXTENCODING_UTF8);
159 
160     rtl::OString lower = aIdent.toAsciiLowerCase();
161     if(lower.getLength() < 6 ||
162        strncmp("ftp://",lower.getStr(),6))
163         throw malformed_exception();
164 
165     char *buffer = new char[1+aIdent.getLength()];
166     const char* p2 = aIdent.getStr();
167     p2 += 6;
168 
169     char ch;
170     char *p1 = buffer;      // determine "username:password@host:port"
171     while((ch = *p2++) != '/' && ch)
172         *p1++ = ch;
173     *p1 = 0;
174 
175     rtl::OUString aExpr(rtl::OUString(buffer,strlen(buffer),
176                                       RTL_TEXTENCODING_UTF8));
177 
178     sal_Int32 l = aExpr.indexOf(sal_Unicode('@'));
179     m_aHost = aExpr.copy(1+l);
180 
181     if(l != -1) {
182         // Now username and password.
183         aExpr = aExpr.copy(0,l);
184         l = aExpr.indexOf(sal_Unicode(':'));
185         if(l != -1) {
186             aPassword = aExpr.copy(1+l);
187             if(aPassword.getLength())
188                 m_bShowPassword = true;
189         }
190         if(l > 0)
191             // Overwritte only if the username is not empty.
192             m_aUsername = aExpr.copy(0,l);
193         else if(aExpr.getLength())
194             m_aUsername = aExpr;
195     }
196 
197     l = m_aHost.lastIndexOf(sal_Unicode(':'));
198     sal_Int32 ipv6Back = m_aHost.lastIndexOf(sal_Unicode(']'));
199     if((ipv6Back == -1 && l != -1) // not ipv6, but a port
200        ||
201        (ipv6Back != -1 && 1+ipv6Back == l) // ipv6, and a port
202     )
203     {
204         if(1+l<m_aHost.getLength())
205             m_aPort = m_aHost.copy(1+l);
206         m_aHost = m_aHost.copy(0,l);
207     }
208 
209     while(ch) {  // now determine the pathsegments ...
210         p1 = buffer;
211         while((ch = *p2++) != '/' && ch)
212             *p1++ = ch;
213         *p1 = 0;
214 
215         if(buffer[0]) {
216             if(strcmp(buffer,"..") == 0 &&
217                m_aPathSegmentVec.size() &&
218                ! m_aPathSegmentVec.back().equalsAscii(".."))
219                 m_aPathSegmentVec.pop_back();
220             else if(strcmp(buffer,".") == 0)
221                 ; // Ignore
222             else
223                 // This is a legal name.
224                 m_aPathSegmentVec.push_back(
225                     rtl::OUString(buffer,
226                                   strlen(buffer),
227                                   RTL_TEXTENCODING_UTF8));
228         }
229     }
230 
231     delete[] buffer;
232 
233     if(m_bShowPassword)
234         m_pFCP->setHost(m_aHost,
235                         m_aPort,
236                         m_aUsername,
237                         aPassword,
238                         aAccount);
239 
240     // now check for something like ";type=i" at end of url
241     if(m_aPathSegmentVec.size() &&
242        (l = m_aPathSegmentVec.back().indexOf(sal_Unicode(';'))) != -1) {
243         m_aType = m_aPathSegmentVec.back().copy(l);
244         m_aPathSegmentVec.back() = m_aPathSegmentVec.back().copy(0,l);
245     }
246 }
247 
248 
249 rtl::OUString FTPURL::ident(bool withslash,bool internal) const
250 {
251     // rebuild the url as one without ellipses,
252     // and more important, as one without username and
253     // password. ( These are set together with the command. )
254 
255     rtl::OUStringBuffer bff;
256     bff.appendAscii("ftp://");
257 
258     if(!m_aUsername.equalsAscii("anonymous")) {
259         bff.append(m_aUsername);
260 
261         rtl::OUString aPassword,aAccount;
262         m_pFCP->forHost(m_aHost,
263                         m_aPort,
264                         m_aUsername,
265                         aPassword,
266                         aAccount);
267 
268         if((m_bShowPassword || internal) &&
269            aPassword.getLength() )
270             bff.append(sal_Unicode(':'))
271                 .append(aPassword);
272 
273         bff.append(sal_Unicode('@'));
274     }
275     bff.append(m_aHost);
276 
277     if(!m_aPort.equalsAscii("21"))
278         bff.append(sal_Unicode(':'))
279             .append(m_aPort)
280             .append(sal_Unicode('/'));
281     else
282         bff.append(sal_Unicode('/'));
283 
284     for(unsigned i = 0; i < m_aPathSegmentVec.size(); ++i)
285         if(i == 0)
286             bff.append(m_aPathSegmentVec[i]);
287         else
288             bff.append(sal_Unicode('/')).append(m_aPathSegmentVec[i]);
289     if(withslash)
290         if(bff.getLength() && bff[bff.getLength()-1] != sal_Unicode('/'))
291             bff.append(sal_Unicode('/'));
292 
293     bff.append(m_aType);
294     return bff.makeStringAndClear();
295 }
296 
297 
298 rtl::OUString FTPURL::parent(bool internal) const
299 {
300     rtl::OUStringBuffer bff;
301 
302     bff.appendAscii("ftp://");
303 
304     if(!m_aUsername.equalsAscii("anonymous")) {
305         bff.append(m_aUsername);
306 
307         rtl::OUString aPassword,aAccount;
308         m_pFCP->forHost(m_aHost,
309                         m_aPort,
310                         m_aUsername,
311                         aPassword,
312                         aAccount);
313 
314         if((internal || m_bShowPassword) && aPassword.getLength())
315             bff.append(sal_Unicode(':'))
316                 .append(aPassword);
317 
318         bff.append(sal_Unicode('@'));
319     }
320 
321     bff.append(m_aHost);
322 
323     if(!m_aPort.equalsAscii("21"))
324         bff.append(sal_Unicode(':'))
325             .append(m_aPort)
326             .append(sal_Unicode('/'));
327     else
328         bff.append(sal_Unicode('/'));
329 
330     rtl::OUString last;
331 
332     for(unsigned int i = 0; i < m_aPathSegmentVec.size(); ++i)
333         if(1+i == m_aPathSegmentVec.size())
334             last = m_aPathSegmentVec[i];
335         else if(i == 0)
336             bff.append(m_aPathSegmentVec[i]);
337         else
338             bff.append(sal_Unicode('/')).append(m_aPathSegmentVec[i]);
339 
340     if(!last.getLength())
341         bff.appendAscii("..");
342     else if(last.equalsAscii(".."))
343         bff.append(last).appendAscii("/..");
344 
345     bff.append(m_aType);
346     return bff.makeStringAndClear();
347 }
348 
349 
350 void FTPURL::child(const rtl::OUString& title)
351 {
352     m_aPathSegmentVec.push_back(encodePathSegment(title));
353 }
354 
355 
356 rtl::OUString FTPURL::child() const
357 {
358     return
359         m_aPathSegmentVec.size() ?
360         decodePathSegment(m_aPathSegmentVec.back()) : rtl::OUString();
361 }
362 
363 
364 
365 /** Listing of a directory.
366  */
367 
368 namespace ftp {
369 
370     enum OS {
371         FTP_DOS,FTP_UNIX,FTP_VMS,FTP_UNKNOWN
372     };
373 
374 }
375 
376 
377 #define SET_CONTROL_CONTAINER \
378     MemoryContainer control;                                      \
379     curl_easy_setopt(curl,                                        \
380                      CURLOPT_HEADERFUNCTION,                      \
381                      memory_write);                               \
382     curl_easy_setopt(curl,                                        \
383                      CURLOPT_WRITEHEADER,                         \
384                      &control)
385 
386 
387 #define SET_DATA_CONTAINER                                        \
388         curl_easy_setopt(curl,CURLOPT_NOBODY,false);              \
389     MemoryContainer data;                                         \
390     curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,memory_write);    \
391     curl_easy_setopt(curl,CURLOPT_WRITEDATA,&data)
392 
393 #define SET_URL(url)                                              \
394     rtl::OString urlParAscii(url.getStr(),                        \
395                              url.getLength(),                     \
396                              RTL_TEXTENCODING_UTF8);              \
397     curl_easy_setopt(curl,                                        \
398                      CURLOPT_URL,                                 \
399                      urlParAscii.getStr());
400 
401         // Setting username:password
402 #define SET_USER_PASSWORD(username,password)                      \
403    rtl::OUString combi(username  +                                \
404                        rtl::OUString::createFromAscii(":") +      \
405                        password);                                 \
406    rtl::OString aUserPsswd(combi.getStr(),                        \
407                            combi.getLength(),                     \
408                            RTL_TEXTENCODING_UTF8);                \
409    curl_easy_setopt(curl,                                         \
410                     CURLOPT_USERPWD,                              \
411                     aUserPsswd.getStr())
412 
413 
414 
415 FILE* FTPURL::open()
416     throw(curl_exception)
417 {
418     if(!m_aPathSegmentVec.size())
419         throw curl_exception(CURLE_FTP_COULDNT_RETR_FILE);
420 
421     CURL *curl = m_pFCP->handle();
422 
423     SET_CONTROL_CONTAINER;
424     rtl::OUString url(ident(false,true));
425     SET_URL(url);
426     FILE *res = tmpfile();
427     curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,file_write);
428     curl_easy_setopt(curl,CURLOPT_WRITEDATA,res);
429 
430     curl_easy_setopt(curl,CURLOPT_POSTQUOTE,0);
431     CURLcode err = curl_easy_perform(curl);
432 
433     if(err == CURLE_OK)
434         rewind(res);
435     else {
436         fclose(res),res = 0;
437         throw curl_exception(err);
438     }
439 
440     return res;
441 }
442 
443 
444 std::vector<FTPDirentry> FTPURL::list(
445     sal_Int16 nMode
446 ) const
447     throw(
448         curl_exception
449     )
450 {
451     CURL *curl = m_pFCP->handle();
452 
453     SET_CONTROL_CONTAINER;
454     SET_DATA_CONTAINER;
455     rtl::OUString url(ident(true,true));
456     SET_URL(url);
457     curl_easy_setopt(curl,CURLOPT_POSTQUOTE,0);
458 
459     CURLcode err = curl_easy_perform(curl);
460     if(err != CURLE_OK)
461         throw curl_exception(err);
462 
463     // now evaluate the error messages
464 
465     sal_uInt32 len = data.m_nWritePos;
466     char* fwd = (char*) data.m_pBuffer;
467     rtl::OString str(fwd,len);
468     char *p1, *p2;
469     p1 = p2 = fwd;
470 
471     OS osKind(FTP_UNKNOWN);
472     std::vector<FTPDirentry> resvec;
473     FTPDirentry aDirEntry;
474     // ensure slash at the end
475     rtl::OUString viewurl(ident(true,false));
476 
477     while(true) {
478         while(p2-fwd < int(len) && *p2 != '\n') ++p2;
479         if(p2-fwd == int(len)) break;
480 
481         *p2 = 0;
482         switch(osKind) {
483             // While FTP knows the 'system'-command,
484             // which returns the operating system type,
485             // this is not usable here: There are Windows-server
486             // formatting the output like UNIX-ls command.
487         case FTP_DOS:
488             FTPDirectoryParser::parseDOS(aDirEntry,p1);
489             break;
490         case FTP_UNIX:
491             FTPDirectoryParser::parseUNIX(aDirEntry,p1);
492             break;
493         case FTP_VMS:
494             FTPDirectoryParser::parseVMS(aDirEntry,p1);
495             break;
496         default:
497             if(FTPDirectoryParser::parseUNIX(aDirEntry,p1))
498                 osKind = FTP_UNIX;
499             else if(FTPDirectoryParser::parseDOS(aDirEntry,p1))
500                 osKind = FTP_DOS;
501             else if(FTPDirectoryParser::parseVMS(aDirEntry,p1))
502                 osKind = FTP_VMS;
503         }
504         aDirEntry.m_aName = aDirEntry.m_aName.trim();
505         if(osKind != int(FTP_UNKNOWN) &&
506            !aDirEntry.m_aName.equalsAscii("..") &&
507            !aDirEntry.m_aName.equalsAscii(".")) {
508             aDirEntry.m_aURL = viewurl + encodePathSegment(aDirEntry.m_aName);
509 
510             sal_Bool isDir =
511                 sal_Bool(aDirEntry.m_nMode&INETCOREFTP_FILEMODE_ISDIR);
512             switch(nMode) {
513                 case OpenMode::DOCUMENTS:
514                     if(!isDir)
515                         resvec.push_back(aDirEntry);
516                     break;
517                 case OpenMode::FOLDERS:
518                     if(isDir)
519                         resvec.push_back(aDirEntry);
520                     break;
521                 default:
522                     resvec.push_back(aDirEntry);
523             };
524         }
525         aDirEntry.clear();
526         p1 = p2 + 1;
527     }
528 
529     return resvec;
530 }
531 
532 
533 rtl::OUString FTPURL::net_title() const
534     throw(curl_exception)
535 {
536     CURL *curl = m_pFCP->handle();
537 
538     SET_CONTROL_CONTAINER;
539     curl_easy_setopt(curl,CURLOPT_NOBODY,true);       // no data => no transfer
540     struct curl_slist *slist = 0;
541     // post request
542     slist = curl_slist_append(slist,"PWD");
543     curl_easy_setopt(curl,CURLOPT_POSTQUOTE,slist);
544 
545     bool try_more(true);
546     CURLcode err;
547     rtl::OUString aNetTitle;
548 
549     while(true) {
550         rtl::OUString url(ident(false,true));
551 
552         if(try_more &&
553            1+url.lastIndexOf(sal_Unicode('/')) != url.getLength())
554             url += rtl::OUString::createFromAscii("/");  // add end-slash
555         else if(!try_more &&
556                 1+url.lastIndexOf(sal_Unicode('/')) == url.getLength())
557             url = url.copy(0,url.getLength()-1);         // remove end-slash
558 
559         SET_URL(url);
560         err = curl_easy_perform(curl);
561 
562         if(err == CURLE_OK) {       // get the title from the server
563             char* fwd = (char*) control.m_pBuffer;
564             sal_uInt32 len = (sal_uInt32) control.m_nWritePos;
565 
566             aNetTitle = rtl::OUString(fwd,len,RTL_TEXTENCODING_UTF8);
567             // the buffer now contains the name of the file;
568             // analyze the output:
569             // Format of current working directory:
570             // 257 "/bla/bla" is current directory
571             sal_Int32 index1 = aNetTitle.lastIndexOf(
572                 rtl::OUString::createFromAscii("257"));
573             index1 = 1+aNetTitle.indexOf(sal_Unicode('"'),index1);
574             sal_Int32 index2 = aNetTitle.indexOf(sal_Unicode('"'),index1);
575             aNetTitle = aNetTitle.copy(index1,index2-index1);
576             if(!aNetTitle.equalsAscii("/")) {
577                 index1 = aNetTitle.lastIndexOf(sal_Unicode('/'));
578                 aNetTitle = aNetTitle.copy(1+index1);
579             }
580             try_more = false;
581         } else if(err == CURLE_BAD_PASSWORD_ENTERED)
582             // the client should retry after getting the correct
583             // username + password
584             throw curl_exception(err);
585 #if LIBCURL_VERSION_NUM>=0x070d01 /* 7.13.1 */
586         else if(err == CURLE_LOGIN_DENIED)
587             // the client should retry after getting the correct
588             // username + password
589             throw curl_exception(err);
590 #endif
591         else if(try_more && err == CURLE_FTP_ACCESS_DENIED) {
592             // We  were  either denied access when trying to login to
593             //  an FTP server or when trying to change working directory
594             //  to the one given in the URL.
595             if(m_aPathSegmentVec.size())
596                 // determine title form url
597                 aNetTitle = decodePathSegment(m_aPathSegmentVec.back());
598             else
599                 // must be root
600                 aNetTitle = rtl::OUString::createFromAscii("/");
601             try_more = false;
602         }
603 
604         if(try_more)
605             try_more = false;
606         else
607             break;
608     }
609 
610     curl_slist_free_all(slist);
611     return aNetTitle;
612 }
613 
614 
615 FTPDirentry FTPURL::direntry() const
616     throw(curl_exception)
617 {
618     rtl::OUString nettitle = net_title();
619     FTPDirentry aDirentry;
620 
621     aDirentry.m_aName = nettitle;                 // init aDirentry
622     if(nettitle.equalsAscii("/") ||
623        nettitle.equalsAscii(".."))
624         aDirentry.m_nMode = INETCOREFTP_FILEMODE_ISDIR;
625     else
626         aDirentry.m_nMode = INETCOREFTP_FILEMODE_UNKNOWN;
627 
628     aDirentry.m_nSize = 0;
629 
630     if(!nettitle.equalsAscii("/")) {
631         // try to open the parent directory
632         FTPURL aURL(parent(),m_pFCP);
633 
634         std::vector<FTPDirentry> aList = aURL.list(OpenMode::ALL);
635 
636         for(unsigned i = 0; i < aList.size(); ++i) {
637             if(aList[i].m_aName == nettitle) { // the relevant file is found
638                 aDirentry = aList[i];
639                 break;
640             }
641         }
642     }
643     return aDirentry;
644 }
645 
646 
647 extern "C" {
648 
649     size_t memory_read(void *ptr,size_t size,size_t nmemb,void *stream)
650     {
651         sal_Int32 nRequested = sal_Int32(size*nmemb);
652         CurlInput *curlInput = static_cast<CurlInput*>(stream);
653         if(curlInput)
654             return size_t(curlInput->read(((sal_Int8*)ptr),nRequested));
655         else
656             return 0;
657     }
658 
659 }
660 
661 
662 void FTPURL::insert(bool replaceExisting,void* stream) const
663     throw(curl_exception)
664 {
665     if(!replaceExisting) {
666 //          FTPDirentry aDirentry(direntry());
667 //          if(aDirentry.m_nMode == INETCOREFTP_FILEMODE_UNKNOWN)
668         // throw curl_exception(FILE_EXIST_DURING_INSERT);
669         throw curl_exception(FILE_MIGHT_EXIST_DURING_INSERT);
670     } // else
671     // overwrite is default in libcurl
672 
673     CURL *curl = m_pFCP->handle();
674 
675     SET_CONTROL_CONTAINER;
676     curl_easy_setopt(curl,CURLOPT_NOBODY,false);    // no data => no transfer
677     curl_easy_setopt(curl,CURLOPT_POSTQUOTE,0);
678     curl_easy_setopt(curl,CURLOPT_QUOTE,0);
679     curl_easy_setopt(curl,CURLOPT_READFUNCTION,memory_read);
680     curl_easy_setopt(curl,CURLOPT_READDATA,stream);
681     curl_easy_setopt(curl, CURLOPT_UPLOAD,1);
682 
683     rtl::OUString url(ident(false,true));
684     SET_URL(url);
685 
686     CURLcode err = curl_easy_perform(curl);
687     curl_easy_setopt(curl, CURLOPT_UPLOAD,false);
688 
689     if(err != CURLE_OK)
690         throw curl_exception(err);
691 }
692 
693 
694 
695 void FTPURL::mkdir(bool ReplaceExisting) const
696     throw(curl_exception)
697 {
698     rtl::OString title;
699     if(m_aPathSegmentVec.size()) {
700         rtl::OUString titleOU = m_aPathSegmentVec.back();
701         titleOU = decodePathSegment(titleOU);
702         title = rtl::OString(titleOU.getStr(),
703                             titleOU.getLength(),
704                             RTL_TEXTENCODING_UTF8);
705     }
706     else
707         // will give an error
708         title = rtl::OString("/");
709 
710     rtl::OString aDel("del "); aDel += title;
711     rtl::OString mkd("mkd "); mkd += title;
712 
713     struct curl_slist *slist = 0;
714 
715     FTPDirentry aDirentry(direntry());
716     if(!ReplaceExisting) {
717 //          if(aDirentry.m_nMode != INETCOREFTP_FILEMODE_UNKNOWN)
718 //              throw curl_exception(FOLDER_EXIST_DURING_INSERT);
719         throw curl_exception(FOLDER_MIGHT_EXIST_DURING_INSERT);
720     } else if(aDirentry.m_nMode != INETCOREFTP_FILEMODE_UNKNOWN)
721         slist = curl_slist_append(slist,aDel.getStr());
722 
723     slist = curl_slist_append(slist,mkd.getStr());
724 
725     CURL *curl = m_pFCP->handle();
726     SET_CONTROL_CONTAINER;
727     curl_easy_setopt(curl,CURLOPT_NOBODY,true);       // no data => no transfer
728     curl_easy_setopt(curl,CURLOPT_QUOTE,0);
729 
730     // post request
731     curl_easy_setopt(curl,CURLOPT_POSTQUOTE,slist);
732 
733     rtl::OUString url(parent(true));
734     if(1+url.lastIndexOf(sal_Unicode('/')) != url.getLength())
735         url += rtl::OUString::createFromAscii("/");
736     SET_URL(url);
737 
738     CURLcode err = curl_easy_perform(curl);
739     curl_slist_free_all(slist);
740     if(err != CURLE_OK)
741         throw curl_exception(err);
742 }
743 
744 
745 rtl::OUString FTPURL::ren(const rtl::OUString& NewTitle)
746     throw(curl_exception)
747 {
748     CURL *curl = m_pFCP->handle();
749 
750     // post request
751     rtl::OString renamefrom("RNFR ");
752     rtl::OUString OldTitle = net_title();
753     renamefrom +=
754         rtl::OString(OldTitle.getStr(),
755                      OldTitle.getLength(),
756                      RTL_TEXTENCODING_UTF8);
757 
758     rtl::OString renameto("RNTO ");
759     renameto +=
760         rtl::OString(NewTitle.getStr(),
761                      NewTitle.getLength(),
762                      RTL_TEXTENCODING_UTF8);
763 
764     struct curl_slist *slist = 0;
765     slist = curl_slist_append(slist,renamefrom.getStr());
766     slist = curl_slist_append(slist,renameto.getStr());
767     curl_easy_setopt(curl,CURLOPT_POSTQUOTE,slist);
768 
769     SET_CONTROL_CONTAINER;
770     curl_easy_setopt(curl,CURLOPT_NOBODY,true);       // no data => no transfer
771     curl_easy_setopt(curl,CURLOPT_QUOTE,0);
772 
773     rtl::OUString url(parent(true));
774     if(1+url.lastIndexOf(sal_Unicode('/')) != url.getLength())
775         url += rtl::OUString::createFromAscii("/");
776     SET_URL(url);
777 
778     CURLcode err = curl_easy_perform(curl);
779     curl_slist_free_all(slist);
780     if(err != CURLE_OK)
781         throw curl_exception(err);
782     else if(m_aPathSegmentVec.size() &&
783             !m_aPathSegmentVec.back().equalsAscii(".."))
784         m_aPathSegmentVec.back() = encodePathSegment(NewTitle);
785     return OldTitle;
786 }
787 
788 
789 
790 void FTPURL::del() const
791     throw(curl_exception)
792 {
793     FTPDirentry aDirentry(direntry());
794 
795     rtl::OString dele(aDirentry.m_aName.getStr(),
796                       aDirentry.m_aName.getLength(),
797                       RTL_TEXTENCODING_UTF8);
798 
799     if(aDirentry.m_nMode & INETCOREFTP_FILEMODE_ISDIR) {
800         std::vector<FTPDirentry> vec = list(sal_Int16(OpenMode::ALL));
801         for( unsigned int i = 0; i < vec.size(); ++i )
802             try {
803                 FTPURL url(vec[i].m_aURL,m_pFCP);
804                 url.del();
805             } catch(const curl_exception&) {
806             }
807         dele = rtl::OString("RMD ") + dele;
808     }
809     else if(aDirentry.m_nMode != INETCOREFTP_FILEMODE_UNKNOWN)
810         dele = rtl::OString("DELE ") + dele;
811     else
812         return;
813 
814     // post request
815     CURL *curl = m_pFCP->handle();
816     struct curl_slist *slist = 0;
817     slist = curl_slist_append(slist,dele.getStr());
818     curl_easy_setopt(curl,CURLOPT_POSTQUOTE,slist);
819 
820     SET_CONTROL_CONTAINER;
821     curl_easy_setopt(curl,CURLOPT_NOBODY,true);       // no data => no transfer
822     curl_easy_setopt(curl,CURLOPT_QUOTE,0);
823 
824     rtl::OUString url(parent(true));
825     if(1+url.lastIndexOf(sal_Unicode('/')) != url.getLength())
826         url += rtl::OUString::createFromAscii("/");
827     SET_URL(url);
828 
829     CURLcode err = curl_easy_perform(curl);
830     curl_slist_free_all(slist);
831     if(err != CURLE_OK)
832         throw curl_exception(err);
833 }
834 
835