bump product version to 4.1.6.2
[LibreOffice.git] / shell / source / unix / sysshell / recently_used_file.cxx
blobb1a71b795ab407725bed9f331d33d0e019b555b5
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 "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>
27 #include <sys/file.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #include <unistd.h>
33 const OUString RECENTLY_USED_FILE_NAME(".recently-used");
34 const OUString SLASH("/");
36 namespace /* private */ {
38 inline void ensure_final_slash(/*inout*/ OUString& path)
40 if (!path.isEmpty() &&
41 (SLASH.pData->buffer[0] != path.pData->buffer[path.getLength() - 1]))
42 path += SLASH;
45 } // namespace private
47 //------------------------------------------------
48 recently_used_file::recently_used_file() :
49 file_(NULL)
51 osl::Security sec;
52 OUString homedir_url;
54 if (sec.getHomeDir(homedir_url))
56 OUString homedir;
57 osl::FileBase::getSystemPathFromFileURL(homedir_url, homedir);
59 OUString rufn = homedir;
60 ensure_final_slash(rufn);
61 rufn += RECENTLY_USED_FILE_NAME;
63 OString tmp =
64 OUStringToOString(rufn, osl_getThreadTextEncoding());
66 file_ = fopen(tmp.getStr(), "r+");
68 /* create if not exist */
69 if (NULL == file_) {
70 mode_t umask_ = umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
71 file_ = fopen(tmp.getStr(), "w+");
72 umask(umask_);
75 if (NULL == file_)
76 throw "I/O error opening ~/.recently-used";
78 if (lockf(fileno(file_), F_LOCK, 0) != 0)
80 fclose(file_);
81 throw "Cannot lock ~/.recently-used";
84 else
85 throw "Cannot determine user home directory";
88 //------------------------------------------------
89 recently_used_file::~recently_used_file()
91 int ret = lockf(fileno(file_), F_ULOCK, 0);
92 SAL_WARN_IF(ret != 0, "shell", "cannot unlock recently unused file");
93 fclose(file_);
96 //------------------------------------------------
97 void recently_used_file::reset() const
99 rewind(file_);
102 //------------------------------------------------
103 void recently_used_file::truncate(off_t length)
105 if (ftruncate(fileno(file_), length) == -1)
106 throw "I/O error: ftruncate failed";
109 //------------------------------------------------
110 size_t recently_used_file::read(char* buffer, size_t size) const
112 size_t r = fread(reinterpret_cast<void*>(buffer), sizeof(char), size, file_);
114 if ((r < size) && ferror(file_))
115 throw "I/O error: read failed";
117 return r;
120 //----------------------------
121 void recently_used_file::write(const char* buffer, size_t size) const
123 if (size != fwrite(reinterpret_cast<const void*>(buffer), sizeof(char), size, file_))
124 throw "I/O error: write failed";
127 //----------------------------
128 bool recently_used_file::eof() const
130 return feof(file_);
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */