1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
28 #include <sys/types.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]))
45 } // namespace private
48 recently_used_file::recently_used_file() :
54 if (sec
.getHomeDir(homedir_url
))
57 osl::FileBase::getSystemPathFromFileURL(homedir_url
, homedir
);
59 OUString rufn
= homedir
;
60 ensure_final_slash(rufn
);
61 rufn
+= RECENTLY_USED_FILE_NAME
;
64 OUStringToOString(rufn
, osl_getThreadTextEncoding());
66 int fd
= open(tmp
.getStr(), O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
);
68 file_
= fdopen(fd
, "w+");
75 throw "I/O error opening ~/.recently-used";
77 if (lockf(fileno(file_
), F_LOCK
, 0) != 0)
80 throw "Cannot lock ~/.recently-used";
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");
96 void recently_used_file::reset() const
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";
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
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */