1 Star 0 Fork 8

黑皮/Xipkpublib_Update

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
XipFileLock.c 4.13 KB
一键复制 编辑 原始数据 按行查看 历史
suitianmou 提交于 2016-07-14 09:04 . mofidiy some bug
/*******************#XipFileLock#******************
*函 数 名: XipFileLock
*函数功能: 文件锁
*函数作者: zhangl
*版 本 号: 1.0.0
*创建日期: 20160318
fullname文件全路径
*修改标记: <1>
*修改人员: suitianmou
*修改日期: 2016.07.13
*修改内容:
将加锁、解锁和检查分成不同的函数
******************************************************/
#include "kernel/syspub.h"
#include "kernel/xipcmstc.h"
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
int XipFileWRLock( char * fullname)
{
int fd = 0;
struct flock lock;
int ret = 0;
memset( &lock, 0x00, sizeof(lock));
if( fullname == NULL)
{
XIPLOG("E", "传入参数为NULL,不可以加/解/检查锁!");
return -5;
}
fd = open( fullname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
if( fd <= 0)
{
XIPLOG("E", "打开文件错误!!![%s][%s]", fullname, strerror(errno));
return -10;
}
lock.l_type=F_WRLCK; /*读写锁*/
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
ret = fcntl( fd, F_SETLK, &lock);
if( ret < 0)
{
XIPLOG("E","文件加F_WRLCK锁失败[%s]!!", strerror(errno));
return -15;
}
memset( &lock, 0x00, sizeof( lock));
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
ret = fcntl( fd, F_GETLK, &lock);
/*不关闭文件,否则加锁失效*
close(fd);
**/
return 0;
}
#if 0 /*RD锁检查不出来TODO*/
int XipFileRDLock( char * fullname)
{
int fd = 0;
struct flock lock;
int ret = 0;
memset( &lock, 0x00, sizeof(lock));
if( fullname == NULL)
{
XIPLOG("E", "传入参数为NULL,不可以加/解/检查锁!");
return -5;
}
fd = open( fullname, O_RDONLY|O_CREAT, S_IRUSR|S_IWUSR);
if( fd <= 0)
{
XIPLOG("E", "打开文件错误!!![%s][%s]", fullname, strerror(errno));
return -10;
}
lock.l_type=F_RDLCK; /*只读锁*/
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
ret = fcntl( fd, F_SETLK, &lock);
if( ret < 0)
{
XIPLOG("E","文件加F_RDLCK锁失败[%s]!!", strerror(errno));
return -15;
}
/*不关闭文件,否则加锁失效*
close(fd);
**/
return 0;
}
#endif
int XipFileUNLock( char * fullname)
{
int fd = 0;
struct flock lock;
int ret = 0;
memset( &lock, 0x00, sizeof(lock));
if( fullname == NULL)
{
XIPLOG("E", "传入参数为NULL,不可以加/解/检查锁!");
return -5;
}
fd = open( fullname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
if( fd <= 0)
{
XIPLOG("E", "打开文件错误!!![%s][%s]", fullname, strerror(errno));
return -10;
}
lock.l_type=F_UNLCK; /*解锁*/
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
ret = fcntl( fd, F_SETLK, &lock);
if( ret < 0)
{
XIPLOG("E","文件解锁失败[%s]!!", strerror(errno));
return -15;
}
/*解锁后关闭文件*/
close(fd);
return 0;
}
/**************************************
* 获取文件锁
* 返回: 0无文件锁, 1有读写文件锁
*
**************************************/
int XipFileChkLock( char *fullname )
{
int fd = 0;
struct flock lock;
int ret = 0;
memset( &lock, 0x00, sizeof(lock));
if( fullname == NULL)
{
XIPLOG("E", "传入参数为NULL,不可以加/解/检查锁!");
return -5;
}
fd = open( fullname, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
if( fd <= 0)
{
XIPLOG("E", "打开文件错误!!![%s][%s]", fullname, strerror(errno));
return -10;
}
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
ret = fcntl( fd, F_GETLK, &lock);
if( ret < 0)
{
XIPLOG("E","文件锁获取失败[%s]!!", strerror(errno));
return -15;
}
if (lock.l_type == F_WRLCK) /*读写锁*/
{
close(fd);
return 1;
}
else if( lock.l_type == F_RDLCK) /*只读锁*/
{
close(fd);
return 2;
}
/*关闭文件*/
close(fd);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LaoGun/Xipkpublib_Update.git
git@gitee.com:LaoGun/Xipkpublib_Update.git
LaoGun
Xipkpublib_Update
Xipkpublib_Update
master

搜索帮助