1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: recently_used_file.cxx,v $
10 * $Revision: 1.7.30.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
34 #ifndef INCLUDED_RECENTLY_USED_FILE
35 #include "recently_used_file.hxx"
37 #include <rtl/ustring.hxx>
38 #include <osl/process.h>
39 #include <osl/security.hxx>
40 #include <osl/thread.h>
41 #include <osl/file.hxx>
44 #include <sys/types.h>
49 const rtl::OUString RECENTLY_USED_FILE_NAME
= rtl::OUString::createFromAscii(".recently-used");
50 const rtl::OUString SLASH
= rtl::OUString::createFromAscii("/");
52 namespace /* private */ {
54 inline void ensure_final_slash(/*inout*/ rtl::OUString
& path
)
56 if ((path
.getLength() > 0) &&
57 (SLASH
.pData
->buffer
[0] != path
.pData
->buffer
[path
.getLength() - 1]))
61 } // namespace private
63 //------------------------------------------------
64 recently_used_file::recently_used_file() :
68 rtl::OUString homedir_url
;
70 if (sec
.getHomeDir(homedir_url
))
72 rtl::OUString homedir
;
73 osl::FileBase::getSystemPathFromFileURL(homedir_url
, homedir
);
75 rtl::OUString rufn
= homedir
;
76 ensure_final_slash(rufn
);
77 rufn
+= RECENTLY_USED_FILE_NAME
;
80 rtl::OUStringToOString(rufn
, osl_getThreadTextEncoding());
82 file_
= fopen(tmp
.getStr(), "r+");
84 /* create if not exist */
86 mode_t umask_
= umask(S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
);
87 file_
= fopen(tmp
.getStr(), "w+");
92 throw "I/O error opening ~/.recently-used";
94 if (lockf(fileno(file_
), F_LOCK
, 0) != 0)
97 throw "Cannot lock ~/.recently-used";
101 throw "Cannot determine user home directory";
104 //------------------------------------------------
105 recently_used_file::~recently_used_file()
107 lockf(fileno(file_
), F_ULOCK
, 0);
111 //------------------------------------------------
112 void recently_used_file::reset() const
117 //------------------------------------------------
118 void recently_used_file::flush()
123 //------------------------------------------------
124 void recently_used_file::truncate(off_t length
)
126 ftruncate(fileno(file_
), length
);
129 //------------------------------------------------
130 size_t recently_used_file::read(char* buffer
, size_t size
) const
132 size_t r
= fread(reinterpret_cast<void*>(buffer
), sizeof(char), size
, file_
);
134 if ((r
< size
) && ferror(file_
))
135 throw "I/O error: read failed";
140 //----------------------------
141 void recently_used_file::write(const char* buffer
, size_t size
) const
143 if (size
!= fwrite(reinterpret_cast<const void*>(buffer
), sizeof(char), size
, file_
))
144 throw "I/O error: write failed";
147 //----------------------------
148 bool recently_used_file::eof() const