1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8" />
6 <meta name=
"generator" content=
"AsciiDoc 8.4.5" />
7 <title>lockfile API
</title>
8 <style type=
"text/css">
10 p
, li
, dt
, dd
, div
, pre
, h1
, h2
, h3
, h4
, h5
, h6
{
12 border: 1px solid red;
17 margin: 1em 5% 1em 5%;
22 text-decoration: underline
;
42 h1
, h2
, h3
, h4
, h5
, h6
{
44 font-family: sans-serif
;
51 border-bottom: 2px solid silver
;
69 border: 1px solid silver
;
88 font-family: sans-serif
;
94 span#revnumber
, span#revdate
, span#revremark
{
95 font-family: sans-serif
;
99 font-family: sans-serif
;
101 border-top: 2px solid silver
;
107 padding-bottom: 0.5em;
111 padding-bottom: 0.5em;
116 margin-bottom: 1.5em;
118 div
.tableblock
, div
.imageblock
, div
.exampleblock
, div
.verseblock
,
119 div
.quoteblock
, div
.literalblock
, div
.listingblock
, div
.sidebarblock
,
120 div
.admonitionblock
{
122 margin-bottom: 1.5em;
124 div
.admonitionblock
{
126 margin-bottom: 2.5em;
129 div
.content
{ /* Block element content. */
133 /* Block element titles. */
134 div
.title
, caption
.title
{
136 font-family: sans-serif
;
140 margin-bottom: 0.5em;
146 td div
.title:first-child
{
149 div
.content div
.title:first-child
{
152 div
.content
+ div
.title
{
156 div
.sidebarblock
> div
.content
{
158 border: 1px solid silver
;
162 div
.listingblock
> div
.content
{
163 border: 1px solid silver
;
172 div
.quoteblock
> div
.attribution
{
181 div
.verseblock
> div
.content
{
184 div
.verseblock
> div
.attribution
{
188 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
189 div
.verseblock
+ div
.attribution
{
193 div
.admonitionblock
.icon
{
197 text-decoration: underline
;
199 padding-right: 0.5em;
201 div
.admonitionblock td
.content
{
203 border-left: 2px solid silver
;
206 div
.exampleblock
> div
.content
{
207 border-left: 2px solid silver
;
211 div
.imageblock div
.content
{ padding-left: 0; }
212 span
.image img
{ border-style: none
; }
213 a
.image:visited
{ color: white
; }
217 margin-bottom: 0.8em;
230 list-style-position: outside
;
233 list-style-type: decimal
;
236 list-style-type: lower-alpha
;
239 list-style-type: upper-alpha
;
242 list-style-type: lower-roman
;
245 list-style-type: upper-roman
;
248 div
.compact ul
, div
.compact ol
,
249 div
.compact p
, div
.compact p
,
250 div
.compact div
, div
.compact div
{
252 margin-bottom: 0.1em;
255 div
.tableblock
> table
{
256 border: 3px solid
#527bbd;
259 font-family: sans-serif
;
271 /* Because the table frame attribute is overriden by CSS in most browsers. */
272 div
.tableblock
> table
[frame
="void"] {
275 div
.tableblock
> table
[frame
="hsides"] {
276 border-left-style: none
;
277 border-right-style: none
;
279 div
.tableblock
> table
[frame
="vsides"] {
280 border-top-style: none
;
281 border-bottom-style: none
;
287 margin-bottom: 0.8em;
290 padding-bottom: 15px;
292 dt
.hdlist1
.strong
, td
.hdlist1
.strong
{
298 padding-right: 0.8em;
304 div
.hdlist
.compact tr
{
314 div#footer-badges
{ display: none
; }
319 font-family: sans-serif
;
323 margin-bottom: 0.1em;
326 div
.toclevel1
, div
.toclevel2
, div
.toclevel3
, div
.toclevel4
{
342 /* Workarounds for IE6's broken and incomplete CSS2. */
344 div
.sidebar-content
{
346 border: 1px solid silver
;
349 div
.sidebar-title
, div
.image-title
{
351 font-family: sans-serif
;
354 margin-bottom: 0.5em;
357 div
.listingblock div
.content
{
358 border: 1px solid silver
;
363 div
.quoteblock-attribution
{
368 div
.verseblock-content
{
371 div
.verseblock-attribution
{
376 div
.exampleblock-content
{
377 border-left: 2px solid silver
;
381 /* IE6 sets dynamically generated links as visited. */
382 div#toc
a:visited
{ color: blue
; }
387 <h1>lockfile API
</h1>
390 <div class=
"sectionbody">
391 <div class=
"paragraph"><p>The lockfile API serves two purposes:
</p></div>
392 <div class=
"ulist"><ul>
395 Mutual exclusion. When we write out a new index file, first
396 we create a new file
<tt>$GIT_DIR/index.lock
</tt>, write the new
397 contents into it, and rename it to the final destination
398 <tt>$GIT_DIR/index
</tt>. We try to create the
<tt>$GIT_DIR/index.lock
</tt>
399 file with O_EXCL so that we can notice and fail when somebody
400 else is already trying to update the index file.
405 Automatic cruft removal. After we create the
"lock" file, we
406 may decide to
<tt>die()
</tt>, and we would want to make sure that we
407 remove the file that has not been committed to its final
408 destination. This is done by remembering the lockfiles we
409 created in a linked list and cleaning them up from an
410 <tt>atexit(
3)
</tt> handler. Outstanding lockfiles are also removed
411 when the program dies on a signal.
417 <h2 id=
"_the_functions">The functions
</h2>
418 <div class=
"sectionbody">
419 <div class=
"dlist"><dl>
421 hold_lock_file_for_update
425 Take a pointer to
<tt>struct lock_file
</tt>, the filename of
426 the final destination (e.g.
<tt>$GIT_DIR/index
</tt>) and a flag
427 <tt>die_on_error
</tt>. Attempt to create a lockfile for the
428 destination and return the file descriptor for writing
429 to the file. If
<tt>die_on_error
</tt> flag is true, it dies if
430 a lock is already taken for the file; otherwise it
431 returns a negative integer to the caller on failure.
439 Take a pointer to the
<tt>struct lock_file
</tt> initialized
440 with an earlier call to
<tt>hold_lock_file_for_update()
</tt>,
441 close the file descriptor and rename the lockfile to its
442 final destination. Returns
0 upon success, a negative
443 value on failure to close(
2) or rename(
2).
451 Take a pointer to the
<tt>struct lock_file
</tt> initialized
452 with an earlier call to
<tt>hold_lock_file_for_update()
</tt>,
453 close the file descriptor and remove the lockfile.
461 Take a pointer to the
<tt>struct lock_file
</tt> initialized
462 with an earlier call to
<tt>hold_lock_file_for_update()
</tt>,
463 and close the file descriptor. Returns
0 upon success,
464 a negative value on failure to close(
2).
468 <div class=
"paragraph"><p>Because the structure is used in an
<tt>atexit(
3)
</tt> handler, its
469 storage has to stay throughout the life of the program. It
470 cannot be an auto variable allocated on the stack.
</p></div>
471 <div class=
"paragraph"><p>Call
<tt>commit_lock_file()
</tt> or
<tt>rollback_lock_file()
</tt> when you are
472 done writing to the file descriptor. If you do not call either
473 and simply
<tt>exit(
3)
</tt> from the program, an
<tt>atexit(
3)
</tt> handler
474 will close and remove the lockfile.
</p></div>
475 <div class=
"paragraph"><p>If you need to close the file descriptor you obtained from
476 <tt>hold_lock_file_for_update
</tt> function yourself, do so by calling
477 <tt>close_lock_file()
</tt>. You should never call
<tt>close(
2)
</tt> yourself!
478 Otherwise the
<tt>struct
479 lock_file
</tt> structure still remembers that the file descriptor
480 needs to be closed, and a later call to
<tt>commit_lock_file()
</tt> or
481 <tt>rollback_lock_file()
</tt> will result in duplicate calls to
482 <tt>close(
2)
</tt>. Worse yet, if you
<tt>close(
2)
</tt>, open another file
483 descriptor for completely different purpose, and then call
484 <tt>commit_lock_file()
</tt> or
<tt>rollback_lock_file()
</tt>, they may close
485 that unrelated file descriptor.
</p></div>
488 <div id=
"footer-text">
489 Last updated
2008-
09-
19 06:
28:
28 UTC