LCOV - code coverage report
Current view: top level - desktop/source/app - userinstall.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 49 65 75.4 %
Date: 2014-11-03 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <sal/config.h>
      21             : 
      22             : #include <cassert>
      23             : 
      24             : #include <boost/shared_ptr.hpp>
      25             : #include <com/sun/star/uno/Exception.hpp>
      26             : #include <comphelper/configuration.hxx>
      27             : #include "config_folders.h"
      28             : #include "officecfg/Setup.hxx"
      29             : #include <osl/file.h>
      30             : #include <osl/file.hxx>
      31             : #include <rtl/ustring.hxx>
      32             : #include <sal/log.hxx>
      33             : #include <unotools/bootstrap.hxx>
      34             : 
      35             : #include "userinstall.hxx"
      36             : 
      37             : namespace desktop { namespace userinstall {
      38             : 
      39             : namespace {
      40             : 
      41             : #if !(defined ANDROID || defined IOS)
      42        2560 : osl::FileBase::RC copyRecursive(
      43             :     OUString const & srcUri, OUString const & dstUri)
      44             : {
      45        2560 :     osl::DirectoryItem item;
      46        2560 :     osl::FileBase::RC e = osl::DirectoryItem::get(srcUri, item);
      47        2560 :     if (e != osl::FileBase::E_None) {
      48           0 :         return e;
      49             :     }
      50        5120 :     osl::FileStatus stat1(osl_FileStatus_Mask_Type);
      51        2560 :     e = item.getFileStatus(stat1);
      52        2560 :     if (e != osl::FileBase::E_None) {
      53           0 :         return e;
      54             :     }
      55        2560 :     if (stat1.getFileType() == osl::FileStatus::Directory) {
      56         512 :         e = osl::Directory::create(dstUri);
      57         512 :         if (e != osl::FileBase::E_None && e != osl::FileBase::E_EXIST) {
      58           0 :             return e;
      59             :         }
      60         512 :         osl::Directory dir(srcUri);
      61         512 :         e = dir.open();
      62         512 :         if (e != osl::FileBase::E_None) {
      63           0 :             return e;
      64             :         }
      65             :         for (;;) {
      66        3008 :             e = dir.getNextItem(item);
      67        3008 :             if (e == osl::FileBase::E_NOENT) {
      68         512 :                 break;
      69             :             }
      70        2496 :             if (e != osl::FileBase::E_None) {
      71           0 :                 return e;
      72             :             }
      73             :             osl::FileStatus stat2(
      74        2496 :                 osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_FileURL);
      75        2496 :             e = item.getFileStatus(stat2);
      76        2496 :             if (e != osl::FileBase::E_None) {
      77           0 :                 return e;
      78             :             }
      79             :             assert(!dstUri.endsWith("/"));
      80             :             e = copyRecursive(
      81        2496 :                 stat2.getFileURL(), dstUri + "/" + stat2.getFileName());
      82             :                 // assumes that all files under presets/ have names that can be
      83             :                 // copied unencoded into file URLs
      84        2496 :             if (e != osl::FileBase::E_None) {
      85           0 :                 return e;
      86             :             }
      87        2496 :         }
      88        1024 :         e = dir.close();
      89             :     } else {
      90        2048 :         e = osl::File::copy(srcUri, dstUri);
      91        2048 :         if (e == osl::FileBase::E_EXIST) {
      92             :             // Assume an earlier attempt failed half-way through:
      93           0 :             e = osl::FileBase::E_None;
      94             :         }
      95             :     }
      96        5120 :     return e;
      97             : }
      98             : #endif
      99             : 
     100          64 : Status create(OUString const & uri) {
     101          64 :     osl::FileBase::RC e = osl::Directory::createPath(uri);
     102          64 :     if (e != osl::FileBase::E_None && e != osl::FileBase::E_EXIST) {
     103           0 :         return ERROR_OTHER;
     104             :     }
     105             : #if !(defined ANDROID || defined IOS)
     106             : #if defined UNIX
     107             :     // Set safer permissions for the user directory by default:
     108             :     osl::File::setAttributes(
     109             :         uri,
     110             :         (osl_File_Attribute_OwnWrite | osl_File_Attribute_OwnRead
     111          64 :          | osl_File_Attribute_OwnExe));
     112             : #endif
     113             :     // As of now osl_copyFile does not work on Android => don't do this:
     114          64 :     OUString baseUri;
     115          64 :     if (utl::Bootstrap::locateBaseInstallation(baseUri)
     116             :         != utl::Bootstrap::PATH_EXISTS)
     117             :     {
     118           0 :         return ERROR_OTHER;
     119             :     }
     120         128 :     switch (copyRecursive(
     121         128 :                 baseUri + "/" LIBO_SHARE_PRESETS_FOLDER, uri + "/user"))
     122             :     {
     123             :     case osl::FileBase::E_None:
     124          64 :         break;
     125             :     case osl::FileBase::E_ACCES:
     126           0 :         return ERROR_CANT_WRITE;
     127             :     case osl::FileBase::E_NOSPC:
     128           0 :         return ERROR_NO_SPACE;
     129             :     default:
     130           0 :         return ERROR_OTHER;
     131             :     }
     132             : #endif
     133             :     boost::shared_ptr<comphelper::ConfigurationChanges> batch(
     134          64 :         comphelper::ConfigurationChanges::create());
     135          64 :     officecfg::Setup::Office::ooSetupInstCompleted::set(true, batch);
     136          64 :     batch->commit();
     137          64 :     return CREATED;
     138             : }
     139             : 
     140         160 : bool isCreated() {
     141             :     try {
     142         160 :         return officecfg::Setup::Office::ooSetupInstCompleted::get();
     143           0 :     } catch (css::uno::Exception & e) {
     144             :         SAL_WARN("desktop.app", "ignoring Exception \"" << e.Message << "\"");
     145           0 :         return false;
     146             :     }
     147             : }
     148             : 
     149             : }
     150             : 
     151         160 : Status finalize() {
     152         160 :     OUString uri;
     153         160 :     switch (utl::Bootstrap::locateUserInstallation(uri)) {
     154             :     case utl::Bootstrap::PATH_EXISTS:
     155         160 :         if (isCreated()) {
     156          96 :             return EXISTED;
     157             :         }
     158             :         // fall through
     159             :     case utl::Bootstrap::PATH_VALID:
     160          64 :         return create(uri);
     161             :     default:
     162           0 :         return ERROR_OTHER;
     163         160 :     }
     164             : }
     165             : 
     166             : } }
     167             : 
     168             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10