1 Title: Improved string imports
2 Tags: en, d, string import, patch, import
4 D__ has a very nice capability of *string imports*. A string import let you read
5 a file at compile time as a string, for example::
7 pragma(msg, import("hello.txt"));
9 __ http://www.digitalmars.com/d/
11 Will print the contents of the file ``hello.txt`` when it's compiled, or it
12 will fail to compile if ``hello.txt`` is not readable or the ``-J`` option is
13 not used. The ``-J`` option is needed because of security reasons, otherwise
14 compiling a program could end up reading any file in your filesystem
15 (storing it in the binary and possibly violating your privacy). For example you
16 could compile a program as root and run it as an unprivileged user thinking it
17 can't possibly read some protected data, but that data could be read at
18 compile-time, with root privileges.
20 Anyway, D ask you to use the ``-J`` option if you are doing string imports,
21 which seems reasonable. What doesn't look so reasonable is that string imports
22 can't access a file in a subdirectory. Let's say we have a file ``test.d`` in
23 the current directory like this::
25 immutable s = import("data/hello.txt");
27 And in the current directory we have a subdirectory called ``data`` and a file
28 ``hello.txt`` in it. This won't compile, ever (no matter what ``-J`` option you
29 use). I think this is an unnecessary limitation, using ``-J.`` should work.
30 I can see why this was done like that, what if you write::
32 immutable s = import("../hello.txt");
34 It looks like this shouldn't work, so we can ban ``..`` from string imports,
37 immutable s = import("data/../data/hello.txt");
39 This should work, it's a little convoluted but it should work. And what about
42 Well, I think this limitation can be relaxed (other people think that too,
43 there is even a `bug report`__ for this), at least on POSIX__\ -compatible
44 OSs, because we can use the `realpath()`__ function to *resolve* the file. If
45 you *resolve* both the ``-J`` directories and the resulting files, it's very
46 easy to check if the string import file really belongs to a ``-J`` subdirectory
49 __ http://d.puremagic.com/issues/show_bug.cgi?id=3420
50 __ http://en.wikipedia.org/wiki/POSIX
51 __ http://www.opengroup.org/onlinepubs/000095399/functions/realpath.html
53 This looks very trivial to implement, so I gave it a shot and posted a patch__
54 and a couple of `test cases`__ to that very same bug report :)
56 __ http://d.puremagic.com/issues/attachment.cgi?id=520
57 __ http://d.puremagic.com/issues/attachment.cgi?id=521&action=edit
59 The patch is incomplete, though, because it's only tested on Linux and it lacks
60 Windows support (I don't know how to do this on Windows and don't have an
61 environment to test it). If you like this feature and you know Windows, please
62 complete the patch, so it has better chances to make it in D2, you only have to
63 implement the ``canonicalName()`` function. If you have other supported POSIX
64 OS, please test the patch and report any problems.
68 .. vim: set et sw=4 sts=4 :