455 lines
12 KiB
HTML
455 lines
12 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
|
||
|
<title>DosSetFileLocks()--Lock and Unlock a Byte Range of an Open File</title>
|
||
|
<!-- Begin Header Records ========================================== -->
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<!-- Change History: -->
|
||
|
<!-- YYMMDD USERID Change description -->
|
||
|
<!-- Unix2 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
|
||
|
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
|
||
|
<!-- Change History: -->
|
||
|
<!-- 011022 JTROUS: Changes from API Review 1, V5R2 -->
|
||
|
<!-- 010514 JET: This file has undergone html cleanup -->
|
||
|
<!-- 020719 MFENLON: updated for QFileSvr.400 threadsafety, V5R3 -->
|
||
|
<!-- 020905 JTROUS: Changes for scan processing, V5R3a, DCR 98680 -->
|
||
|
<!-- 021118 JTROUS: Correct ERROR_GEN_FAILURE (no change flag) -->
|
||
|
<!-- 050413 JTROUS: Minor fixups, V5R4 review (no change flag) -->
|
||
|
<!-- End Header Records -->
|
||
|
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
||
|
</head>
|
||
|
<body>
|
||
|
<!-- Java sync-link -->
|
||
|
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript">
|
||
|
</script>
|
||
|
|
||
|
<a name="Top_Of_Page"></a>
|
||
|
|
||
|
<h2>DosSetFileLocks()--Lock and Unlock a Byte Range of an Open File</h2>
|
||
|
|
||
|
<div class="box" style="width: 80%;">
|
||
|
<br>
|
||
|
Syntax
|
||
|
|
||
|
<pre>
|
||
|
#define INCL_DOSERRORS
|
||
|
#define INCL_DOSFILEMGR
|
||
|
#include <os2.h>
|
||
|
|
||
|
APIRET APIENTRY DosSetFileLocks(HFILE <em>FileHandle</em>,
|
||
|
PFILELOCK <em>ppUnLockRange</em>,
|
||
|
PFILELOCK <em>ppLockRange</em>,
|
||
|
ULONG <em>ulTimeOut</em>,
|
||
|
ULONG <em>ulFlags</em>);
|
||
|
</pre>
|
||
|
|
||
|
Service Program Name: QP0LLIB1<br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
Default Public Authority: *USE<br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
Threadsafe: Conditional; see <a href="#HDRDSFLUSG">Usage Notes</a>.<br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
</div>
|
||
|
|
||
|
<p>The <strong>DosSetFileLocks()</strong> function locks and unlocks a range of
|
||
|
an open file. A non-zero range indicates that a lock or unlock request is being
|
||
|
made.</p>
|
||
|
|
||
|
<br>
|
||
|
<h3>Parameters</h3>
|
||
|
|
||
|
<dl>
|
||
|
<dt><strong><em>FileHandle</em></strong></dt>
|
||
|
|
||
|
<dd>(Input) The file descriptor of the file in which a range is to be
|
||
|
locked.</dd>
|
||
|
|
||
|
<dt><strong><em>ppUnlockRange</em></strong></dt>
|
||
|
|
||
|
<dd>(Input) Address of the structure containing the offset and length of a
|
||
|
range to be unlocked. The structure is as follows:
|
||
|
|
||
|
<dl compact>
|
||
|
<dt><em>FileOffset</em></dt>
|
||
|
|
||
|
<dd>The offset to the beginning of the range to be unlocked.</dd>
|
||
|
|
||
|
<dt><em>RangeLength</em></dt>
|
||
|
|
||
|
<dd>The length of the range to be unlocked. A value of zero means that
|
||
|
unlocking is not required.</dd>
|
||
|
</dl>
|
||
|
</dd>
|
||
|
|
||
|
<dt><strong><em>ppLockRange</em></strong></dt>
|
||
|
|
||
|
<dd>(Input) Address of the structure containing the offset and length of a
|
||
|
range to be locked. The structure is as follows:
|
||
|
|
||
|
<dl compact>
|
||
|
<dt><em>FileOffset</em></dt>
|
||
|
|
||
|
<dd>The offset to the beginning of the range to be locked.</dd>
|
||
|
|
||
|
<dt><em>RangeLength</em></dt>
|
||
|
|
||
|
<dd>The length of the range to be locked. A value of zero means that locking is
|
||
|
not required.</dd>
|
||
|
</dl>
|
||
|
</dd>
|
||
|
|
||
|
<dt><strong><em>ulTimeOut</em></strong></dt>
|
||
|
|
||
|
<dd>(Input) The maximum time, in milliseconds, that the process is to wait for
|
||
|
the requested locks.</dd>
|
||
|
|
||
|
<dt><strong><em>ulFlags</em></strong></dt>
|
||
|
|
||
|
<dd>(Input) Flags that describe the action to be taken. If any flags other than
|
||
|
those listed below are specified, the error ERROR_INVALID_PARAMETER will be
|
||
|
returned.
|
||
|
|
||
|
<p>The following values are to be specified in <em>ulFlags</em>:</p>
|
||
|
|
||
|
<dl compact>
|
||
|
<dt><em>'0x0002' or QP0L_DOSSETFILELOCKS_ATOMIC</em></dt>
|
||
|
|
||
|
<dd>Atomic. This bit defines a request for atomic locking. If this bit is set
|
||
|
to 1 and the lock range is equal to the unlock range, an atomic lock occurs. If
|
||
|
this bit is set to 1 and the lock range is not equal to the unlock range,
|
||
|
ERROR_LOCK_VIOLATION is returned.</dd>
|
||
|
|
||
|
<dt><em>'0x0001' or QP0L_DOSSETFILELOCKS_SHARE</em></dt>
|
||
|
|
||
|
<dd>Share. This bit defines the type of access that other processes may have to
|
||
|
the file range that is being locked.
|
||
|
|
||
|
<p>If this bit is set to 0 (the default), other processes have no access to the
|
||
|
locked file range. The current process has exclusive access to the locked file
|
||
|
range, which must not overlap any other locked file range.</p>
|
||
|
|
||
|
<p>If this bit is set to 1, the current process and other processes have shared
|
||
|
access to the locked file range. A file range with shared access may overlap
|
||
|
any other file range with shared access, but must not overlap any other file
|
||
|
range with exclusive access.</p>
|
||
|
</dd>
|
||
|
</dl>
|
||
|
</dd>
|
||
|
</dl>
|
||
|
|
||
|
<br>
|
||
|
<h3>Authorities</h3>
|
||
|
|
||
|
<p>No authorization is required.</p>
|
||
|
|
||
|
<br>
|
||
|
<h3>Return Value</h3>
|
||
|
|
||
|
<dl compact>
|
||
|
<dt><em>NO_ERROR (0)</em></dt>
|
||
|
|
||
|
<dd><strong>DosSetFileLocks()</strong> was successful.</dd>
|
||
|
|
||
|
<dt><em>value (non-zero)</em></dt>
|
||
|
|
||
|
<dd><strong>DosSetFileLocks()</strong> was not successful.
|
||
|
The <em>value</em> that is returned indicates the error.</dd>
|
||
|
</dl>
|
||
|
|
||
|
<br>
|
||
|
<h3>Error Conditions</h3>
|
||
|
|
||
|
<p>If <strong>DosSetFileLocks()</strong> is not successful, the value that is
|
||
|
returned is one of the following errors. The <<strong>bseerr.h</strong>>
|
||
|
header file defines these values.</p>
|
||
|
|
||
|
<dl compact>
|
||
|
<dt><em>[ERROR_GEN_FAILURE]</em></dt>
|
||
|
|
||
|
<dd>
|
||
|
<p>A general failure occurred.</p>
|
||
|
|
||
|
<p>This may result from damage in the system. Refer to messages in the job log
|
||
|
for other possible causes.</p>
|
||
|
</dd>
|
||
|
|
||
|
<dt><em>[ERROR_INVALID_HANDLE]</em></dt>
|
||
|
|
||
|
<dd>
|
||
|
<p>An invalid file handle was found.</p>
|
||
|
|
||
|
<p>The file handle passed to this function is not valid.</p>
|
||
|
</dd>
|
||
|
|
||
|
<dt><em>[ERROR_LOCK_VIOLATION]</em></dt>
|
||
|
|
||
|
<dd>
|
||
|
<p>A lock violation was found.</p>
|
||
|
|
||
|
<p>The requested lock and unlock ranges are both zero.</p>
|
||
|
</dd>
|
||
|
|
||
|
<dt><em>[ERROR_INVALID_PARAMETER]</em></dt>
|
||
|
|
||
|
<dd>
|
||
|
<p>An invalid parameter was found.</p>
|
||
|
|
||
|
<p>A parameter passed to this function is not valid.</p>
|
||
|
|
||
|
<p>The byte range specified by the offset and length in the ppUnlockRange or
|
||
|
ppLockRange parameters extends beyond 2GB minus 1 byte.</p>
|
||
|
</dd>
|
||
|
|
||
|
<dt><em>[ERROR_ATOMIC_LOCK_NOT_SUPPORTED]</em></dt>
|
||
|
|
||
|
<dd>
|
||
|
<p>The atomic lock operation is not supported.</p>
|
||
|
|
||
|
<p>The file system does not support atomic lock operations.</p>
|
||
|
</dd>
|
||
|
|
||
|
<dt><em>[ERROR_TIMER_NOT_SUPPORTED]</em></dt>
|
||
|
|
||
|
<dd>
|
||
|
<p>The lock timer value is not supported.</p>
|
||
|
|
||
|
<p>The file system does not support the lock timer value.</p>
|
||
|
</dd>
|
||
|
</dl>
|
||
|
|
||
|
<br>
|
||
|
<h3>Error Messages</h3>
|
||
|
|
||
|
<p>The system may send the following messages from this function.</p>
|
||
|
|
||
|
<table width="100%" cellpadding="5">
|
||
|
<!-- cols="15 85" -->
|
||
|
<tr>
|
||
|
<th align="left" valign="top">Message ID</th>
|
||
|
<th align="left" valign="top">Error Message Text</th>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td width="15%" valign="top">CPE3418 E</td>
|
||
|
<td width="85%" valign="top">Possible APAR condition or hardware failure.</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td valign="top">CPFA0D4 E</td>
|
||
|
<td valign="top">File system error occurred. Error number &1.</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td valign="top">CPF3CF2 E</td>
|
||
|
<td valign="top">Error(s) occurred during running of &1 API.</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr>
|
||
|
<td valign="top">CPF9872 E</td>
|
||
|
<td valign="top">Program or service program &1 in library &2 ended.
|
||
|
Reason code &3.</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
|
||
|
<br>
|
||
|
<br>
|
||
|
<h3><a name="HDRDSFLUSG">Usage Notes</a></h3>
|
||
|
|
||
|
<ol type="1">
|
||
|
<li>If this function is called
|
||
|
by a thread executing one of the scan-related exit programs (or any of its
|
||
|
created threads), it will fail with error code [ENOTSUP]. See <a href=
|
||
|
"ifsopenexit.htm">Integrated File System Scan on Open Exit Programs</a> and <a
|
||
|
href="ifscloseexit.htm">Integrated File System Scan on Close Exit Programs</a>
|
||
|
for more information.
|
||
|
<br><br>
|
||
|
</li>
|
||
|
|
||
|
<li>This function will fail with error code [ERROR_GEN_FAILURE] when all the
|
||
|
following conditions are true:
|
||
|
|
||
|
<ul>
|
||
|
<li>Where multiple threads exist in the job.</li>
|
||
|
|
||
|
<li>The object this function is operating on resides in a file system that is
|
||
|
not threadsafe. Only the following file systems are threadsafe for this
|
||
|
function:
|
||
|
|
||
|
<ul>
|
||
|
<li>"Root" (/)</li>
|
||
|
<li>QOpenSys</li>
|
||
|
<li>User-defined</li>
|
||
|
<li>QNTC</li>
|
||
|
<li>QSYS.LIB</li>
|
||
|
<li>Independent ASP QSYS.LIB</li>
|
||
|
<li>QOPT</li>
|
||
|
<li>QFileSvr.400</li>
|
||
|
|
||
|
</ul>
|
||
|
</li>
|
||
|
</ul>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li>The following file systems do not support timer values other than 0. An
|
||
|
attempt to a value other than 0 for the timer value results in an
|
||
|
ERROR_TIMER_NOT_SUPPORTED error.
|
||
|
<p>Also, the following file systems do not support the atomic locking flag. If you
|
||
|
turn on the atomic locking flag, an ERROR_ATOMIC_LOCKS_NOT_SUPPORTED error is
|
||
|
returned.</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>"Root" (/)</li>
|
||
|
<li>QOpenSys</li>
|
||
|
<li>User-Defined File System</li>
|
||
|
<li>QDLS</li>
|
||
|
<li>QOPT</li>
|
||
|
<li>QNetWare</li>
|
||
|
</ul>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li>The following file systems do not support byte range locks. An attempt to
|
||
|
use this API results in an ERROR_GEN_FAILURE error.
|
||
|
|
||
|
<ul>
|
||
|
<li>QSYS.LIB</li>
|
||
|
<li>Independent ASP QSYS.LIB</li>
|
||
|
<li>Network File System</li>
|
||
|
<li>QFileSvr.400</li>
|
||
|
|
||
|
</ul>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li>When you develop in C-based languages and this function is compiled with
|
||
|
the _LARGE_FILES macro defined, it will be mapped to <strong>
|
||
|
DosSetFileLocks64()</strong>. Additionally, the <samp>PFILELOCK</samp> data
|
||
|
type will be mapped to a type <samp>PFILELOCK64</samp>.
|
||
|
<br><br></li>
|
||
|
|
||
|
<li>Locks placed on character special files result in advisory locks. For
|
||
|
more information on advisory locking, please see the <a href="fcntl.htm">
|
||
|
fcntl()--Perform File Control Command</a>.
|
||
|
</li>
|
||
|
</ol>
|
||
|
|
||
|
<br>
|
||
|
<h3>Related Information</h3>
|
||
|
|
||
|
<ul>
|
||
|
<li>The <<strong>fcntl.h</strong>> file (see <a href="unix13.htm">Header
|
||
|
Files for UNIX-Type Functions</a>)</li>
|
||
|
|
||
|
<li>The <<strong>os2.h</strong>> file (see <a href="unix13.htm">Header
|
||
|
Files for UNIX-Type Functions</a>)</li>
|
||
|
|
||
|
<li>The <<strong>os2def.h</strong>> file (see <a href="unix13.htm">Header
|
||
|
Files for UNIX-Type Functions</a>)</li>
|
||
|
|
||
|
<li>The <<strong>bse.h</strong>> file (see <a href="unix13.htm">Header
|
||
|
Files for UNIX-Type Functions</a>)</li>
|
||
|
|
||
|
<li>The <<strong>bsedos.h</strong>> file (see <a href="unix13.htm">Header
|
||
|
Files for UNIX-Type Functions</a>)</li>
|
||
|
|
||
|
<li>The <<strong>bseerr.h</strong>> file (see <a href="unix13.htm">Header
|
||
|
Files for UNIX-Type Functions</a>)</li>
|
||
|
|
||
|
<li><a href="dossfl64.htm">DosSetFileLocks64()</a>--Lock and Unlock a Byte
|
||
|
Range of an Open File (Large File Enabled)</li>
|
||
|
</ul>
|
||
|
|
||
|
<br>
|
||
|
<h3>Example</h3>
|
||
|
|
||
|
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
|
||
|
for information pertaining to code examples.</p>
|
||
|
|
||
|
<p>The following example opens, locks, and unlocks a file.</p>
|
||
|
|
||
|
<pre>
|
||
|
#define INCL_DOSERRORS
|
||
|
#define INCL_DOSFILEMGR
|
||
|
#include <os2.h>
|
||
|
#include <stdio.h>
|
||
|
|
||
|
#define NULL_RANGE 0L
|
||
|
#define LOCK_FLAGS 0
|
||
|
|
||
|
main() {
|
||
|
char fn[]="lock.file";
|
||
|
char buf[] =
|
||
|
"Test data for locking and unlocking range of a file";
|
||
|
int fd;
|
||
|
ULONG lockTimeout = 2000; /* lock timeout of 2 seconds */
|
||
|
FILELOCK Area; /* area of file to lock/unlock */
|
||
|
|
||
|
Area.Offset = 4; /* start locking at byte 4 */
|
||
|
Area.Range = 10; /* lock 10 bytes for the file */
|
||
|
|
||
|
/* Create a file for this example */
|
||
|
fd = creat(fn, S_IWUSR | S_IRUSR);
|
||
|
/* Write some data to the file */
|
||
|
write(fd, buf, sizof(buf) -1);
|
||
|
close(fd);
|
||
|
|
||
|
/* Open the file */
|
||
|
if ((fd = open(fn, O_RDWR) < 0)
|
||
|
{
|
||
|
perror("open() error");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/* Lock a range */
|
||
|
rc = DosSetFileLocks((HFILE)fd,
|
||
|
NULL_RANGE,
|
||
|
&Area,
|
||
|
&LockTimeout,
|
||
|
LOCK_FLAGS);
|
||
|
if(rc != 0) /* Lock failed */
|
||
|
{
|
||
|
perror("DosSetFileLocks() error");
|
||
|
}
|
||
|
|
||
|
|
||
|
/* Unlock a range */
|
||
|
rc = DosSetFileLocks((HFILE)fd,
|
||
|
&Area,
|
||
|
NULL_RANGE,
|
||
|
&LockTimeout,
|
||
|
LOCK_FLAGS);
|
||
|
if(rc != 0) /* Unlock failed */
|
||
|
{
|
||
|
perror("DosSetFileLocks() error");
|
||
|
}
|
||
|
|
||
|
close(fd);
|
||
|
unlink(fn);
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<hr>
|
||
|
API introduced: V4R2
|
||
|
|
||
|
<hr>
|
||
|
<center>
|
||
|
<table cellpadding="2" cellspacing="2">
|
||
|
<tr align="center">
|
||
|
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
|
||
|
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</center>
|
||
|
</body>
|
||
|
</html>
|
||
|
|