Skip to content

Commit

Permalink
Merge pull request #653 from Chilledheart/gtk_remove_writefilewith_co…
Browse files Browse the repository at this point in the history
…ntent

gtk: remove WriteFileWithContent
  • Loading branch information
Chilledheart committed Jan 12, 2024
2 parents 77bd670 + c758d6b commit 5485c71
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/config/config_impl_local.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ConfigImplLocal : public ConfigImpl {

std::string json_content = root_.dump(4);
if (static_cast<ssize_t>(json_content.size()) !=
WriteFileWithBuffer(path_, json_content.c_str(), json_content.size())) {
WriteFileWithBuffer(path_, json_content)) {
LOG(WARNING) << "failed to write to path: \"" << path_
<< " with content \"" << json_content;
return false;
Expand Down
6 changes: 2 additions & 4 deletions src/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,14 @@ static bool WriteFileDescriptor(int fd, std::string_view data) {
}

ssize_t WriteFileWithBuffer(const std::string& path,
const char* buf,
size_t buf_len) {
std::string_view buf) {
int fd = HANDLE_EINTR(::open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
if (fd < 0) {
return false;
}

ssize_t ret = WriteFileDescriptor(fd, std::string_view(buf, buf_len))
? buf_len : -1;
ssize_t ret = WriteFileDescriptor(fd, buf) ? buf.length() : -1;

if (IGNORE_EINTR(close(fd)) < 0) {
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#ifndef YASS_UTILS
#define YASS_UTILS


#include <stdint.h>
#include <string>
#include <string_view>
#include <thread>
#include <wchar.h>
#include <optional>
Expand Down Expand Up @@ -171,7 +171,7 @@ using ssize_t = ptrdiff_t;
#endif

ssize_t ReadFileToBuffer(const std::string& path, char* buf, size_t buf_len);
ssize_t WriteFileWithBuffer(const std::string& path, const char* buf, size_t buf_len);
ssize_t WriteFileWithBuffer(const std::string& path, std::string_view buf);
PlatformFile OpenReadFile(const std::string &path);
#ifdef _WIN32
PlatformFile OpenReadFile(const std::wstring& path);
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TEST(UtilsTest, ReadFileAndWrite4K) {
std::string tmp = tmp_dir / tmp_name;
#endif

ASSERT_TRUE(WriteFileWithBuffer(tmp, buf.c_str(), buf.size()));
ASSERT_TRUE(WriteFileWithBuffer(tmp, buf));
ASSERT_TRUE(ReadFileToBuffer(tmp, buf2.data(), buf2.size()+1));
ASSERT_EQ(buf, buf2);

Expand Down
7 changes: 3 additions & 4 deletions src/core/utils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,18 +783,17 @@ ssize_t ReadFileToBuffer(const std::string& path, char* buf, size_t buf_len) {
return read;
}

ssize_t WriteFileWithBuffer(const std::string& path,
const char* buf,
size_t buf_len) {
ssize_t WriteFileWithBuffer(const std::string& path, std::string_view buf) {
DWORD written;
const size_t buf_len = buf.size();
HANDLE hFile = ::CreateFileW(SysUTF8ToWide(path).c_str(), GENERIC_WRITE,
0, nullptr, CREATE_ALWAYS, 0, nullptr);
if (hFile == INVALID_HANDLE_VALUE) {
DPLOG(WARNING) << "WriteFile failed for path " << path;
return -1;
}

if (!::WriteFile(hFile, buf, buf_len, &written, nullptr)) {
if (!::WriteFile(hFile, buf.data(), buf_len, &written, nullptr)) {
// WriteFile failed.
DPLOG(WARNING) << "writing file " << path << " failed";
::CloseHandle(hFile);
Expand Down
2 changes: 0 additions & 2 deletions src/gtk/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,4 @@ class Dispatcher {

void SetUpGLibLogHandler();

#define DEFAULT_AUTOSTART_NAME "yass"

#endif // YASS_GUI_UTILS
39 changes: 14 additions & 25 deletions src/gtk/utils_gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "core/process_utils.hpp"
#include "config/config.hpp"

#include <string_view>
#include <absl/strings/str_cat.h>
#include <absl/strings/str_format.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
Expand All @@ -26,36 +28,20 @@

using namespace yass;

static const char* kAutoStartFileContent =
static constexpr char kDefaultAutoStartName[] = "yass";

static constexpr std::string_view kAutoStartFileContent =
"[Desktop Entry]\n"
"Type=Application\n"
"Name=yass\n"
"Comment=Yet Another Shadow Socket is a lightweight and secure http/socks4/socks5 proxy for embedded devices and low end boxes.\n"
"Icon=yass\n"
"Exec=yass --background\n"
"Exec=%s --background\n"
"Terminal=false\n"
"Categories=Network;GTK;Utility\n";

namespace {

bool WriteFileWithContent(const std::string& path, std::string_view context) {
int fd = ::open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT,
S_IRUSR | S_IWUSR);
if (fd < 0) {
return false;
}
ssize_t written = ::write(fd, context.data(), context.size());

if (written != static_cast<long>(context.size())) {
::close(fd);
return false;
}
if (::close(fd) < 0) {
return false;
}
return true;
}

// followed https://github.com/qt/qtbase/blob/7fe1198f6edb40de2299272c7523d85d7486598b/src/corelib/io/qstandardpaths_unix.cpp#L218
std::string GetConfigDir() {
const char *config_dir_ptr = getenv("XDG_CONFIG_HOME");
Expand All @@ -82,15 +68,15 @@ bool IsKDE() {

bool Utils::GetAutoStart() {
std::string autostart_desktop_path =
absl::StrCat(GetAutostartDirectory(), "/" ,
DEFAULT_AUTOSTART_NAME , ".desktop");
absl::StrCat(GetAutostartDirectory(), "/",
kDefaultAutoStartName, ".desktop");
return IsFile(autostart_desktop_path);
}

void Utils::EnableAutoStart(bool on) {
std::string autostart_desktop_path =
absl::StrCat(GetAutostartDirectory(), "/" ,
DEFAULT_AUTOSTART_NAME , ".desktop");
absl::StrCat(GetAutostartDirectory(), "/",
kDefaultAutoStartName, ".desktop");
if (!on) {
if (!RemoveFile(autostart_desktop_path)) {
PLOG(WARNING)
Expand All @@ -111,7 +97,10 @@ void Utils::EnableAutoStart(bool on) {
}

// write to target
if (!WriteFileWithContent(autostart_desktop_path, kAutoStartFileContent)) {
std::string executable_path = "yass";
GetExecutablePath(&executable_path);
std::string desktop_entry = absl::StrFormat(kAutoStartFileContent, executable_path);
if (!WriteFileWithBuffer(autostart_desktop_path, desktop_entry)) {
PLOG(WARNING) << "Internal error: unable to create autostart file";
}

Expand Down

0 comments on commit 5485c71

Please sign in to comment.