Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "recently_used_file.hxx"
30 : : #include <rtl/ustring.hxx>
31 : : #include <osl/process.h>
32 : : #include <osl/security.hxx>
33 : : #include <osl/thread.h>
34 : : #include <osl/file.hxx>
35 : :
36 : : #include <sys/file.h>
37 : : #include <sys/types.h>
38 : : #include <sys/stat.h>
39 : :
40 : : #include <unistd.h>
41 : :
42 : 0 : const rtl::OUString RECENTLY_USED_FILE_NAME(".recently-used");
43 : 0 : const rtl::OUString SLASH("/");
44 : :
45 : : namespace /* private */ {
46 : :
47 : 0 : inline void ensure_final_slash(/*inout*/ rtl::OUString& path)
48 : : {
49 : 0 : if (!path.isEmpty() &&
50 : 0 : (SLASH.pData->buffer[0] != path.pData->buffer[path.getLength() - 1]))
51 : 0 : path += SLASH;
52 : 0 : }
53 : :
54 : : } // namespace private
55 : :
56 : : //------------------------------------------------
57 : 0 : recently_used_file::recently_used_file() :
58 : 0 : file_(NULL)
59 : : {
60 : 0 : osl::Security sec;
61 : 0 : rtl::OUString homedir_url;
62 : :
63 : 0 : if (sec.getHomeDir(homedir_url))
64 : : {
65 : 0 : rtl::OUString homedir;
66 : 0 : osl::FileBase::getSystemPathFromFileURL(homedir_url, homedir);
67 : :
68 : 0 : rtl::OUString rufn = homedir;
69 : 0 : ensure_final_slash(rufn);
70 : 0 : rufn += RECENTLY_USED_FILE_NAME;
71 : :
72 : : rtl::OString tmp =
73 : 0 : rtl::OUStringToOString(rufn, osl_getThreadTextEncoding());
74 : :
75 : 0 : file_ = fopen(tmp.getStr(), "r+");
76 : :
77 : : /* create if not exist */
78 : 0 : if (NULL == file_) {
79 : 0 : mode_t umask_ = umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
80 : 0 : file_ = fopen(tmp.getStr(), "w+");
81 : 0 : umask(umask_);
82 : : }
83 : :
84 : 0 : if (NULL == file_)
85 : 0 : throw "I/O error opening ~/.recently-used";
86 : :
87 : 0 : if (lockf(fileno(file_), F_LOCK, 0) != 0)
88 : : {
89 : 0 : fclose(file_);
90 : 0 : throw "Cannot lock ~/.recently-used";
91 : 0 : }
92 : : }
93 : : else
94 : 0 : throw "Cannot determine user home directory";
95 : 0 : }
96 : :
97 : : //------------------------------------------------
98 : 0 : recently_used_file::~recently_used_file()
99 : : {
100 : 0 : int ret = lockf(fileno(file_), F_ULOCK, 0);
101 : : SAL_WARN_IF(ret != 0, "shell", "cannot unlock recently unused file");
102 : 0 : fclose(file_);
103 : 0 : }
104 : :
105 : : //------------------------------------------------
106 : 0 : void recently_used_file::reset() const
107 : : {
108 : 0 : rewind(file_);
109 : 0 : }
110 : :
111 : : //------------------------------------------------
112 : 0 : void recently_used_file::truncate(off_t length)
113 : : {
114 : 0 : if (ftruncate(fileno(file_), length) == -1)
115 : 0 : throw "I/O error: ftruncate failed";
116 : 0 : }
117 : :
118 : : //------------------------------------------------
119 : 0 : size_t recently_used_file::read(char* buffer, size_t size) const
120 : : {
121 : 0 : size_t r = fread(reinterpret_cast<void*>(buffer), sizeof(char), size, file_);
122 : :
123 : 0 : if ((r < size) && ferror(file_))
124 : 0 : throw "I/O error: read failed";
125 : :
126 : 0 : return r;
127 : : }
128 : :
129 : : //----------------------------
130 : 0 : void recently_used_file::write(const char* buffer, size_t size) const
131 : : {
132 : 0 : if (size != fwrite(reinterpret_cast<const void*>(buffer), sizeof(char), size, file_))
133 : 0 : throw "I/O error: write failed";
134 : 0 : }
135 : :
136 : : //----------------------------
137 : 0 : bool recently_used_file::eof() const
138 : : {
139 : 0 : return feof(file_);
140 : 0 : }
141 : :
142 : :
143 : :
144 : :
145 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|