annotate lib/utimens.c @ 14152:c5273d4d49b3

Update to Unicode 5.2.0. * lib/gen-uni-tables.c (output_predicate, output_category, output_combclass, output_bidi_category, output_decimal_digit_test, output_decimal_digit, output_digit_test, output_digit, output_numeric_test, output_numeric, output_mirror, output_scripts, output_scripts_byname, output_blocks, output_ident_category): Fix comment header. (is_WBP_MIDNUMLET, is_WBP_MIDLETTER): New functions, extracted from get_wbp. (PROP_CASED, PROP_CASE_IGNORABLE, PROP_CHANGES_WHEN_*): New enumeration items. (fill_properties): Also fill the peoperties Cased, Case_Ignorable, Changes_When_Lowercased, Changes_When_Uppercased, Changes_When_Titlecased, Changes_When_Casefolded, Changes_When_Casemapped. (is_property_alphabetic, is_property_default_ignorable_code_point): Update for Unicode 5.2.0. (is_property_cased, is_property_case_ignorable, is_property_changes_when_lowercased, is_property_changes_when_uppercased, is_property_changes_when_titlecased, is_property_changes_when_casefolded, is_property_changes_when_casemapped): New functions. (output_properties): Output also the properties cased, case_ignorable, changes_when_lowercased, changes_when_uppercased, changes_when_titlecased, changes_when_casefolded, changes_when_casemapped. (symbolic_width): Update for Unicode 5.2.0, incorporating changes from Unicode TR#11 revision 17 -> 19. (LBP_CP): New enumeration value. (LBP_*): Adjust values accordingly. (get_lbp): Update for Unicode 5.2.0, incorporating changes from Unicode TR#14 revision 22 -> 24. (debug_output_lbp): Allow for LBP_* bits >= 32. Support LBP_CP. (fill_org_lbp, debug_output_org_lbp, output_lbp): Support LBP_CP. (get_wbp): Update for Unicode 5.2.0, incorporating changes from Unicode TR#29 revision 13 -> 15. Use functions is_WBP_MIDNUMLET, is_WBP_MIDLETTER. (output_composition_tables): Allow for 24 bits instead of 16 bits in the code1 and code2 of each composition rule. * lib/unicase/cased.h: Regenerated for Unicode 5.2.0. * lib/unicase/ignorable.h: Likewise. * lib/unicase/tocasefold.h: Likewise. * lib/unicase/tolower.h: Likewise. * lib/unicase/totitle.h: Likewise. * lib/unicase/toupper.h: Likewise. * lib/unictype/bidi_of.h: Likewise. * lib/unictype/blocks.h: Likewise. * lib/unictype/categ_C.h: Likewise. * lib/unictype/categ_Cf.h: Likewise. * lib/unictype/categ_Cn.h: Likewise. * lib/unictype/categ_L.h: Likewise. * lib/unictype/categ_Ll.h: Likewise. * lib/unictype/categ_Lm.h: Likewise. * lib/unictype/categ_Lo.h: Likewise. * lib/unictype/categ_Lu.h: Likewise. * lib/unictype/categ_M.h: Likewise. * lib/unictype/categ_Mc.h: Likewise. * lib/unictype/categ_Mn.h: Likewise. * lib/unictype/categ_N.h: Likewise. * lib/unictype/categ_Nd.h: Likewise. * lib/unictype/categ_Nl.h: Likewise. * lib/unictype/categ_No.h: Likewise. * lib/unictype/categ_P.h: Likewise. * lib/unictype/categ_Pd.h: Likewise. * lib/unictype/categ_Po.h: Likewise. * lib/unictype/categ_S.h: Likewise. * lib/unictype/categ_Sc.h: Likewise. * lib/unictype/categ_So.h: Likewise. * lib/unictype/categ_of.h: Likewise. * lib/unictype/combining.h: Likewise. * lib/unictype/ctype_alnum.h: Likewise. * lib/unictype/ctype_alpha.h: Likewise. * lib/unictype/ctype_graph.h: Likewise. * lib/unictype/ctype_lower.h: Likewise. * lib/unictype/ctype_print.h: Likewise. * lib/unictype/ctype_punct.h: Likewise. * lib/unictype/ctype_upper.h: Likewise. * lib/unictype/decdigit.h: Likewise. * lib/unictype/digit.h: Likewise. * lib/unictype/numeric.h: Likewise. * lib/unictype/pr_alphabetic.h: Likewise. * lib/unictype/pr_bidi_arabic_digit.h: Likewise. * lib/unictype/pr_bidi_eur_num_terminator.h: Likewise. * lib/unictype/pr_bidi_european_digit.h: Likewise. * lib/unictype/pr_bidi_hebrew_right_to_left.h: Likewise. * lib/unictype/pr_bidi_left_to_right.h: Likewise. * lib/unictype/pr_bidi_non_spacing_mark.h: Likewise. * lib/unictype/pr_bidi_other_neutral.h: Likewise. * lib/unictype/pr_combining.h: Likewise. * lib/unictype/pr_composite.h: Likewise. * lib/unictype/pr_currency_symbol.h: Likewise. * lib/unictype/pr_dash.h: Likewise. * lib/unictype/pr_decimal_digit.h: Likewise. * lib/unictype/pr_deprecated.h: Likewise. * lib/unictype/pr_diacritic.h: Likewise. * lib/unictype/pr_extender.h: Likewise. * lib/unictype/pr_grapheme_base.h: Likewise. * lib/unictype/pr_grapheme_extend.h: Likewise. * lib/unictype/pr_grapheme_link.h: Likewise. * lib/unictype/pr_id_continue.h: Likewise. * lib/unictype/pr_id_start.h: Likewise. * lib/unictype/pr_ideographic.h: Likewise. * lib/unictype/pr_ignorable_control.h: Likewise. * lib/unictype/pr_logical_order_exception.h: Likewise. * lib/unictype/pr_lowercase.h: Likewise. * lib/unictype/pr_numeric.h: Likewise. * lib/unictype/pr_other_alphabetic.h: Likewise. * lib/unictype/pr_punctuation.h: Likewise. * lib/unictype/pr_sentence_terminal.h: Likewise. * lib/unictype/pr_terminal_punctuation.h: Likewise. * lib/unictype/pr_unassigned_code_value.h: Likewise. * lib/unictype/pr_unified_ideograph.h: Likewise. * lib/unictype/pr_uppercase.h: Likewise. * lib/unictype/pr_xid_continue.h: Likewise. * lib/unictype/pr_xid_start.h: Likewise. * lib/unictype/pr_zero_width.h: Likewise. * lib/unictype/scripts.h: Likewise. * lib/unictype/scripts_byname.gperf: Likewise. * lib/unictype/sy_java_ident.h: Likewise. * lib/unigbrk/gbrkprop.h: Likewise. * lib/unilbrk/lbrkprop1.h: Likewise. * lib/unilbrk/lbrkprop2.h: Likewise. * lib/unilbrk/lbrktables.h: Likewise. * lib/unilbrk/lbrktables.c (unilbrk_table): Add a row and column for LBP_CP. Implement rule LB30. * lib/uniwidth/width.c (nonspacing_table_data): Add U+0816..U+0819, U+081B..U+0823, U+0825..U+0827, U+0829..U+082D, U+0900, U+0955, U+109D, U+1A56, U+1A58..U+1A5E, U+1A60, U+1A62, U+1A65..U+1A6C, U+1A73..U+1A7C, U+1A7F, U+1CD0..U+1CD2, U+1CD4..U+1CE0, U+1CE2..U+1CE8, U+1CED, U+1DFD, U+2CEF..U+2CF1, U+A6F0..U+A6F1, U+A8E0..U+A8F1, U+A980..U+A982, U+A9B3, U+A9B6..U+A9B9, U+A9BC, U+AAB0, U+AAB2..U+AAB4, U+AAB7..U+AAB8, U+AABE..U+AABF, U+AAC1, U+ABE5, U+ABE8, U+ABED, U+11080..U+11081, U+110B3..U+110B6, U+110B9..U+110BA, U+110BD. (uc_width): Return 2 also for unassigned code points of planes 2 and 3. * lib/uninorm/composition-table.gperf: Regenerated for Unicode 5.2.0. * lib/uninorm/composition.c (struct composition_rule): Allow for 24 bits instead of 16 bits in the code1 and code2 of each composition rule. (uc_composition): Update for Unicode 5.2.0. * lib/uninorm/decomposition-table1.h: Regenerated for Unicode 5.2.0. * lib/uninorm/decomposition-table2.h: Likewise. * lib/uniwbrk/wbrkprop.h: Likewise. * tests/unicase/test-cased.c: Likewise. * tests/unicase/test-ignorable.c: Likewise. * tests/unicase/test-uc_tolower.c: Likewise. * tests/unicase/test-uc_totitle.c: Likewise. * tests/unicase/test-uc_toupper.c: Likewise. * tests/unictype/test-categ_C.c: Likewise. * tests/unictype/test-categ_Cf.c: Likewise. * tests/unictype/test-categ_Cn.c: Likewise. * tests/unictype/test-categ_L.c: Likewise. * tests/unictype/test-categ_Ll.c: Likewise. * tests/unictype/test-categ_Lm.c: Likewise. * tests/unictype/test-categ_Lo.c: Likewise. * tests/unictype/test-categ_Lu.c: Likewise. * tests/unictype/test-categ_M.c: Likewise. * tests/unictype/test-categ_Mc.c: Likewise. * tests/unictype/test-categ_Mn.c: Likewise. * tests/unictype/test-categ_N.c: Likewise. * tests/unictype/test-categ_Nd.c: Likewise. * tests/unictype/test-categ_Nl.c: Likewise. * tests/unictype/test-categ_No.c: Likewise. * tests/unictype/test-categ_P.c: Likewise. * tests/unictype/test-categ_Pd.c: Likewise. * tests/unictype/test-categ_Po.c: Likewise. * tests/unictype/test-categ_S.c: Likewise. * tests/unictype/test-categ_Sc.c: Likewise. * tests/unictype/test-categ_So.c: Likewise. * tests/unictype/test-ctype_alnum.c: Likewise. * tests/unictype/test-ctype_alpha.c: Likewise. * tests/unictype/test-ctype_graph.c: Likewise. * tests/unictype/test-ctype_lower.c: Likewise. * tests/unictype/test-ctype_print.c: Likewise. * tests/unictype/test-ctype_punct.c: Likewise. * tests/unictype/test-ctype_upper.c: Likewise. * tests/unictype/test-decdigit.h: Likewise. * tests/unictype/test-digit.h: Likewise. * tests/unictype/test-numeric.h: Likewise. * tests/unictype/test-pr_alphabetic.c: Likewise. * tests/unictype/test-pr_bidi_arabic_digit.c: Likewise. * tests/unictype/test-pr_bidi_eur_num_terminator.c: Likewise. * tests/unictype/test-pr_bidi_european_digit.c: Likewise. * tests/unictype/test-pr_bidi_hebrew_right_to_left.c: Likewise. * tests/unictype/test-pr_bidi_left_to_right.c: Likewise. * tests/unictype/test-pr_bidi_non_spacing_mark.c: Likewise. * tests/unictype/test-pr_bidi_other_neutral.c: Likewise. * tests/unictype/test-pr_combining.c: Likewise. * tests/unictype/test-pr_composite.c: Likewise. * tests/unictype/test-pr_currency_symbol.c: Likewise. * tests/unictype/test-pr_dash.c: Likewise. * tests/unictype/test-pr_decimal_digit.c: Likewise. * tests/unictype/test-pr_deprecated.c: Likewise. * tests/unictype/test-pr_diacritic.c: Likewise. * tests/unictype/test-pr_extender.c: Likewise. * tests/unictype/test-pr_grapheme_base.c: Likewise. * tests/unictype/test-pr_grapheme_extend.c: Likewise. * tests/unictype/test-pr_grapheme_link.c: Likewise. * tests/unictype/test-pr_id_continue.c: Likewise. * tests/unictype/test-pr_id_start.c: Likewise. * tests/unictype/test-pr_ideographic.c: Likewise. * tests/unictype/test-pr_ignorable_control.c: Likewise. * tests/unictype/test-pr_logical_order_exception.c: Likewise. * tests/unictype/test-pr_lowercase.c: Likewise. * tests/unictype/test-pr_numeric.c: Likewise. * tests/unictype/test-pr_other_alphabetic.c: Likewise. * tests/unictype/test-pr_punctuation.c: Likewise. * tests/unictype/test-pr_sentence_terminal.c: Likewise. * tests/unictype/test-pr_terminal_punctuation.c: Likewise. * tests/unictype/test-pr_unassigned_code_value.c: Likewise. * tests/unictype/test-pr_unified_ideograph.c: Likewise. * tests/unictype/test-pr_uppercase.c: Likewise. * tests/unictype/test-pr_xid_continue.c: Likewise. * tests/unictype/test-pr_xid_start.c: Likewise. * tests/unictype/test-pr_zero_width.c: Likewise. * tests/unigbrk/test-uc-gbrk-prop.h: Likewise. * tests/unilbrk/test-u8-possible-linebreaks.c (main): Update for changed behaviour: line breaking is now disallowed between a letter or '=' and '('. * tests/unilbrk/test-u16-possible-linebreaks.c (main): Likewise. * tests/unilbrk/test-u32-possible-linebreaks.c (main): Likewise. * tests/unilbrk/test-ulc-possible-linebreaks.c (main): Likewise. * tests/unilbrk/test-ulc-width-linebreaks.c (main): Likewise. * tests/uniwidth/test-uc_width2.sh: Same updates as in lib/uniwidth/width.c. * tests/uninorm/NormalizationTest.txt: Update from Unicode 5.2.0, without comments, but with the original copyright notice. * lib/unicase/special-casing-table.gperf: Regenerated; only comment changes. * lib/unictype/categ_Cc.h: Likewise. * lib/unictype/categ_Co.h: Likewise. * lib/unictype/categ_Cs.h: Likewise. * lib/unictype/categ_Lt.h: Likewise. * lib/unictype/categ_Me.h: Likewise. * lib/unictype/categ_Pc.h: Likewise. * lib/unictype/categ_Pe.h: Likewise. * lib/unictype/categ_Pf.h: Likewise. * lib/unictype/categ_Pi.h: Likewise. * lib/unictype/categ_Ps.h: Likewise. * lib/unictype/categ_Sk.h: Likewise. * lib/unictype/categ_Sm.h: Likewise. * lib/unictype/categ_Z.h: Likewise. * lib/unictype/categ_Zl.h: Likewise. * lib/unictype/categ_Zp.h: Likewise. * lib/unictype/categ_Zs.h: Likewise. * lib/unictype/ctype_blank.h: Likewise. * lib/unictype/ctype_cntrl.h: Likewise. * lib/unictype/ctype_digit.h: Likewise. * lib/unictype/ctype_space.h: Likewise. * lib/unictype/ctype_xdigit.h: Likewise. * lib/unictype/mirror.h: Likewise. * lib/unictype/pr_ascii_hex_digit.h: Likewise. * lib/unictype/pr_bidi_arabic_right_to_left.h: Likewise. * lib/unictype/pr_bidi_block_separator.h: Likewise. * lib/unictype/pr_bidi_boundary_neutral.h: Likewise. * lib/unictype/pr_bidi_common_separator.h: Likewise. * lib/unictype/pr_bidi_control.h: Likewise. * lib/unictype/pr_bidi_embedding_or_override.h: Likewise. * lib/unictype/pr_bidi_eur_num_separator.h: Likewise. * lib/unictype/pr_bidi_pdf.h: Likewise. * lib/unictype/pr_bidi_segment_separator.h: Likewise. * lib/unictype/pr_bidi_whitespace.h: Likewise. * lib/unictype/pr_default_ignorable_code_point.h: Likewise. * lib/unictype/pr_format_control.h: Likewise. * lib/unictype/pr_hex_digit.h: Likewise. * lib/unictype/pr_hyphen.h: Likewise. * lib/unictype/pr_ids_binary_operator.h: Likewise. * lib/unictype/pr_ids_trinary_operator.h: Likewise. * lib/unictype/pr_iso_control.h: Likewise. * lib/unictype/pr_join_control.h: Likewise. * lib/unictype/pr_left_of_pair.h: Likewise. * lib/unictype/pr_line_separator.h: Likewise. * lib/unictype/pr_math.h: Likewise. * lib/unictype/pr_non_break.h: Likewise. * lib/unictype/pr_not_a_character.h: Likewise. * lib/unictype/pr_other_default_ignorable_code_point.h: Likewise. * lib/unictype/pr_other_grapheme_extend.h: Likewise. * lib/unictype/pr_other_id_continue.h: Likewise. * lib/unictype/pr_other_id_start.h: Likewise. * lib/unictype/pr_other_lowercase.h: Likewise. * lib/unictype/pr_other_math.h: Likewise. * lib/unictype/pr_other_uppercase.h: Likewise. * lib/unictype/pr_paired_punctuation.h: Likewise. * lib/unictype/pr_paragraph_separator.h: Likewise. * lib/unictype/pr_pattern_syntax.h: Likewise. * lib/unictype/pr_pattern_white_space.h: Likewise. * lib/unictype/pr_private_use.h: Likewise. * lib/unictype/pr_quotation_mark.h: Likewise. * lib/unictype/pr_radical.h: Likewise. * lib/unictype/pr_soft_dotted.h: Likewise. * lib/unictype/pr_space.h: Likewise. * lib/unictype/pr_titlecase.h: Likewise. * lib/unictype/pr_variation_selector.h: Likewise. * lib/unictype/pr_white_space.h: Likewise. * lib/unictype/sy_c_ident.h: Likewise. * lib/unictype/sy_c_whitespace.h: Likewise. * lib/unictype/sy_java_whitespace.h: Likewise. * modules/uni*/*: Bump version number of expected libunistring version. Reported by Simon Josefsson.
author Bruno Haible <bruno@clisp.org>
date Sun, 09 Jan 2011 11:09:25 +0100
parents 97fc9a21a8fb
children befccd4a7d52
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6734
3e5ad4566013 * utimens.c (futimens): glibc futimesat messes up if /proc
Paul Eggert <eggert@cs.ucla.edu>
parents: 6440
diff changeset
1 /* Set file access and modification times.
3e5ad4566013 * utimens.c (futimens): glibc futimesat messes up if /proc
Paul Eggert <eggert@cs.ucla.edu>
parents: 6440
diff changeset
2
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 14047
diff changeset
3 Copyright (C) 2003-2011 Free Software Foundation, Inc.
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8842
diff changeset
5 This program is free software: you can redistribute it and/or modify it
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
6 under the terms of the GNU General Public License as published by the
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8842
diff changeset
7 Free Software Foundation; either version 3 of the License, or any
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
8 later version.
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13 GNU General Public License for more details.
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8842
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
17
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18 /* Written by Paul Eggert. */
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
19
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
20 /* derived from a function in touch.c */
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 6819
diff changeset
22 #include <config.h>
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
23
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
24 #include "utimens.h"
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
25
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
26 #include <assert.h>
5589
6ddb405e16b6 (futimens): Account for the fact that futimes
Paul Eggert <eggert@cs.ucla.edu>
parents: 5485
diff changeset
27 #include <errno.h>
6283
b377d8796118 * lib/utimens.c (futimens): Use futimesat if available.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
28 #include <fcntl.h>
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
29 #include <stdbool.h>
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
30 #include <sys/stat.h>
7897
b7a83a69ac23 * MODULES.html.sh (Support for systems lacking POSIX:2001): New
Paul Eggert <eggert@cs.ucla.edu>
parents: 7713
diff changeset
31 #include <sys/time.h>
6322
716071856296 Sync from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6297
diff changeset
32 #include <unistd.h>
5589
6ddb405e16b6 (futimens): Account for the fact that futimes
Paul Eggert <eggert@cs.ucla.edu>
parents: 5485
diff changeset
33
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
34 #include "stat-time.h"
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
35 #include "timespec.h"
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
36
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
37 #if HAVE_UTIME_H
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
38 # include <utime.h>
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
39 #endif
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
40
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
41 /* Some systems (even some that do have <utime.h>) don't declare this
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
42 structure anywhere. */
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
43 #ifndef HAVE_STRUCT_UTIMBUF
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
44 struct utimbuf
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
45 {
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
46 long actime;
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
47 long modtime;
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
48 };
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
49 #endif
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
50
12174
73f2681e0524 utimensat: new module
Eric Blake <ebb9@byu.net>
parents: 12173
diff changeset
51 /* Avoid recursion with rpl_futimens or rpl_utimensat. */
12162
fe82ecf4f214 futimens: new module
Eric Blake <ebb9@byu.net>
parents: 12161
diff changeset
52 #undef futimens
12174
73f2681e0524 utimensat: new module
Eric Blake <ebb9@byu.net>
parents: 12173
diff changeset
53 #undef utimensat
12162
fe82ecf4f214 futimens: new module
Eric Blake <ebb9@byu.net>
parents: 12161
diff changeset
54
12194
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
55 /* Solaris 9 mistakenly succeeds when given a non-directory with a
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
56 trailing slash. Force the use of rpl_stat for a fix. */
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
57 #ifndef REPLACE_FUNC_STAT_FILE
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
58 # define REPLACE_FUNC_STAT_FILE 0
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
59 #endif
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
60
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
61 #if HAVE_UTIMENSAT || HAVE_FUTIMENS
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
62 /* Cache variables for whether the utimensat syscall works; used to
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
63 avoid calling the syscall if we know it will just fail with ENOSYS,
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
64 and to avoid unnecessary work in massaging timestamps if the
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
65 syscall will work. Multiple variables are needed, to distinguish
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
66 between the following scenarios on Linux:
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
67 utimensat doesn't exist, or is in glibc but kernel 2.6.18 fails with ENOSYS
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
68 kernel 2.6.22 and earlier rejects AT_SYMLINK_NOFOLLOW
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
69 kernel 2.6.25 and earlier reject UTIME_NOW/UTIME_OMIT with non-zero tv_sec
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
70 kernel 2.6.32 used with xfs or ntfs-3g fail to honor UTIME_OMIT
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
71 utimensat completely works
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
72 For each cache variable: 0 = unknown, 1 = yes, -1 = no. */
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
73 static int utimensat_works_really;
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
74 static int lutimensat_works_really;
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
75 #endif /* HAVE_UTIMENSAT || HAVE_FUTIMENS */
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
76
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
77 /* Validate the requested timestamps. Return 0 if the resulting
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
78 timespec can be used for utimensat (after possibly modifying it to
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
79 work around bugs in utimensat). Return a positive value if the
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
80 timespec needs further adjustment based on stat results: 1 if any
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
81 adjustment is needed for utimes, and 2 if any adjustment is needed
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
82 for Linux utimensat. Return -1, with errno set to EINVAL, if
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
83 timespec is out of range. */
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
84 static int
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
85 validate_timespec (struct timespec timespec[2])
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
86 {
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
87 int result = 0;
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
88 int utime_omit_count = 0;
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
89 assert (timespec);
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
90 if ((timespec[0].tv_nsec != UTIME_NOW
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
91 && timespec[0].tv_nsec != UTIME_OMIT
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
92 && (timespec[0].tv_nsec < 0 || 1000000000 <= timespec[0].tv_nsec))
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
93 || (timespec[1].tv_nsec != UTIME_NOW
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
94 && timespec[1].tv_nsec != UTIME_OMIT
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
95 && (timespec[1].tv_nsec < 0 || 1000000000 <= timespec[1].tv_nsec)))
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
96 {
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
97 errno = EINVAL;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
98 return -1;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
99 }
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
100 /* Work around Linux kernel 2.6.25 bug, where utimensat fails with
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
101 EINVAL if tv_sec is not 0 when using the flag values of tv_nsec.
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
102 Flag a Linux kernel 2.6.32 bug, where an mtime of UTIME_OMIT
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
103 fails to bump ctime. */
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
104 if (timespec[0].tv_nsec == UTIME_NOW
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
105 || timespec[0].tv_nsec == UTIME_OMIT)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
106 {
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
107 timespec[0].tv_sec = 0;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
108 result = 1;
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
109 if (timespec[0].tv_nsec == UTIME_OMIT)
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
110 utime_omit_count++;
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
111 }
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
112 if (timespec[1].tv_nsec == UTIME_NOW
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
113 || timespec[1].tv_nsec == UTIME_OMIT)
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
114 {
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
115 timespec[1].tv_sec = 0;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
116 result = 1;
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
117 if (timespec[1].tv_nsec == UTIME_OMIT)
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
118 utime_omit_count++;
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
119 }
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
120 return result + (utime_omit_count == 1);
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
121 }
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
122
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
123 /* Normalize any UTIME_NOW or UTIME_OMIT values in *TS, using stat
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
124 buffer STATBUF to obtain the current timestamps of the file. If
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
125 both times are UTIME_NOW, set *TS to NULL (as this can avoid some
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
126 permissions issues). If both times are UTIME_OMIT, return true
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
127 (nothing further beyond the prior collection of STATBUF is
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
128 necessary); otherwise return false. */
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
129 static bool
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
130 update_timespec (struct stat const *statbuf, struct timespec *ts[2])
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
131 {
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
132 struct timespec *timespec = *ts;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
133 if (timespec[0].tv_nsec == UTIME_OMIT
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
134 && timespec[1].tv_nsec == UTIME_OMIT)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
135 return true;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
136 if (timespec[0].tv_nsec == UTIME_NOW
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
137 && timespec[1].tv_nsec == UTIME_NOW)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
138 {
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
139 *ts = NULL;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
140 return false;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
141 }
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
142
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
143 if (timespec[0].tv_nsec == UTIME_OMIT)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
144 timespec[0] = get_stat_atime (statbuf);
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
145 else if (timespec[0].tv_nsec == UTIME_NOW)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
146 gettime (&timespec[0]);
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
147
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
148 if (timespec[1].tv_nsec == UTIME_OMIT)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
149 timespec[1] = get_stat_mtime (statbuf);
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
150 else if (timespec[1].tv_nsec == UTIME_NOW)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
151 gettime (&timespec[1]);
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
152
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
153 return false;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
154 }
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
155
5485
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
156 /* Set the access and modification time stamps of FD (a.k.a. FILE) to be
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
157 TIMESPEC[0] and TIMESPEC[1], respectively.
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
158 FD must be either negative -- in which case it is ignored --
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
159 or a file descriptor that is open on FILE.
6297
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
160 If FD is nonnegative, then FILE can be NULL, which means
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
161 use just futimes (or equivalent) instead of utimes (or equivalent),
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
162 and fail if on an old system without futimes (or equivalent).
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
163 If TIMESPEC is null, set the time stamps to the current time.
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
164 Return 0 on success, -1 (setting errno) on failure. */
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
165
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
166 int
13696
bfe487360d66 fdutimens, fdutimensat: update signature, again
Eric Blake <eblake@redhat.com>
parents: 13284
diff changeset
167 fdutimens (int fd, char const *file, struct timespec const timespec[2])
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
168 {
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
169 struct timespec adjusted_timespec[2];
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
170 struct timespec *ts = timespec ? adjusted_timespec : NULL;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
171 int adjustment_needed = 0;
12554
0774f328f2ff utimens: avoid shadowing warning
Eric Blake <ebb9@byu.net>
parents: 12525
diff changeset
172 struct stat st;
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
173
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
174 if (ts)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
175 {
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
176 adjusted_timespec[0] = timespec[0];
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
177 adjusted_timespec[1] = timespec[1];
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
178 adjustment_needed = validate_timespec (ts);
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
179 }
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
180 if (adjustment_needed < 0)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
181 return -1;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
182
12157
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
183 /* Require that at least one of FD or FILE are valid. Works around
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
184 a Linux bug where futimens (AT_FDCWD, NULL) changes "." rather
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
185 than failing. */
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
186 if (!file)
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
187 {
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
188 if (fd < 0)
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
189 {
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
190 errno = EBADF;
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
191 return -1;
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
192 }
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
193 if (dup2 (fd, fd) != fd)
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
194 return -1;
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
195 }
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
196
7712
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
197 /* Some Linux-based NFS clients are buggy, and mishandle time stamps
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
198 of files in NFS file systems in some cases. We have no
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
199 configure-time test for this, but please see
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
200 <http://bugs.gentoo.org/show_bug.cgi?id=132673> for references to
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
201 some of the problems with Linux 2.6.16. If this affects you,
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
202 compile with -DHAVE_BUGGY_NFS_TIME_STAMPS; this is reported to
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
203 help in some cases, albeit at a cost in performance. But you
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
204 really should upgrade your kernel to a fixed version, since the
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
205 problem affects many applications. */
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
206
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
207 #if HAVE_BUGGY_NFS_TIME_STAMPS
7713
064005e1c3c3 Prefer fd < 0 to ! (0 <= fd).
Paul Eggert <eggert@cs.ucla.edu>
parents: 7712
diff changeset
208 if (fd < 0)
064005e1c3c3 Prefer fd < 0 to ! (0 <= fd).
Paul Eggert <eggert@cs.ucla.edu>
parents: 7712
diff changeset
209 sync ();
064005e1c3c3 Prefer fd < 0 to ! (0 <= fd).
Paul Eggert <eggert@cs.ucla.edu>
parents: 7712
diff changeset
210 else
7712
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
211 fsync (fd);
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
212 #endif
aead3397193a * lib/utimens.c (futimens) [HAVE_BUGGY_NFS_TIME_STAMPS]: Add a
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
213
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
214 /* POSIX 2008 added two interfaces to set file timestamps with
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
215 nanosecond resolution; newer Linux implements both functions via
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
216 a single syscall. We provide a fallback for ENOSYS (for example,
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
217 compiling against Linux 2.6.25 kernel headers and glibc 2.7, but
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
218 running on Linux 2.6.18 kernel). */
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
219 #if HAVE_UTIMENSAT || HAVE_FUTIMENS
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
220 if (0 <= utimensat_works_really)
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
221 {
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
222 int result;
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
223 # if __linux__
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
224 /* As recently as Linux kernel 2.6.32 (Dec 2009), several file
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
225 systems (xfs, ntfs-3g) have bugs with a single UTIME_OMIT,
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
226 but work if both times are either explicitly specified or
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
227 UTIME_NOW. Work around it with a preparatory [f]stat prior
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
228 to calling futimens/utimensat; fortunately, there is not much
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
229 timing impact due to the extra syscall even on file systems
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
230 where UTIME_OMIT would have worked. FIXME: Simplify this in
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
231 2012, when file system bugs are no longer common. */
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
232 if (adjustment_needed == 2)
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
233 {
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
234 if (fd < 0 ? stat (file, &st) : fstat (fd, &st))
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
235 return -1;
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
236 if (ts[0].tv_nsec == UTIME_OMIT)
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
237 ts[0] = get_stat_atime (&st);
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
238 else if (ts[1].tv_nsec == UTIME_OMIT)
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
239 ts[1] = get_stat_mtime (&st);
12554
0774f328f2ff utimens: avoid shadowing warning
Eric Blake <ebb9@byu.net>
parents: 12525
diff changeset
240 /* Note that st is good, in case utimensat gives ENOSYS. */
0774f328f2ff utimens: avoid shadowing warning
Eric Blake <ebb9@byu.net>
parents: 12525
diff changeset
241 adjustment_needed++;
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
242 }
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
243 # endif /* __linux__ */
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
244 # if HAVE_UTIMENSAT
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
245 if (fd < 0)
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
246 {
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
247 result = utimensat (AT_FDCWD, file, ts, 0);
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
248 # ifdef __linux__
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
249 /* Work around a kernel bug:
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
250 http://bugzilla.redhat.com/442352
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
251 http://bugzilla.redhat.com/449910
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
252 It appears that utimensat can mistakenly return 280 rather
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
253 than -1 upon ENOSYS failure.
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
254 FIXME: remove in 2010 or whenever the offending kernels
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
255 are no longer in common use. */
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
256 if (0 < result)
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
257 errno = ENOSYS;
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
258 # endif /* __linux__ */
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
259 if (result == 0 || errno != ENOSYS)
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
260 {
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
261 utimensat_works_really = 1;
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
262 return result;
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
263 }
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
264 }
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
265 # endif /* HAVE_UTIMENSAT */
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
266 # if HAVE_FUTIMENS
12580
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
267 if (0 <= fd)
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
268 {
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
269 result = futimens (fd, ts);
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
270 # ifdef __linux__
12580
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
271 /* Work around the same bug as above. */
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
272 if (0 < result)
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
273 errno = ENOSYS;
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
274 # endif /* __linux__ */
12580
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
275 if (result == 0 || errno != ENOSYS)
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
276 {
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
277 utimensat_works_really = 1;
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
278 return result;
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
279 }
3a190157862b utimens (fdutimens): ignore a negative FD, per contract
Aurelien Jarno <aurelien@aurel32.net>
parents: 12559
diff changeset
280 }
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
281 # endif /* HAVE_FUTIMENS */
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
282 }
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
283 utimensat_works_really = -1;
12353
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
284 lutimensat_works_really = -1;
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
285 #endif /* HAVE_UTIMENSAT || HAVE_FUTIMENS */
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
286
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
287 /* The platform lacks an interface to set file timestamps with
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
288 nanosecond resolution, so do the best we can, discarding any
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
289 fractional part of the timestamp. */
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
290
12194
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
291 if (adjustment_needed || (REPLACE_FUNC_STAT_FILE && fd < 0))
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
292 {
12554
0774f328f2ff utimens: avoid shadowing warning
Eric Blake <ebb9@byu.net>
parents: 12525
diff changeset
293 if (adjustment_needed != 3
0774f328f2ff utimens: avoid shadowing warning
Eric Blake <ebb9@byu.net>
parents: 12525
diff changeset
294 && (fd < 0 ? stat (file, &st) : fstat (fd, &st)))
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
295 return -1;
12194
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
296 if (ts && update_timespec (&st, &ts))
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
297 return 0;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
298 }
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
299
6297
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
300 {
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
301 #if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
302 struct timeval timeval[2];
13284
e08c8b78a796 utimens: avoid compiler warning
Andreas Gruenbacher <agruen@suse.de>
parents: 12580
diff changeset
303 struct timeval *t;
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
304 if (ts)
6297
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
305 {
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
306 timeval[0].tv_sec = ts[0].tv_sec;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
307 timeval[0].tv_usec = ts[0].tv_nsec / 1000;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
308 timeval[1].tv_sec = ts[1].tv_sec;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
309 timeval[1].tv_usec = ts[1].tv_nsec / 1000;
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
310 t = timeval;
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
311 }
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
312 else
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
313 t = NULL;
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
314
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
315 if (fd < 0)
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
316 {
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
317 # if HAVE_FUTIMESAT
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
318 return futimesat (AT_FDCWD, file, t);
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
319 # endif
6297
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
320 }
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
321 else
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
322 {
12317
407921985bc0 utimens: fix regression on Solaris
Eric Blake <ebb9@byu.net>
parents: 12256
diff changeset
323 /* If futimesat or futimes fails here, don't try to speed things
407921985bc0 utimens: fix regression on Solaris
Eric Blake <ebb9@byu.net>
parents: 12256
diff changeset
324 up by returning right away. glibc can incorrectly fail with
407921985bc0 utimens: fix regression on Solaris
Eric Blake <ebb9@byu.net>
parents: 12256
diff changeset
325 errno == ENOENT if /proc isn't mounted. Also, Mandrake 10.0
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
326 in high security mode doesn't allow ordinary users to read
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
327 /proc/self, so glibc incorrectly fails with errno == EACCES.
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
328 If errno == EIO, EPERM, or EROFS, it's probably safe to fail
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
329 right away, but these cases are rare enough that they're not
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
330 worth optimizing, and who knows what other messed-up systems
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
331 are out there? So play it safe and fall back on the code
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
332 below. */
14039
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
333
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
334 # if (HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG) || HAVE_FUTIMES
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
335 # if HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
336 # undef futimes
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
337 # define futimes(fd, t) futimesat (fd, NULL, t)
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
338 # endif
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
339 if (futimes (fd, t) == 0)
14039
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
340 {
14047
126b2fb2abab utimens: work around glibc rounding bug on more platforms
Paul Eggert <eggert@cs.ucla.edu>
parents: 14039
diff changeset
341 # if __linux__ && __GLIBC__
14039
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
342 /* Work around a longstanding glibc bug, still present as
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
343 of 2010-12-27. On older Linux kernels that lack both
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
344 utimensat and utimes, glibc's futimes rounds instead of
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
345 truncating when falling back on utime. The same bug
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
346 occurs in futimesat with a null 2nd arg. */
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
347 if (t)
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
348 {
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
349 bool abig = 500000 <= t[0].tv_usec;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
350 bool mbig = 500000 <= t[1].tv_usec;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
351 if ((abig | mbig) && fstat (fd, &st) == 0)
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
352 {
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
353 /* If these two subtractions overflow, they'll
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
354 track the overflows inside the buggy glibc. */
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
355 time_t adiff = st.st_atime - t[0].tv_sec;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
356 time_t mdiff = st.st_mtime - t[1].tv_sec;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
357
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
358 struct timeval *tt = NULL;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
359 struct timeval truncated_timeval[2];
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
360 truncated_timeval[0] = t[0];
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
361 truncated_timeval[1] = t[1];
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
362 if (abig && adiff == 1 && get_stat_atime_ns (&st) == 0)
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
363 {
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
364 tt = truncated_timeval;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
365 tt[0].tv_usec = 0;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
366 }
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
367 if (mbig && mdiff == 1 && get_stat_mtime_ns (&st) == 0)
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
368 {
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
369 tt = truncated_timeval;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
370 tt[1].tv_usec = 0;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
371 }
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
372 if (tt)
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
373 futimes (fd, tt);
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
374 }
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
375 }
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
376 # endif
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
377
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
378 return 0;
aeebba7a29c7 utimens: work around glibc rounding bug on older Linux kernels
Paul Eggert <eggert@cs.ucla.edu>
parents: 13696
diff changeset
379 }
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
380 # endif
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
381 }
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
382 #endif /* HAVE_FUTIMESAT || HAVE_WORKING_UTIMES */
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
383
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
384 if (!file)
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
385 {
12317
407921985bc0 utimens: fix regression on Solaris
Eric Blake <ebb9@byu.net>
parents: 12256
diff changeset
386 #if ! ((HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG) \
407921985bc0 utimens: fix regression on Solaris
Eric Blake <ebb9@byu.net>
parents: 12256
diff changeset
387 || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
388 errno = ENOSYS;
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
389 #endif
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
390 return -1;
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
391 }
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
392
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
393 #if HAVE_WORKING_UTIMES
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
394 return utimes (file, t);
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
395 #else
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
396 {
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
397 struct utimbuf utimbuf;
12157
7fb8a2ac5948 utimens: validate futimens usage
Eric Blake <ebb9@byu.net>
parents: 12134
diff changeset
398 struct utimbuf *ut;
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
399 if (ts)
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
400 {
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
401 utimbuf.actime = ts[0].tv_sec;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
402 utimbuf.modtime = ts[1].tv_sec;
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
403 ut = &utimbuf;
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
404 }
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
405 else
12133
799664c0f270 openat, utimens: whitespace cleanup
Eric Blake <ebb9@byu.net>
parents: 11941
diff changeset
406 ut = NULL;
10133
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
407
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
408 return utime (file, ut);
bbccff5988c3 Prefer new POSIX 200x interfaces over futimesat.
Eric Blake <ebb9@byu.net>
parents: 9613
diff changeset
409 }
10154
148190ed341d Provide futimens/utimensat fallbacks for older kernels.
Eric Blake <ebb9@byu.net>
parents: 10133
diff changeset
410 #endif /* !HAVE_WORKING_UTIMES */
6297
10379892281e * utimens.c (ENOSYS): Define if not already defined.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6283
diff changeset
411 }
5147
3249aec7f48c Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
412 }
5485
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
413
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
414 /* Set the access and modification time stamps of FILE to be
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
415 TIMESPEC[0] and TIMESPEC[1], respectively. */
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
416 int
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
417 utimens (char const *file, struct timespec const timespec[2])
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
418 {
13696
bfe487360d66 fdutimens, fdutimensat: update signature, again
Eric Blake <eblake@redhat.com>
parents: 13284
diff changeset
419 return fdutimens (-1, file, timespec);
5485
863556283cee (__attribute__, ATTRIBUTE_UNUSED): New macros.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5147
diff changeset
420 }
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
421
12173
efdb2ab5743f utimens: let lutimens work on non-symlinks
Eric Blake <ebb9@byu.net>
parents: 12172
diff changeset
422 /* Set the access and modification time stamps of FILE to be
efdb2ab5743f utimens: let lutimens work on non-symlinks
Eric Blake <ebb9@byu.net>
parents: 12172
diff changeset
423 TIMESPEC[0] and TIMESPEC[1], respectively, without dereferencing
efdb2ab5743f utimens: let lutimens work on non-symlinks
Eric Blake <ebb9@byu.net>
parents: 12172
diff changeset
424 symlinks. Fail with ENOSYS if the platform does not support
efdb2ab5743f utimens: let lutimens work on non-symlinks
Eric Blake <ebb9@byu.net>
parents: 12172
diff changeset
425 changing symlink timestamps, but FILE was a symlink. */
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
426 int
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
427 lutimens (char const *file, struct timespec const timespec[2])
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
428 {
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
429 struct timespec adjusted_timespec[2];
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
430 struct timespec *ts = timespec ? adjusted_timespec : NULL;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
431 int adjustment_needed = 0;
12173
efdb2ab5743f utimens: let lutimens work on non-symlinks
Eric Blake <ebb9@byu.net>
parents: 12172
diff changeset
432 struct stat st;
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
433
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
434 if (ts)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
435 {
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
436 adjusted_timespec[0] = timespec[0];
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
437 adjusted_timespec[1] = timespec[1];
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
438 adjustment_needed = validate_timespec (ts);
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
439 }
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
440 if (adjustment_needed < 0)
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
441 return -1;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
442
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
443 /* The Linux kernel did not support symlink timestamps until
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
444 utimensat, in version 2.6.22, so we don't need to mimic
13696
bfe487360d66 fdutimens, fdutimensat: update signature, again
Eric Blake <eblake@redhat.com>
parents: 13284
diff changeset
445 fdutimens' worry about buggy NFS clients. But we do have to
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
446 worry about bogus return values. */
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
447
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
448 #if HAVE_UTIMENSAT
12353
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
449 if (0 <= lutimensat_works_really)
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
450 {
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
451 int result;
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
452 # if __linux__
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
453 /* As recently as Linux kernel 2.6.32 (Dec 2009), several file
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
454 systems (xfs, ntfs-3g) have bugs with a single UTIME_OMIT,
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
455 but work if both times are either explicitly specified or
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
456 UTIME_NOW. Work around it with a preparatory lstat prior to
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
457 calling utimensat; fortunately, there is not much timing
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
458 impact due to the extra syscall even on file systems where
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
459 UTIME_OMIT would have worked. FIXME: Simplify this in 2012,
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
460 when file system bugs are no longer common. */
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
461 if (adjustment_needed == 2)
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
462 {
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
463 if (lstat (file, &st))
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
464 return -1;
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
465 if (ts[0].tv_nsec == UTIME_OMIT)
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
466 ts[0] = get_stat_atime (&st);
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
467 else if (ts[1].tv_nsec == UTIME_OMIT)
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
468 ts[1] = get_stat_mtime (&st);
12554
0774f328f2ff utimens: avoid shadowing warning
Eric Blake <ebb9@byu.net>
parents: 12525
diff changeset
469 /* Note that st is good, in case utimensat gives ENOSYS. */
0774f328f2ff utimens: avoid shadowing warning
Eric Blake <ebb9@byu.net>
parents: 12525
diff changeset
470 adjustment_needed++;
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
471 }
12525
53f80a530574 futimens, utimensat: work around ntfs-3g bug
Eric Blake <ebb9@byu.net>
parents: 12518
diff changeset
472 # endif /* __linux__ */
12467
912bdb1c31fb utimens: work around Linux ctime bug
Eric Blake <ebb9@byu.net>
parents: 12447
diff changeset
473 result = utimensat (AT_FDCWD, file, ts, AT_SYMLINK_NOFOLLOW);
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
474 # ifdef __linux__
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
475 /* Work around a kernel bug:
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
476 http://bugzilla.redhat.com/442352
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
477 http://bugzilla.redhat.com/449910
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
478 It appears that utimensat can mistakenly return 280 rather
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
479 than -1 upon ENOSYS failure.
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
480 FIXME: remove in 2010 or whenever the offending kernels
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
481 are no longer in common use. */
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
482 if (0 < result)
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
483 errno = ENOSYS;
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
484 # endif
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
485 if (result == 0 || errno != ENOSYS)
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
486 {
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
487 utimensat_works_really = 1;
12353
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
488 lutimensat_works_really = 1;
12172
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
489 return result;
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
490 }
52ebfd595434 utimens: cache whether utimensat syscall works
Eric Blake <ebb9@byu.net>
parents: 12162
diff changeset
491 }
12353
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
492 lutimensat_works_really = -1;
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
493 #endif /* HAVE_UTIMENSAT */
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
494
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
495 /* The platform lacks an interface to set file timestamps with
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
496 nanosecond resolution, so do the best we can, discarding any
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
497 fractional part of the timestamp. */
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
498
12194
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
499 if (adjustment_needed || REPLACE_FUNC_STAT_FILE)
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
500 {
12554
0774f328f2ff utimens: avoid shadowing warning
Eric Blake <ebb9@byu.net>
parents: 12525
diff changeset
501 if (adjustment_needed != 3 && lstat (file, &st))
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
502 return -1;
12194
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
503 if (ts && update_timespec (&st, &ts))
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
504 return 0;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
505 }
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
506
12353
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
507 /* On Linux, lutimes is a thin wrapper around utimensat, so there is
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
508 no point trying lutimes if utimensat failed with ENOSYS. */
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
509 #if HAVE_LUTIMES && !HAVE_UTIMENSAT
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
510 {
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
511 struct timeval timeval[2];
13284
e08c8b78a796 utimens: avoid compiler warning
Andreas Gruenbacher <agruen@suse.de>
parents: 12580
diff changeset
512 struct timeval *t;
12447
b79168ca6a08 utimens: one more try at avoiding compiler warning
Eric Blake <ebb9@byu.net>
parents: 12377
diff changeset
513 int result;
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
514 if (ts)
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
515 {
12160
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
516 timeval[0].tv_sec = ts[0].tv_sec;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
517 timeval[0].tv_usec = ts[0].tv_nsec / 1000;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
518 timeval[1].tv_sec = ts[1].tv_sec;
d0732ed2dd55 utimens: add UTIME_NOW and UTIME_OMIT support
Eric Blake <ebb9@byu.net>
parents: 12158
diff changeset
519 timeval[1].tv_usec = ts[1].tv_nsec / 1000;
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
520 t = timeval;
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
521 }
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
522 else
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
523 t = NULL;
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
524
12353
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
525 result = lutimes (file, t);
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
526 if (result == 0 || errno != ENOSYS)
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
527 return result;
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
528 }
12353
fd066c132b7d utimens: work around older Linux failure with symlinks
Eric Blake <ebb9@byu.net>
parents: 12317
diff changeset
529 #endif /* HAVE_LUTIMES && !HAVE_UTIMENSAT */
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
530
12173
efdb2ab5743f utimens: let lutimens work on non-symlinks
Eric Blake <ebb9@byu.net>
parents: 12172
diff changeset
531 /* Out of luck for symlinks, but we still handle regular files. */
12194
16c7c4fa9754 utimensat: work around Solaris 9 bug
Eric Blake <ebb9@byu.net>
parents: 12174
diff changeset
532 if (!(adjustment_needed || REPLACE_FUNC_STAT_FILE) && lstat (file, &st))
12173
efdb2ab5743f utimens: let lutimens work on non-symlinks
Eric Blake <ebb9@byu.net>
parents: 12172
diff changeset
533 return -1;
efdb2ab5743f utimens: let lutimens work on non-symlinks
Eric Blake <ebb9@byu.net>
parents: 12172
diff changeset
534 if (!S_ISLNK (st.st_mode))
13696
bfe487360d66 fdutimens, fdutimensat: update signature, again
Eric Blake <eblake@redhat.com>
parents: 13284
diff changeset
535 return fdutimens (-1, file, ts);
12158
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
536 errno = ENOSYS;
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
537 return -1;
899dd13e9f8b utimens: add lutimens interface
Eric Blake <ebb9@byu.net>
parents: 12157
diff changeset
538 }