blob: 4a98ad867791195a64b82a90c3e53c5ca42c0a2e [file] [log] [blame]
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <string>
#include <vector>
#include <base/files/file_path.h>
#include <base/macros.h>
namespace webservd {
// TempFileManager maintains life-times of temporary files associated with HTTP
// requests. Web server might require temporary storage to back certain large
// requests and this class is used to track those files and make sure that all
// the temporary files are deleted when the request is complete.
class TempFileManager {
// FileSystemInterface allows to abstract the file system in tests.
class FileDeleterInterface {
virtual bool DeleteFile(const base::FilePath& path) = 0;
virtual ~FileDeleterInterface() = default;
TempFileManager(const base::FilePath& temp_dir_path,
FileDeleterInterface* file_deleter);
TempFileManager(const TempFileManager&) = delete;
TempFileManager& operator=(const TempFileManager&) = delete;
// Generate a new temporary file name for a request with unique ID
// |request_id|. No actual file is created on the file system at this time.
// The file name is registered with the request ID so it can be later deleted
// when request is completed.
base::FilePath CreateTempFileName(const std::string& request_id);
// Deletes all the files belonging to the given request.
void DeleteRequestTempFiles(const std::string& request_id);
// Deletes all the files in the list.
void DeleteFiles(const std::vector<base::FilePath>& files);
// Root temp directory to store temporary files into.
base::FilePath temp_dir_path_;
// File system interface to abstract underlying file system for testing.
FileDeleterInterface* file_deleter_;
// List of files belonging to a particular request.
std::map<std::string, std::vector<base::FilePath>> request_files_;
// Actual implementation of FileDeleterInterface to delete temporary files
// on the real file system.
class FileDeleter : public TempFileManager::FileDeleterInterface {
bool DeleteFile(const base::FilePath& path) override;
} // namespace webservd