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