Bug 39914

Summary: Please tag the cache directory with CACHEDIR.TAG
Product: fontconfig Reporter: Josh Triplett <josh>
Component: fc-cacheAssignee: Akira TAGOH <akira>
Status: RESOLVED FIXED QA Contact: Behdad Esfahbod <freedesktop>
Severity: enhancement    
Priority: medium CC: akira, fontconfig-bugs, freedesktop
Version: 2.9   
Hardware: Other   
OS: All   
Whiteboard:
i915 platform: i915 features:

Description Josh Triplett 2011-08-07 19:22:04 UTC
Various backup utilities can automatically exclude cache directories if tagged via the "Cache Directory Tagging" standard (http://www.brynosaurus.com/cachedir/).  Doing so requires creating a file CACHEDIR.TAG in the cache directory (/var/cache/fontconfig or ~/.fontconfig), containing the contents "Signature: 8a477f597d28d172789f06886806bc55".  Please consider implementing this standard by creating this file, so that backup software can automatically exclude fontconfig caches.
Comment 1 Behdad Esfahbod 2011-08-08 08:24:59 UTC
Has the spec been discussed in the freedesktop.org context?  I'd be more open to implementation if it's moved to fdo.
Comment 2 Josh Triplett 2011-08-08 10:12:11 UTC
(In reply to comment #1)
> Has the spec been discussed in the freedesktop.org context?  I'd be more open
> to implementation if it's moved to fdo.

Various backup and archive tools have already adopted the spec, including tar, dar, and obnam.  Various programs already use it to tag their caches, including ccache, bzflag, and libdvdcss.  This seems sufficient to make implementation useful; in my case, I'd like it for use with the --exclude-caches options of both tar and obnam.

Adopting it as an fd.o standard seems like a fine idea, but entirely orthogonal to implementing it, and certainly not a prerequisite. :)
Comment 3 Behdad Esfahbod 2011-08-08 16:00:49 UTC
Patches are welcome :).
Comment 6 Akira TAGOH 2011-11-09 00:18:42 UTC
Has anyone tested or reviewed this patch yet?
Comment 7 Akira TAGOH 2012-02-21 01:55:21 UTC
Hmm, I guess it needs to be updated a bit for Windows.
Comment 8 Josh Triplett 2012-03-20 21:33:54 UTC
(In reply to comment #5)
> Correct the comment...
> http://cgit.freedesktop.org/~tagoh/fontconfig/commit/?h=bz39914&id=b6af6a06da68becff835370f4b9e313a2bc1d9de

Looks mostly reasonable.  You don't need line-continuation backslashes for the string constant, since each line has its own quoted string and all of them will get concatenated.  Also, the sentence "Otherwise it will ignores" needs grammatical fixing.  Other than that, the patch looks fine; ship it.
Comment 9 Josh Triplett 2012-03-20 21:35:31 UTC
(In reply to comment #7)
> Hmm, I guess it needs to be updated a bit for Windows.

Other than the use of '/' rather than whatever portable way fontconfig specifies path separators, your patch *looks* like it should work on other platforms.
Comment 10 Akira TAGOH 2012-03-20 22:57:13 UTC
Thanks for the comment.

http://cgit.freedesktop.org/~tagoh/fontconfig/commit/?h=bz39914

should works...
Comment 11 Josh Triplett 2012-03-20 23:17:20 UTC
(In reply to comment #10)
> Thanks for the comment.
> 
> http://cgit.freedesktop.org/~tagoh/fontconfig/commit/?h=bz39914
> 
> should works...

You still have a line continuation character that you don't need.

Otherwise, the patch looks reasonable.  I *think* fontconfig might have an existing mechanism for converting pathnames with '/' to Windows paths with '\\', but what you've written works, and someone with more knowledge of the Windows port can improve it later.

With the last line continuation character removed:
Reviewed-by: Josh Triplett <josh@joshtriplett.org>
Comment 12 Akira TAGOH 2012-03-21 00:10:29 UTC
Thanks. fixed in 4f7f3bf9.
Comment 13 Behdad Esfahbod 2012-03-21 09:45:12 UTC
First:

+ static const FcChar8 cache_tag_contents[] =
+ "Signature: 8a477f597d28d172789f06886806bc55\n"
+ "# This file is a cache directory tag created by fontconfig.\n"
+ "# For information about cache directory tags, see:\n"
+ "# http://www.brynosaurus.com/cachedir/\n";
+ static size_t cache_tag_contents_size = 0;
+ FcBool ret = FcTrue;
+
+ if (cache_tag_contents_size == 0)
+ cache_tag_contents_size = strlen((char *)cache_tag_contents);

You can simply use "sizeof (cache_tag_contents)".  No need for runtime strlen().


+ write(fd, cache_tag_contents, cache_tag_contents_size);

write() can fail with -EINTR.  Just use stdio or something.


Last but not least.  This is the wrong place to create this file.  Calling fc-cache is by all means considered optional.  The file should be create in the library.  Now, I'm inclined to say we should try creating it only when creating the cache directory, but that wouldn't help existing users.  I'd rather not add a stat() to every application's startup just for this file...
Comment 14 Josh Triplett 2012-03-21 10:51:07 UTC
(In reply to comment #13)
> First:
> 
> + static const FcChar8 cache_tag_contents[] =
> + "Signature: 8a477f597d28d172789f06886806bc55\n"
> + "# This file is a cache directory tag created by fontconfig.\n"
> + "# For information about cache directory tags, see:\n"
> + "# http://www.brynosaurus.com/cachedir/\n";
> + static size_t cache_tag_contents_size = 0;
> + FcBool ret = FcTrue;
> +
> + if (cache_tag_contents_size == 0)
> + cache_tag_contents_size = strlen((char *)cache_tag_contents);
> 
> You can simply use "sizeof (cache_tag_contents)".  No need for runtime
> strlen().

If you want to use sizeof, you'd need sizeof(cache_tag_contents)-1, since sizeof counts the trailing '\0'.  More importantly, though, compilers such as GCC know how to optimize strlen(constant_string) to a constant.

> + write(fd, cache_tag_contents, cache_tag_contents_size);
> 
> write() can fail with -EINTR.  Just use stdio or something.

True.
Comment 15 Akira TAGOH 2012-03-21 19:42:13 UTC
Thanks for the comment.

(In reply to comment #13)
> Last but not least.  This is the wrong place to create this file.  Calling
> fc-cache is by all means considered optional.  The file should be create in the
> library.  Now, I'm inclined to say we should try creating it only when creating
> the cache directory, but that wouldn't help existing users.  I'd rather not add
> a stat() to every application's startup just for this file...

Then we should keep the function in fc-cache to create this file at least. we could simply skip this process when updating caches at the startup time if necessary. creating CACHEDIR.TAG would be an optional then.
Comment 16 Behdad Esfahbod 2012-03-22 07:30:43 UTC
Maybe you can do it in fc-cache, and in the library only when we are creating a non-existing cache directory.
Comment 17 Behdad Esfahbod 2012-03-22 07:31:55 UTC
(In reply to comment #16)
> Maybe you can do it in fc-cache, and in the library only when we are creating a
> non-existing cache directory.

Infact that is necessary.  For the system-wide cache dir, rpm, etc, call fc-cache and the directory already exists.  For user fonts, user never calls fc-cache, and the first time the directory does not exist.  So we need both paths.
Comment 18 Akira TAGOH 2012-03-22 18:41:58 UTC
(In reply to comment #17)
> (In reply to comment #16)
> > Maybe you can do it in fc-cache, and in the library only when we are creating a
> > non-existing cache directory.
> 
> Infact that is necessary.  For the system-wide cache dir, rpm, etc, call
> fc-cache and the directory already exists.  For user fonts, user never calls
> fc-cache, and the first time the directory does not exist.  So we need both
> paths.

Sure. the easier way to share the efforts would be to have an API to do it in the library and call it as needed though, I don't think it's necessary for public and would rather be better doing it internally. so I'm pondering to have it as the internal API though, you said in another bug the tools like fc-cache and so on shouldn't include fcint.h. I don't want to have duplicate code as far as possible though...
Comment 19 Behdad Esfahbod 2012-03-24 08:58:54 UTC
Yes, all the utilities just use the public API.  We sure can add new API for this.
Comment 20 Akira TAGOH 2012-03-28 01:30:14 UTC
Okay, the patch is updated:
http://cgit.freedesktop.org/~tagoh/fontconfig/commit/?h=bz39914
Comment 21 Akira TAGOH 2012-04-04 20:15:04 UTC
hmm, just wondering if there are any way to hide this API. actually it's not related to fontconfig functionality at all. we should export scanDirs() in fc-cache instead of it perhaps?
Comment 22 Akira TAGOH 2012-04-04 20:16:25 UTC
reopened anyway. it's hard to keep it on track with FIXED status.
Comment 23 Akira TAGOH 2012-04-19 23:43:48 UTC
Okay, added instread:

FcPublic void
FcCacheCreateTagFile (const FcConfig *config);

that should be better than taking the cache_dir directly.
Comment 24 Akira TAGOH 2012-04-22 19:30:44 UTC
fixed in 06d6b7c3.

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.