Skip to content

Commit

Permalink
Merge #3270
Browse files Browse the repository at this point in the history
Replace Qt with std in critical parts of sftp_server
  • Loading branch information
Chris Townsend authored Dec 5, 2023
2 parents 8bc1e83 + 147abc0 commit 102cd1d
Show file tree
Hide file tree
Showing 14 changed files with 674 additions and 535 deletions.
17 changes: 17 additions & 0 deletions include/multipass/file_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ namespace multipass
{
namespace fs = std::filesystem;

struct NamedFd
{
NamedFd(const fs::path& path, int fd);
~NamedFd();

fs::path path;
int fd;
};

class FileOps : public Singleton<FileOps>
{
public:
Expand Down Expand Up @@ -78,6 +87,12 @@ class FileOps : public Singleton<FileOps>
// QSaveFile operations
virtual bool commit(QSaveFile& file) const;

// posix operations
virtual std::unique_ptr<NamedFd> open_fd(const fs::path& path, int flags, int perms) const;
virtual int read(int fd, void* buf, size_t nbytes) const;
virtual int write(int fd, const void* buf, size_t nbytes) const;
virtual off_t lseek(int fd, off_t offset, int whence) const;

// std operations
virtual void open(std::fstream& stream, const char* filename, std::ios_base::openmode mode) const;
virtual std::unique_ptr<std::ostream> open_write(const fs::path& path,
Expand All @@ -93,8 +108,10 @@ class FileOps : public Singleton<FileOps>
virtual fs::path read_symlink(const fs::path& path, std::error_code& err) const;
virtual void permissions(const fs::path& path, fs::perms perms, std::error_code& err) const;
virtual fs::file_status status(const fs::path& path, std::error_code& err) const;
virtual fs::file_status symlink_status(const fs::path& path, std::error_code& err) const;
virtual std::unique_ptr<RecursiveDirIterator> recursive_dir_iterator(const fs::path& path,
std::error_code& err) const;
virtual std::unique_ptr<DirIterator> dir_iterator(const fs::path& path, std::error_code& err) const;
};
} // namespace multipass

Expand Down
27 changes: 27 additions & 0 deletions include/multipass/posix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef MULTIPASS_POSIX_H
#define MULTIPASS_POSIX_H

#ifdef MULTIPASS_PLATFORM_WINDOWS
#include <io.h>
#else
#include <unistd.h>
#endif

#endif // MULTIPASS_POSIX_H
47 changes: 45 additions & 2 deletions include/multipass/recursive_dir_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define RECURSIVE_DIR_ITERATOR_H

#include <filesystem>
#include <optional>

namespace multipass
{
Expand All @@ -31,7 +32,7 @@ class DirectoryEntry
DirectoryEntry() = default;
DirectoryEntry(const DirectoryEntry&) = default;
DirectoryEntry(DirectoryEntry&&) = default;
DirectoryEntry(const fs::directory_entry& _entry) : entry{_entry}
explicit DirectoryEntry(const fs::directory_entry& _entry) : entry{_entry}
{
}

Expand Down Expand Up @@ -240,7 +241,7 @@ class RecursiveDirIterator

virtual const DirectoryEntry& next()
{
return current = *iter++;
return current = DirectoryEntry{*iter++};
}

virtual ~RecursiveDirIterator() = default;
Expand All @@ -249,6 +250,48 @@ class RecursiveDirIterator
fs::recursive_directory_iterator iter;
DirectoryEntry current;
};

// wrapper class around std::filesystem::directory_iterator used for mocking purposes
class DirIterator
{
public:
DirIterator() = default;
DirIterator(const fs::path& path, std::error_code& err) : self{path / "."}, parent{path / ".."}, iter{path, err}
{
}

virtual bool hasNext()
{
return iter != fs::end(iter);
}

virtual const DirectoryEntry& next()
{
if (self)
{
const fs::directory_entry entry{*self};
self.reset();
return current = DirectoryEntry{entry};
}

if (parent)
{
const fs::directory_entry entry{*parent};
parent.reset();
return current = DirectoryEntry{entry};
}

return current = DirectoryEntry{*iter++};
}

virtual ~DirIterator() = default;

private:
std::optional<fs::path> self;
std::optional<fs::path> parent;
fs::directory_iterator iter;
DirectoryEntry current;
};
} // namespace multipass

#endif // RECURSIVE_DIR_ITERATOR_H
Loading

0 comments on commit 102cd1d

Please sign in to comment.