fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / shell / source / unix / sysshell / recently_used_file.cxx
blob16b0a373ad1b6c443da48f8a7d25c7f679afa0e3
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>
30 #include <fcntl.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
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 int fd = open(tmp.getStr(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
67 if (fd != -1) {
68 file_ = fdopen(fd, "w+");
69 if (file_ == 0) {
70 close(fd);
74 if (NULL == file_)
75 throw "I/O error opening ~/.recently-used";
77 if (lockf(fileno(file_), F_LOCK, 0) != 0)
79 fclose(file_);
80 throw "Cannot lock ~/.recently-used";
83 else
84 throw "Cannot determine user home directory";
88 recently_used_file::~recently_used_file()
90 int ret = lockf(fileno(file_), F_ULOCK, 0);
91 SAL_WARN_IF(ret != 0, "shell", "cannot unlock recently unused file");
92 fclose(file_);
96 void recently_used_file::reset() const
98 rewind(file_);
102 void recently_used_file::truncate(off_t length)
104 if (ftruncate(fileno(file_), length) == -1)
105 throw "I/O error: ftruncate failed";
109 size_t recently_used_file::read(char* buffer, size_t size) const
111 size_t r = fread(buffer, sizeof(char), size, file_);
113 if ((r < size) && ferror(file_))
114 throw "I/O error: read failed";
116 return r;
120 void recently_used_file::write(const char* buffer, size_t size) const
122 if (size != fwrite(buffer, sizeof(char), size, file_))
123 throw "I/O error: write failed";
127 bool recently_used_file::eof() const
129 return feof(file_);
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */