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
47 //------------------------------------------------
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 file_
= fopen(tmp
.getStr(), "r+");
68 /* create if not exist */
70 mode_t umask_
= umask(S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
);
71 file_
= fopen(tmp
.getStr(), "w+");
76 throw "I/O error opening ~/.recently-used";
78 if (lockf(fileno(file_
), F_LOCK
, 0) != 0)
81 throw "Cannot lock ~/.recently-used";
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");
96 //------------------------------------------------
97 void recently_used_file::reset() const
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";
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
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */