LCOV - code coverage report
Current view: top level - shell/source/unix/sysshell - recently_used_file.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 54 0.0 %
Date: 2014-04-14 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 :         file_ = fopen(tmp.getStr(), "r+");
      67             : 
      68             :         /* create if not exist */
      69           0 :         if (NULL == file_) {
      70           0 :             mode_t umask_ = umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
      71           0 :             file_ = fopen(tmp.getStr(), "w+");
      72           0 :             umask(umask_);
      73             :         }
      74             : 
      75           0 :         if (NULL == file_)
      76           0 :             throw "I/O error opening ~/.recently-used";
      77             : 
      78           0 :         if (lockf(fileno(file_), F_LOCK, 0) != 0)
      79             :         {
      80           0 :             fclose(file_);
      81           0 :             throw "Cannot lock ~/.recently-used";
      82           0 :         }
      83             :     }
      84             :     else
      85           0 :         throw "Cannot determine user home directory";
      86           0 : }
      87             : 
      88             : 
      89           0 : recently_used_file::~recently_used_file()
      90             : {
      91           0 :     int ret = lockf(fileno(file_), F_ULOCK, 0);
      92             :     SAL_WARN_IF(ret != 0, "shell", "cannot unlock recently unused file");
      93           0 :     fclose(file_);
      94           0 : }
      95             : 
      96             : 
      97           0 : void recently_used_file::reset() const
      98             : {
      99           0 :     rewind(file_);
     100           0 : }
     101             : 
     102             : 
     103           0 : void recently_used_file::truncate(off_t length)
     104             : {
     105           0 :     if (ftruncate(fileno(file_), length) == -1)
     106           0 :         throw "I/O error: ftruncate failed";
     107           0 : }
     108             : 
     109             : 
     110           0 : size_t recently_used_file::read(char* buffer, size_t size) const
     111             : {
     112           0 :     size_t  r = fread(reinterpret_cast<void*>(buffer), sizeof(char), size, file_);
     113             : 
     114           0 :     if ((r < size) && ferror(file_))
     115           0 :         throw "I/O error: read failed";
     116             : 
     117           0 :     return r;
     118             : }
     119             : 
     120             : 
     121           0 : void recently_used_file::write(const char* buffer, size_t size) const
     122             : {
     123           0 :     if (size != fwrite(reinterpret_cast<const void*>(buffer), sizeof(char), size, file_))
     124           0 :         throw "I/O error: write failed";
     125           0 : }
     126             : 
     127             : 
     128           0 : bool recently_used_file::eof() const
     129             : {
     130           0 :     return feof(file_);
     131           0 : }
     132             : 
     133             : 
     134             : 
     135             : 
     136             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10