locking file

abdeslam benzeghioua abenzeghioua at citranet.fr
Thu Apr 27 14:22:21 GMT 2006


 Hello,

 I have a shared file in unix system and we used samba to acces this file
 from Windows XP
1. I can lock this file from Windows and anothers process can't acces this
file from Windows
   but from unix any process can read and write in this file.

2. I can lock this file from unix and windows program can't edit and write
it.

 Please can you tell me how to lock file from unix or from windows in
 exclusive acces.

I send you my smb.conf
We use samba : version: 2.2.8


program windows
---------------
...
	HANDLE hFile;
	HANDLE hAppend;
	DWORD  dwBytesRead, dwBytesWritten, dwPos;
	BYTE   buff[4096];
	int result;

	// Open the existing file.

	hFile = CreateFile(filename, // open One.txt
			  GENERIC_READ,             // open for reading
			  0,                        // do not share
			  NULL,                     // no security
			  OPEN_EXISTING,            // existing file only
			  FILE_ATTRIBUTE_NORMAL,    // normal file
			  NULL);                    // no attr. template

	if (hFile == INVALID_HANDLE_VALUE)
	{
	   printf("Could not open One.txt.");
	   return;
	}
	bool bread = ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL);
	if (!bread)
	{
		AfxMessageBox("can't read file or are locked");
	}
	else
	{
		result = LockFile(hFile,0, 0, dwBytesRead, 0);
	}
...
...

--------------------------------------------------------

program unix
------------
int main(int argc, char *argv[])
{
                     /* l_type   l_whence  l_start  l_len  l_pid   */
    struct flock fl = { F_WRLCK, SEEK_SET, 0,       0,     0 };
    int fd;
    char filename[256];

    fl.l_pid = getpid();

    if (argc > 1)
    {
        fl.l_type = F_WRLCK; /*F_RDLCK;*/
       strcpy(filename, argv[1]);
    }
    else
       return -1;
    if ((fd = open(filename, O_RDWR)) == -1) {
        perror("open");
        exit(1);
    }
    if (!readfile(filename))
    {
	printf("can't read file or are locked");
      close(fd);
      exit(0);
    }
    printf("Press <RETURN> to try to get lock: ");
    getchar();
    printf("Trying to get lock...");

    if (fcntl(fd, F_SETLKW, &fl) == -1) {
        perror("fcntl");
        exit(1);
    }

    printf("got lock\n");
    printf("Press <RETURN> to release lock: ");
    getchar();

    fl.l_type = F_UNLCK;  /* set to unlock same region */

    if (fcntl(fd, F_SETLK, &fl) == -1) {
        perror("fcntl");
        exit(1);
    }

    printf("Unlocked.\n");
    close(fd);
}


Thanks,
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.344 / Virus Database: 268.4.6/324 - Release Date: 25/04/2006
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smb.conf
Type: application/octet-stream
Size: 1257 bytes
Desc: not available
Url : http://lists.samba.org/archive/samba-technical/attachments/20060427/11ac314f/smb.obj


More information about the samba-technical mailing list