sodero / InstallerLG

A reimplementation of the LISP-like 'Installer' scripting language.
Other
16 stars 4 forks source link

tmp file #24

Closed polluks closed 6 years ago

polluks commented 6 years ago

https://github.com/sodero/InstallerLG/blob/58ba22e5f3f7a3288d2b88cc79f494efe587063e/src/file.c#L1738 mktemp() is not ANSI better use tmpfile().

sodero commented 6 years ago

With tmpfile() I can't control where the file ends up. It needs to end up in the same partition as s:user-startup, otherwise the rename operation will fail. Without the rename operation, writing to s:user-startup won't be atomic, and crashes / power failures and so on might lead to an empty user-startup. By using rename there will always be a valid user-startup, the old one or the new one, nothing in between.

polluks commented 6 years ago

I see, thanks. At least mkstemp() works w/o ixemul:

     Quite often a programmer will want to replace a use of mktemp() with
     mkstemp(), usually to avoid the problems described above.  Doing this
     correctly requires a good understanding of the code in question.

     For instance, code of this form:

           char sfn[15] = "";
           FILE *sfp;

           strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
           if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
                   fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
                   return (NULL);
           }
           return (sfp);

     should be rewritten like this:

           char sfn[15] = "";
           FILE *sfp;
           int fd = -1;

           strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
           if ((fd = mkstemp(sfn)) == -1 ||
               (sfp = fdopen(fd, "w+")) == NULL) {
                   if (fd != -1) {
                           unlink(sfn);
                           close(fd);
                   }
                   fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
                   return (NULL);
           }
           return (sfp);
sodero commented 6 years ago

Thanks, looks good. I'll go for mkstemp instead.