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

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

renamed winlock to filelock as it's not windows specific.

File size: 1.0 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// assume Unix
8// lock a file on linux
9// [hs, 2 july 2010]
10// - modified to create a locl file local to the collection [jmt12]
11int lock ()
12{
13 string file_path ("");
14 char *collect_dir = getenv ("GSDLCOLLECTDIR");
15 if (collect_dir != NULL)
16 {
17 file_path += collect_dir;
18 }
19 file_path += "/tmp";
20 if ( access( file_path.c_str(), 00 ) != 0 )
21 {
22 mkdir(file_path.c_str(), 00777);
23 }
24 file_path += "/gdb.lock";
25 ///out << "txt2dbl::lock(" << file_path << ") => ";
26 int fd2 = open (file_path.c_str(), O_CREAT|O_RDWR, 00644);
27 close (fd2);
28 int fd = open (file_path.c_str(), O_RDWR);
29 flock lock = {F_WRLCK, SEEK_SET, 0, 0, 0};
30 fcntl (fd, F_SETLKW, &lock);
31 ///out << "locked!" << endl;
32 return fd;
33}
34
35// unlock a file on linux
36// [hs, 2 july 2010]
37int unlock ( int fd )
38{
39 flock lock1 = {F_UNLCK, SEEK_SET, 0, 0, 0};
40 fcntl (fd, F_SETLKW, &lock1);
41 return 0;
42}
43#endif
Note: See TracBrowser for help on using the repository browser.