📄 bothlock.cpp
字号:
#include <stdio.h>#include <sys/file.h>#include <unistd.h>#include "bothlock.h"#include <sys/types.h>#include <pwd.h>#include "webemail.h"int lock_flock(FILE *file);int lock_fcntl(FILE *file, char type);int unlock_flock(FILE *file);int unlock_fcntl(FILE *file);int bothlock(FILE *file, char type){ if (lock_flock(file)) return 1; if (lock_fcntl(file, type)) return 2; return 0;}int bothunlock(FILE *file){ fsync(fileno(file)); if (unlock_flock(file)) return 1; if (unlock_fcntl(file)) return 2; return 0;}int lock_flock(FILE *file){ if (flock(fileno(file), LOCK_EX)<0) return 1; return 0; }int lock_fcntl(FILE *file, char type){ struct flock lock; if (type == 'w') lock.l_type = F_WRLCK; else if (type == 'r') lock.l_type = F_RDLCK; else return 1; lock.l_whence = SEEK_SET; lock.l_start = lock.l_len=0; if (fcntl(fileno(file), F_SETLKW, &lock) < 0) { //perror("fcntl"); //printf("Error using fcntl"); return 1; } return 0;}int unlock_flock(FILE *file){ if (flock(fileno(file), LOCK_UN)<0) return 1; return 0;}int unlock_fcntl(FILE *file){ struct flock lock; lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = lock.l_len=0; if (fcntl(fileno(file), F_SETLKW, &lock) < 0) { //perror("fcntl"); //printf("Error using fcntl"); return 1; } return 0;}FILE* processlock(){ passwd *info; char lockname[400]; FILE *file; info=getpwnam(globaluser); snprintf(lockname, 400, "%s/.webmail/flock", info->pw_dir); file = fopen(lockname, "w"); if (file == NULL) { error("processlock(): Can't open flock"); return NULL; } bothlock(file, 'w'); return file;}void processunlock(FILE * file){ bothunlock(file); fclose(file); return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -