core.fsync: add configuration parsing

This change introduces code to parse the core.fsync setting and configure the fsync_components variable. core.fsync is configured as a comma-separated list of component names to sync. Each time a core.fsync variable is encountered in the configuration heirarchy, we start off with a clean state with the platform default value. Passing 'none' resets the value to indicate nothing will be synced. We gather all negative and positive entries from the comma separated list and then compute the new value by removing all the negative entries and adding all of the positive entries. We issue a warning for components that are not recognized so that the configuration code is compatible with configs from future versions of Git with more repo components. Complete documentation for the new setting is included in a later patch in the series so that it can be reviewed once in final form. Signed-off-by: Neeraj Singh <neerajsi@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Neeraj Singh committed Mar 10, 2022 at 22:43 UTC 844a8ad4f868dcac8851012fe6dafd49b301b2ae
3 files changed +82 -5
Documentation/config/core.txt
+5 -4
@@ -558,11 +558,12 @@ core.fsyncMethod::
558
559 core.fsyncObjectFiles::
560 This boolean will enable 'fsync()' when writing object files.
561 + This setting is deprecated. Use core.fsync instead.
562 +
562 -This is a total waste of time and effort on a filesystem that orders
563 -data writes properly, but can be useful for filesystems that do not use
564 -journalling (traditional UNIX filesystems) or that only journal metadata
565 -and not file contents (OS X's HFS+, or Linux ext3 with "data=writeback").
563 +This setting affects data added to the Git repository in loose-object
564 +form. When set to true, Git will issue an fsync or similar system call
565 +to flush caches so that loose-objects remain consistent in the face
566 +of a unclean system shutdown.
567
568 core.preloadIndex::
569 Enable parallel index preload for operations like 'git diff'
config.c
+76
@@ -1323,6 +1323,73 @@ static int git_parse_maybe_bool_text(const char *value)
1323 return -1;
1324 }
1325
1326 +static const struct fsync_component_name {
1327 + const char *name;
1328 + enum fsync_component component_bits;
1329 +} fsync_component_names[] = {
1330 + { "loose-object", FSYNC_COMPONENT_LOOSE_OBJECT },
1331 + { "pack", FSYNC_COMPONENT_PACK },
1332 + { "pack-metadata", FSYNC_COMPONENT_PACK_METADATA },
1333 + { "commit-graph", FSYNC_COMPONENT_COMMIT_GRAPH },
1334 +};
1335 +
1336 +static enum fsync_component parse_fsync_components(const char *var, const char *string)
1337 +{
1338 + enum fsync_component current = FSYNC_COMPONENTS_DEFAULT;
1339 + enum fsync_component positive = 0, negative = 0;
1340 +
1341 + while (string) {
1342 + int i;
1343 + size_t len;
1344 + const char *ep;
1345 + int negated = 0;
1346 + int found = 0;
1347 +
1348 + string = string + strspn(string, ", \t\n\r");
1349 + ep = strchrnul(string, ',');
1350 + len = ep - string;
1351 + if (!strcmp(string, "none")) {
1352 + current = FSYNC_COMPONENT_NONE;
1353 + goto next_name;
1354 + }
1355 +
1356 + if (*string == '-') {
1357 + negated = 1;
1358 + string++;
1359 + len--;
1360 + if (!len)
1361 + warning(_("invalid value for variable %s"), var);
1362 + }
1363 +
1364 + if (!len)
1365 + break;
1366 +
1367 + for (i = 0; i < ARRAY_SIZE(fsync_component_names); ++i) {
1368 + const struct fsync_component_name *n = &fsync_component_names[i];
1369 +
1370 + if (strncmp(n->name, string, len))
1371 + continue;
1372 +
1373 + found = 1;
1374 + if (negated)
1375 + negative |= n->component_bits;
1376 + else
1377 + positive |= n->component_bits;
1378 + }
1379 +
1380 + if (!found) {
1381 + char *component = xstrndup(string, len);
1382 + warning(_("ignoring unknown core.fsync component '%s'"), component);
1383 + free(component);
1384 + }
1385 +
1386 +next_name:
1387 + string = ep;
1388 + }
1389 +
1390 + return (current & ~negative) | positive;
1391 +}
1392 +
1393 int git_parse_maybe_bool(const char *value)
1394 {
1395 int v = git_parse_maybe_bool_text(value);
@@ -1600,6 +1667,13 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
1667 return 0;
1668 }
1669
1670 + if (!strcmp(var, "core.fsync")) {
1671 + if (!value)
1672 + return config_error_nonbool(var);
1673 + fsync_components = parse_fsync_components(var, value);
1674 + return 0;
1675 + }
1676 +
1677 if (!strcmp(var, "core.fsyncmethod")) {
1678 if (!value)
1679 return config_error_nonbool(var);
@@ -1613,6 +1687,8 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
1687 }
1688
1689 if (!strcmp(var, "core.fsyncobjectfiles")) {
1690 + if (fsync_object_files < 0)
1691 + warning(_("core.fsyncobjectfiles is deprecated; use core.fsync instead"));
1692 fsync_object_files = git_config_bool(var, value);
1693 return 0;
1694 }
environment.c
+1 -1
@@ -42,7 +42,7 @@ const char *git_attributes_file;
42 const char *git_hooks_path;
43 int zlib_compression_level = Z_BEST_SPEED;
44 int pack_compression_level = Z_DEFAULT_COMPRESSION;
45 -int fsync_object_files;
45 +int fsync_object_files = -1;
46 int use_fsync = -1;
47 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
48 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;