Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / ucbhelper / localfilehelper.cxx
blob19de32703b37ae5b7c46b51a9be428e54f515909
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/sdbc/XResultSet.hpp>
21 #include <com/sun/star/ucb/XContentAccess.hpp>
22 #include <com/sun/star/ucb/CommandAbortedException.hpp>
23 #include <comphelper/processfactory.hxx>
24 #include <comphelper/sequence.hxx>
25 #include <sal/log.hxx>
26 #include <unotools/localfilehelper.hxx>
27 #include <rtl/ustring.hxx>
28 #include <osl/file.hxx>
29 #include <ucbhelper/content.hxx>
30 #include <vector>
32 using namespace ::osl;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::ucb;
36 namespace utl
39 css::uno::Sequence < OUString > LocalFileHelper::GetFolderContents( const OUString& rFolder, bool bFolder )
41 std::vector< OUString > vFiles;
42 try
44 ::ucbhelper::Content aCnt(
45 rFolder, Reference< XCommandEnvironment >(),
46 comphelper::getProcessComponentContext() );
47 Reference< css::sdbc::XResultSet > xResultSet;
48 css::uno::Sequence< OUString > aProps { "Url" };
50 try
52 ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
53 xResultSet = aCnt.createCursor( aProps, eInclude );
55 catch( css::ucb::CommandAbortedException& )
58 catch( Exception& )
62 if ( xResultSet.is() )
64 Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
65 try
67 while ( xResultSet->next() )
68 vFiles.push_back( xContentAccess->queryContentIdentifierString() );
70 catch( css::ucb::CommandAbortedException& )
73 catch( Exception& )
78 catch( Exception& )
82 return comphelper::containerToSequence(vFiles);
85 void removeTree(OUString const & url) {
86 osl::Directory dir(url);
87 osl::FileBase::RC rc = dir.open();
88 switch (rc) {
89 case osl::FileBase::E_None:
90 break;
91 case osl::FileBase::E_NOENT:
92 return; //TODO: SAL_WARN if recursive
93 default:
94 SAL_WARN("desktop.app", "cannot open directory " << dir.getURL() << ": " << +rc);
95 return;
97 for (;;) {
98 osl::DirectoryItem i;
99 rc = dir.getNextItem(i, SAL_MAX_UINT32);
100 if (rc == osl::FileBase::E_NOENT) {
101 break;
103 if (rc != osl::FileBase::E_None) {
104 SAL_WARN( "desktop.app", "cannot iterate directory " << dir.getURL() << ": " << +rc);
105 break;
107 osl::FileStatus stat(
108 osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName |
109 osl_FileStatus_Mask_FileURL);
110 rc = i.getFileStatus(stat);
111 if (rc != osl::FileBase::E_None) {
112 SAL_WARN( "desktop.app", "cannot stat in directory " << dir.getURL() << ": " << +rc);
113 continue;
115 if (stat.getFileType() == osl::FileStatus::Directory) { //TODO: symlinks
116 removeTree(stat.getFileURL());
117 } else {
118 rc = osl::File::remove(stat.getFileURL());
119 SAL_WARN_IF(
120 rc != osl::FileBase::E_None, "desktop.app",
121 "cannot remove file " << stat.getFileURL() << ": " << +rc);
124 if (dir.isOpen()) {
125 rc = dir.close();
126 SAL_WARN_IF(
127 rc != osl::FileBase::E_None, "desktop.app",
128 "cannot close directory " << dir.getURL() << ": " << +rc);
130 rc = osl::Directory::remove(url);
131 SAL_WARN_IF(
132 rc != osl::FileBase::E_None, "desktop.app",
133 "cannot remove directory " << url << ": " << +rc);
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */