2010-10-12 15:53:47 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-10-02 14:13:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2003-09-29 13:54:55 +00:00
|
|
|
|
|
|
|
#include "recently_used_file.hxx"
|
|
|
|
#include <rtl/ustring.hxx>
|
|
|
|
#include <osl/process.h>
|
|
|
|
#include <osl/security.hxx>
|
|
|
|
#include <osl/thread.h>
|
|
|
|
#include <osl/file.hxx>
|
|
|
|
|
|
|
|
#include <sys/file.h>
|
2006-03-22 08:39:25 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2014-11-09 18:15:45 +01:00
|
|
|
#include <fcntl.h>
|
2003-09-29 13:54:55 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
const OUString RECENTLY_USED_FILE_NAME(".recently-used");
|
|
|
|
const OUString SLASH("/");
|
2003-09-29 13:54:55 +00:00
|
|
|
|
|
|
|
namespace /* private */ {
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
inline void ensure_final_slash(/*inout*/ OUString& path)
|
2003-09-29 13:54:55 +00:00
|
|
|
{
|
2012-01-11 11:37:33 -02:00
|
|
|
if (!path.isEmpty() &&
|
2003-09-29 13:54:55 +00:00
|
|
|
(SLASH.pData->buffer[0] != path.pData->buffer[path.getLength() - 1]))
|
|
|
|
path += SLASH;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace private
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2003-09-29 13:54:55 +00:00
|
|
|
recently_used_file::recently_used_file() :
|
|
|
|
file_(NULL)
|
|
|
|
{
|
|
|
|
osl::Security sec;
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString homedir_url;
|
2003-09-29 13:54:55 +00:00
|
|
|
|
|
|
|
if (sec.getHomeDir(homedir_url))
|
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString homedir;
|
2003-09-29 13:54:55 +00:00
|
|
|
osl::FileBase::getSystemPathFromFileURL(homedir_url, homedir);
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString rufn = homedir;
|
2003-09-29 13:54:55 +00:00
|
|
|
ensure_final_slash(rufn);
|
|
|
|
rufn += RECENTLY_USED_FILE_NAME;
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
OString tmp =
|
|
|
|
OUStringToOString(rufn, osl_getThreadTextEncoding());
|
2003-09-29 13:54:55 +00:00
|
|
|
|
2014-05-13 11:39:30 +02:00
|
|
|
int fd = open(tmp.getStr(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd != -1) {
|
|
|
|
file_ = fdopen(fd, "w+");
|
|
|
|
if (file_ == 0) {
|
|
|
|
close(fd);
|
|
|
|
}
|
2006-03-22 08:39:25 +00:00
|
|
|
}
|
2003-09-29 13:54:55 +00:00
|
|
|
|
|
|
|
if (NULL == file_)
|
|
|
|
throw "I/O error opening ~/.recently-used";
|
|
|
|
|
|
|
|
if (lockf(fileno(file_), F_LOCK, 0) != 0)
|
|
|
|
{
|
|
|
|
fclose(file_);
|
|
|
|
throw "Cannot lock ~/.recently-used";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw "Cannot determine user home directory";
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2003-09-29 13:54:55 +00:00
|
|
|
recently_used_file::~recently_used_file()
|
|
|
|
{
|
2012-03-21 21:33:04 +00:00
|
|
|
int ret = lockf(fileno(file_), F_ULOCK, 0);
|
|
|
|
SAL_WARN_IF(ret != 0, "shell", "cannot unlock recently unused file");
|
2003-09-29 13:54:55 +00:00
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2003-09-29 13:54:55 +00:00
|
|
|
void recently_used_file::reset() const
|
|
|
|
{
|
|
|
|
rewind(file_);
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2003-09-29 13:54:55 +00:00
|
|
|
void recently_used_file::truncate(off_t length)
|
|
|
|
{
|
2010-05-24 11:28:33 +01:00
|
|
|
if (ftruncate(fileno(file_), length) == -1)
|
|
|
|
throw "I/O error: ftruncate failed";
|
2003-09-29 13:54:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2003-09-29 13:54:55 +00:00
|
|
|
size_t recently_used_file::read(char* buffer, size_t size) const
|
|
|
|
{
|
2015-05-12 18:27:37 +02:00
|
|
|
size_t r = fread(buffer, sizeof(char), size, file_);
|
2003-09-29 13:54:55 +00:00
|
|
|
|
|
|
|
if ((r < size) && ferror(file_))
|
|
|
|
throw "I/O error: read failed";
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2003-09-29 13:54:55 +00:00
|
|
|
void recently_used_file::write(const char* buffer, size_t size) const
|
|
|
|
{
|
2015-05-12 18:27:37 +02:00
|
|
|
if (size != fwrite(buffer, sizeof(char), size, file_))
|
2003-09-29 13:54:55 +00:00
|
|
|
throw "I/O error: write failed";
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2003-09-29 13:54:55 +00:00
|
|
|
bool recently_used_file::eof() const
|
|
|
|
{
|
|
|
|
return feof(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-12 15:53:47 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|