source: main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/filelock.cpp@ 27627

Last change on this file since 27627 was 27627, checked in by ak19, 11 years ago

File lock code currently experimental, so not in the main greenstone build sequence.

File size: 1.1 KB
Line 
1
2#ifdef _MSC_VER
3// Windows implementation
4HANDLE hFile = CreateFile(_T("c:\\file.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
5
6#else
7
8/* header file for mkdir */
9#include <sys/stat.h>
10#include <sys/types.h>
11
12// assume Unix
13// lock a file on linux
14// [hs, 2 july 2010]
15// - modified to create a locl file local to the collection [jmt12]
16int lock ()
17{
18 string file_path ("");
19 char *collect_dir = getenv ("GSDLCOLLECTDIR");
20 if (collect_dir != NULL)
21 {
22 file_path += collect_dir;
23 }
24 file_path += "/tmp";
25 if ( access( file_path.c_str(), 00 ) != 0 )
26 {
27 mkdir(file_path.c_str(), 00777);
28 }
29 file_path += "/gdb.lock";
30 ///out << "txt2dbl::lock(" << file_path << ") => ";
31 int fd2 = open (file_path.c_str(), O_CREAT|O_RDWR, 00644);
32 close (fd2);
33 int fd = open (file_path.c_str(), O_RDWR);
34 flock lock = {F_WRLCK, SEEK_SET, 0, 0, 0};
35 fcntl (fd, F_SETLKW, &lock);
36 ///out << "locked!" << endl;
37 return fd;
38}
39
40// unlock a file on linux
41// [hs, 2 july 2010]
42int unlock ( int fd )
43{
44 flock lock1 = {F_UNLCK, SEEK_SET, 0, 0, 0};
45 fcntl (fd, F_SETLKW, &lock1);
46 return 0;
47}
48#endif
Note: See TracBrowser for help on using the repository browser.