如何将lockf C调用移植到Android?

问题描述 投票:0回答:1

请帮助我们将

lockf
C 调用移植到 Android。这里的情况被
#ifndef
/
#endif
行包围:https://github.com/PurpleI2P/i2pd/blob/710a35993db02644f7f28dbee739fe1e7a68a5a5/daemon/UnixDaemon.cpp#L164

我已经按照SO答案[2]的建议尝试了

flock
,但本地Android(ndk v.
flock
)上的
23.2.8568313
似乎是一个
struct
,而不是一个函数。

这里 [1] 有关于每进程文件和目录锁定的内容,现在将进行实验......

[1] https://stackoverflow.com/a/10308744/529442

[2] https://stackoverflow.com/a/24170314/529442

android c android-ndk
1个回答
0
投票

受@IanAbbott 评论启发的工作示例:

    std::string pidfile="/storage/emulated/0/locktest"; //make sure it's writable for you
    auto pidFH = open(pidfile.c_str(), O_RDWR | O_CREAT, 0600);
    if (pidFH < 0)
    {
        std::cerr << "Could not create locktest file " << pidfile << ": " << strerror(errno) << std::endl;
        return 1;
    }

    struct flock fl;
    fl.l_len = 0;
    fl.l_type = F_WRLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;

    if (fcntl(pidFH, F_SETLK, &fl) != 0)
    {
        std::cerr << "Could not lock locktest file " << pidfile << ": " << strerror(errno) << std::endl;
        return 2;
    }
© www.soinside.com 2019 - 2024. All rights reserved.