Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#422: request shared resources no longer works #464

Merged
merged 6 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [#442] fix unittest fail under 32-bit system.
* [#439] try to fix compile problem under Hurd.
* [#447] fix bug: use defined icon no longer works.
* [#422] fix bug: request shared resource no longer works.
* [#441] use `GtkHeaderBar` under Linux.
* [#462] when recv file, if file exist, save to `foo (1).ext` (was `1_foo.ext`).
* Translation updated
Expand Down
2 changes: 2 additions & 0 deletions src/api/iptux-core/Models.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class FileInfo {
FileInfo& operator=(const FileInfo& fileInfo);

bool operator==(const FileInfo& rhs) const;
bool isExist() const;
void ensureFilesizeFilled();

uint32_t fileid; ///< 唯一标识
uint32_t packetn; ///< 包编号
Expand Down
16 changes: 16 additions & 0 deletions src/iptux-core/Models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
#include "iptux-core/Models.h"

#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <sstream>
#include <unistd.h>

#include "iptux-core/internal/AnalogFS.h"
#include "iptux-core/internal/ipmsg.h"
#include "iptux-utils/utils.h"

Expand Down Expand Up @@ -128,6 +131,7 @@ FileInfo::FileInfo()
filectime(0),
filemtime(0),
filenum(0) {}

FileInfo::~FileInfo() {
g_free(filepath);
}
Expand All @@ -145,6 +149,18 @@ FileInfo::FileInfo(const FileInfo& f)
filepath = g_strdup(f.filepath);
}

bool FileInfo::isExist() const {
return g_access(filepath, F_OK) != -1;
}

void FileInfo::ensureFilesizeFilled() {
if (filesize >= 0) {
return;
}
AnalogFS afs;
filesize = afs.ftwsize(filepath);
}

MsgPara::MsgPara(CPPalInfo pal)
: stype(MessageSourceType::PAL),
btype(GROUP_BELONG_TYPE_REGULAR),
Expand Down
21 changes: 21 additions & 0 deletions src/iptux-core/ModelsTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gtest/gtest.h"

#include "iptux-core/Models.h"
#include "iptux-utils/TestHelper.h"
#include "iptux-utils/utils.h"

using namespace std;
Expand Down Expand Up @@ -74,3 +75,23 @@ TEST(MsgPara, getSummary) {
msg.dtlist.push_back(ChipData(MessageContentType::PICTURE, "foobar"));
EXPECT_EQ(msg.getSummary(), "Received an image");
}

TEST(FileInfo, isExist) {
auto path1 = testDataPath("hexdumptest.dat");
FileInfo f;
f.filepath = g_strdup(path1.c_str());
ASSERT_TRUE(f.isExist());

auto path2 = testDataPath("hexdumptest-notexist.dat");
FileInfo f2;
f2.filepath = g_strdup(path2.c_str());
ASSERT_FALSE(f2.isExist());
}

TEST(FileInfo, ensureFilesizeFilled) {
auto path1 = testDataPath("hexdumptest.dat");
FileInfo f;
f.filepath = g_strdup(path1.c_str());
f.ensureFilesizeFilled();
ASSERT_EQ(f.filesize, 256);
}
26 changes: 10 additions & 16 deletions src/iptux-core/internal/SendFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
#include <cinttypes>
#include <cstring>
#include <memory>

#include <sys/socket.h>
#include <unistd.h>

#include "iptux-core/internal/AnalogFS.h"
#include "iptux-core/internal/Command.h"
#include "iptux-core/internal/SendFileData.h"
#include "iptux-utils/output.h"
Expand Down Expand Up @@ -94,12 +92,10 @@ void SendFile::RequestDataEntry(CoreThread* coreThread,
LOG_INFO("Pal not exist: %s", inAddrToString(addr.sin_addr).c_str());
return;
}

/* 发送文件数据 */
// /**
// *文件信息可能被删除或修改,必须单独复制一份.
// */
// file->fileown = pal;
if (!file->fileown) {
// for public shared file, there need one owner
file->fileown = coreThread->getMe();
}
SendFile(coreThread).ThreadSendFile(sock, file);
}

Expand All @@ -112,7 +108,6 @@ void SendFile::RequestDataEntry(CoreThread* coreThread,
void SendFile::SendFileInfo(PPalInfo pal,
uint32_t opttype,
vector<FileInfo>& fileInfos) {
AnalogFS afs;
Command cmd(*coreThread);
char buf[MAX_UDPLEN];
size_t len;
Expand All @@ -126,15 +121,15 @@ void SendFile::SendFileInfo(PPalInfo pal,
/* 将文件信息写入缓冲区 */
for (FileInfo& fileInfo : fileInfos) {
auto file = &fileInfo;
if (access(file->filepath, F_OK) == -1) {
if (!fileInfo.isExist()) {
continue;
}
fileInfo.ensureFilesizeFilled();
name = ipmsg_get_filename_pal(file->filepath); //获取面向好友的文件名
file->packetn = cmd.Packetn();
snprintf(ptr, MAX_UDPLEN - len,
"%" PRIu32 ":%s:%" PRIx64 ":%" PRIx32 ":%x:\a",
file->fileid, name, file->filesize, file->filectime,
(unsigned int)file->fileattr);
"%" PRIu32 ":%s:%" PRIx64 ":%" PRIx32 ":%x:\a", file->fileid, name,
file->filesize, file->filectime, (unsigned int)file->fileattr);
g_free(name);
len += strlen(ptr);
ptr = buf + len;
Expand All @@ -153,17 +148,16 @@ void SendFile::SendFileInfo(PPalInfo pal,
void SendFile::BcstFileInfo(const std::vector<const PalInfo*>& pals,
uint32_t opttype,
const std::vector<FileInfo*>& files) {
AnalogFS afs;
Command cmd(*coreThread);

for (auto pal : pals) {
vector<string> buffer;
for (auto file : files) {
if (!(file->fileown->GetKey() == pal->GetKey()))
continue;
if (access(file->filepath, F_OK) == -1)
if (!file->isExist())
continue;
file->filesize = afs.ftwsize(file->filepath); //不得不计算文件长度了
file->ensureFilesizeFilled();
file->packetn = cmd.Packetn();

buffer.push_back(Command::encodeFileInfo(*file));
Expand Down
2 changes: 2 additions & 0 deletions src/iptux-core/internal/SendFileData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ void SendFileData::SendRegularFile() {
return;
}

file->ensureFilesizeFilled();

/* 发送文件数据 */
gettimeofday(&filetime, NULL);
finishsize = SendData(fd, file->filesize);
Expand Down