1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_shell.hxx" 26 27 #ifndef INCLUDED_RECENTLY_USED_FILE 28 #include "recently_used_file.hxx" 29 #endif 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 const rtl::OUString RECENTLY_USED_FILE_NAME = rtl::OUString::createFromAscii(".recently-used"); 43 const rtl::OUString SLASH = rtl::OUString::createFromAscii("/"); 44 45 namespace /* private */ { 46 47 inline void ensure_final_slash(/*inout*/ rtl::OUString& path) 48 { 49 if ((path.getLength() > 0) && 50 (SLASH.pData->buffer[0] != path.pData->buffer[path.getLength() - 1])) 51 path += SLASH; 52 } 53 54 } // namespace private 55 56 //------------------------------------------------ 57 recently_used_file::recently_used_file() : 58 file_(NULL) 59 { 60 osl::Security sec; 61 rtl::OUString homedir_url; 62 63 if (sec.getHomeDir(homedir_url)) 64 { 65 rtl::OUString homedir; 66 osl::FileBase::getSystemPathFromFileURL(homedir_url, homedir); 67 68 rtl::OUString rufn = homedir; 69 ensure_final_slash(rufn); 70 rufn += RECENTLY_USED_FILE_NAME; 71 72 rtl::OString tmp = 73 rtl::OUStringToOString(rufn, osl_getThreadTextEncoding()); 74 75 file_ = fopen(tmp.getStr(), "r+"); 76 77 /* create if not exist */ 78 if (NULL == file_) { 79 mode_t umask_ = umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); 80 file_ = fopen(tmp.getStr(), "w+"); 81 umask(umask_); 82 } 83 84 if (NULL == file_) 85 throw "I/O error opening ~/.recently-used"; 86 87 if (lockf(fileno(file_), F_LOCK, 0) != 0) 88 { 89 fclose(file_); 90 throw "Cannot lock ~/.recently-used"; 91 } 92 } 93 else 94 throw "Cannot determine user home directory"; 95 } 96 97 //------------------------------------------------ 98 recently_used_file::~recently_used_file() 99 { 100 lockf(fileno(file_), F_ULOCK, 0); 101 fclose(file_); 102 } 103 104 //------------------------------------------------ 105 void recently_used_file::reset() const 106 { 107 rewind(file_); 108 } 109 110 //------------------------------------------------ 111 void recently_used_file::truncate(off_t length) 112 { 113 if (ftruncate(fileno(file_), length) == -1) 114 throw "I/O error: ftruncate failed"; 115 } 116 117 //------------------------------------------------ 118 size_t recently_used_file::read(char* buffer, size_t size) const 119 { 120 size_t r = fread(reinterpret_cast<void*>(buffer), sizeof(char), size, file_); 121 122 if ((r < size) && ferror(file_)) 123 throw "I/O error: read failed"; 124 125 return r; 126 } 127 128 //---------------------------- 129 void recently_used_file::write(const char* buffer, size_t size) const 130 { 131 if (size != fwrite(reinterpret_cast<const void*>(buffer), sizeof(char), size, file_)) 132 throw "I/O error: write failed"; 133 } 134 135 //---------------------------- 136 bool recently_used_file::eof() const 137 { 138 return feof(file_); 139 } 140 141 142 143 144