LCOV - code coverage report
Current view: top level - shell/source/unix/sysshell - recently_used_file.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 54 0.0 %
Date: 2014-11-03 Functions: 0 10 0.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 "recently_used_file.hxx"
      21             : #include <rtl/ustring.hxx>
      22             : #include <osl/process.h>
      23             : #include <osl/security.hxx>
      24             : #include <osl/thread.h>
      25             : #include <osl/file.hxx>
      26             : 
      27             : #include <sys/file.h>
      28             : #include <sys/types.h>
      29             : #include <sys/stat.h>
      30             : 
      31             : #include <unistd.h>
      32             : 
      33           0 : const OUString RECENTLY_USED_FILE_NAME(".recently-used");
      34           0 : const OUString SLASH("/");
      35             : 
      36             : namespace /* private */ {
      37             : 
      38           0 : inline void ensure_final_slash(/*inout*/ OUString& path)
      39             : {
      40           0 :     if (!path.isEmpty() &&
      41           0 :         (SLASH.pData->buffer[0] != path.pData->buffer[path.getLength() - 1]))
      42           0 :         path += SLASH;
      43           0 : }
      44             : 
      45             : } // namespace private
      46             : 
      47             : 
      48           0 : recently_used_file::recently_used_file() :
      49           0 :     file_(NULL)
      50             : {
      51           0 :     osl::Security sec;
      52           0 :     OUString homedir_url;
      53             : 
      54           0 :     if (sec.getHomeDir(homedir_url))
      55             :     {
      56           0 :         OUString homedir;
      57           0 :         osl::FileBase::getSystemPathFromFileURL(homedir_url, homedir);
      58             : 
      59           0 :         OUString rufn = homedir;
      60           0 :         ensure_final_slash(rufn);
      61           0 :         rufn += RECENTLY_USED_FILE_NAME;
      62             : 
      63             :         OString tmp =
      64           0 :             OUStringToOString(rufn, osl_getThreadTextEncoding());
      65             : 
      66           0 :         int fd = open(tmp.getStr(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
      67           0 :         if (fd != -1) {
      68           0 :             file_ = fdopen(fd, "w+");
      69           0 :             if (file_ == 0) {
      70           0 :                 close(fd);
      71             :             }
      72             :         }
      73             : 
      74           0 :         if (NULL == file_)
      75           0 :             throw "I/O error opening ~/.recently-used";
      76             : 
      77           0 :         if (lockf(fileno(file_), F_LOCK, 0) != 0)
      78             :         {
      79           0 :             fclose(file_);
      80           0 :             throw "Cannot lock ~/.recently-used";
      81           0 :         }
      82             :     }
      83             :     else
      84           0 :         throw "Cannot determine user home directory";
      85           0 : }
      86             : 
      87             : 
      88           0 : recently_used_file::~recently_used_file()
      89             : {
      90           0 :     int ret = lockf(fileno(file_), F_ULOCK, 0);
      91             :     SAL_WARN_IF(ret != 0, "shell", "cannot unlock recently unused file");
      92           0 :     fclose(file_);
      93           0 : }
      94             : 
      95             : 
      96           0 : void recently_used_file::reset() const
      97             : {
      98           0 :     rewind(file_);
      99           0 : }
     100             : 
     101             : 
     102           0 : void recently_used_file::truncate(off_t length)
     103             : {
     104           0 :     if (ftruncate(fileno(file_), length) == -1)
     105           0 :         throw "I/O error: ftruncate failed";
     106           0 : }
     107             : 
     108             : 
     109           0 : size_t recently_used_file::read(char* buffer, size_t size) const
     110             : {
     111           0 :     size_t  r = fread(reinterpret_cast<void*>(buffer), sizeof(char), size, file_);
     112             : 
     113           0 :     if ((r < size) && ferror(file_))
     114           0 :         throw "I/O error: read failed";
     115             : 
     116           0 :     return r;
     117             : }
     118             : 
     119             : 
     120           0 : void recently_used_file::write(const char* buffer, size_t size) const
     121             : {
     122           0 :     if (size != fwrite(reinterpret_cast<const void*>(buffer), sizeof(char), size, file_))
     123           0 :         throw "I/O error: write failed";
     124           0 : }
     125             : 
     126             : 
     127           0 : bool recently_used_file::eof() const
     128             : {
     129           0 :     return feof(file_);
     130           0 : }
     131             : 
     132             : 
     133             : 
     134             : 
     135             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10