int sp_lockfile(spfile *f, char *path) { f->creat = 0; f->fd = open(path, O_CREAT|O_WRONLY, 0600); if (spunlikely(f->fd == -1)) return -1; f->file = sp_strdup(f->a, path); if (spunlikely(f->file == NULL)) { close(f->fd); f->fd = -1; return -1; } struct flock l; memset(&l, 0, sizeof(l)); l.l_whence = SEEK_SET; l.l_start = 0; l.l_len = 0; l.l_type = F_WRLCK; int rc = fcntl(f->fd, F_SETLK, &l); if (spunlikely(rc == -1)) { sp_fileclose(f); return 1; } return 0; }
int sp_mapclose(spfile *f) { int rc = sp_mapcut(f); if (spunlikely(rc == -1)) return -1; if (f->map) { rc = sp_unmap(f); if (spunlikely(rc == -1)) return -1; } return sp_fileclose(f); }
int sp_lockfile(spfile *f, char *path) { f->creat = 0; #ifdef _MSC_VER f->fd = sp_win_openfileshare(path, O_CREAT | O_WRONLY, 0); if (f->fd == -1 && GetLastError() == ERROR_SHARING_VIOLATION) return 1; // already locked #else f->fd = open(path, O_CREAT | O_WRONLY, 0600); #endif if (spunlikely(f->fd == -1)) return -1; f->file = sp_strdup(f->a, path); if (spunlikely(f->file == NULL)) { close(f->fd); f->fd = -1; return -1; } #ifdef _MSC_VER int rc = LockFile((HANDLE)_get_osfhandle(f->fd), 0, 0, 0xffffffff, 0xffffffff); if (spunlikely(rc == 0)) { sp_fileclose(f); return 1; } return 0; #else struct flock l; memset(&l, 0, sizeof(l)); l.l_whence = SEEK_SET; l.l_start = 0; l.l_len = 0; l.l_type = F_WRLCK; int rc = fcntl(f->fd, F_SETLK, &l); if (spunlikely(rc == -1)) { sp_fileclose(f); return 1; } return 0; #endif }
int sp_unlockfile(spfile *f) { if (spunlikely(f->fd == -1)) return 0; struct flock l; memset(&l, 0, sizeof(l)); l.l_whence = SEEK_SET; l.l_start = 0; l.l_len = 0; l.l_type = F_UNLCK; fcntl(f->fd, F_SETLK, &l); unlink(f->file); return sp_fileclose(f); }
int sp_unlockfile(spfile *f) { if (spunlikely(f->fd == -1)) return 0; #ifdef _MSC_VER UnlockFile((HANDLE)_get_osfhandle(f->fd), 0, 0, 0xffffffff, 0xffffffff); #else struct flock l; memset(&l, 0, sizeof(l)); l.l_whence = SEEK_SET; l.l_start = 0; l.l_len = 0; l.l_type = F_UNLCK; fcntl(f->fd, F_SETLK, &l); #endif unlink(f->file); return sp_fileclose(f); }
int sp_logclose(spfile *f) { return sp_fileclose(f); }