{"action":"created","comment":{"url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/comments/24104001","id":24104001,"diff_hunk":"@@ -1522,6 +1524,143 @@ for every line.\n     }\n \n /**\n+Creates a file with a randomly generated name and returns an open File to it.\n+\n+The name will start with the given prefix and be located in the given directory.\n+If no directory is given, then it will default to the result of\n+$(XREF file, tempDir).\n+\n+There is nothing special about the file created other than the fact that its\n+name is randomly generated, and it defaults to being in the temp directory on\n+the system. It will only be deleted if explicitly deleted or if it's in the temp\n+directory, and the temp is cleared out (as happens on system startup or shutdown\n+on some systems).\n+\n+Params:\n+    prefix = Optional prefix for file name.\n+    suffix = Optional suffix for file name (which also provides a way to give\n+             the file an extension).\n+    dir    = Directory of temporary file.\n+  */\n+    static File tempFile(string prefix = \"\", string suffix = \"\", string dir = \"\")\n+    {\n+        import std.exception : ErrnoException;\n+        import std.file : isSymlink, tempDir;\n+        import std.path : absolutePath, baseName, buildPath, dirName;\n+\n+        // For some reason, import std.file at the module level of std.stdio or\n+        // even just inside of File results in a static assertion being hit in\n+        // std.range.primitives.put. So, for now at least, we'll just assign\n+        // dir here rather than using tempDir as a default argument so that the\n+        // import can be local.\n+        if(dir.empty)\n+            dir = std.file.tempDir();\n+\n+        static string genTempName(string prefix, string suffix, string dir)\n+        {\n+            import std.ascii : digits, letters;\n+            import std.random : rndGen;\n+            rndGen.popFront();\n+            auto name = new char[](prefix.length + 10 + rndGen.front % 5 + suffix.length);\n+            name[0 .. prefix.length] = prefix;\n+\n+            char[letters.length + digits.length] chars;\n+            chars[0 .. letters.length] = letters[];\n+            chars[letters.length .. $] = digits[];\n+\n+            foreach(ref c; name[prefix.length .. $])\n+            {\n+                c = chars[rndGen.front % chars.length];\n+                rndGen.popFront();\n+            }\n+\n+            name[$ - suffix.length .. $] = suffix;\n+            return buildPath(dir, name);\n+        }\n+\n+        // Once should be enough given the randomness, but we'll give it three\n+        // times to be safe.\n+        foreach(i; 0 .. 3)\n+        {\n+            auto filename = genTempName(prefix, suffix, dir);\n+\n+            // O_EXCL does not support symbolic links.\n+            if(dirName(filename).isSymlink)\n+                filename = buildPath(absolutePath(dirName(filename), baseName(filename)));\n+\n+            import std.internal.cstring : tempCString;\n+            version(Posix)\n+            {\n+                import core.sys.posix.sys.stat, core.sys.posix.fcntl;\n+                enum flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;\n+                auto fd = open(tempCString(filename), O_RDWR | O_CREAT | O_EXCL, flags);\n+            }\n+            else version(Windows)\n+            {\n+                enum flags = _S_IREAD | _S_IWRITE;\n+                auto fd = _wsopen(tempCString!wchar(filename), O_RDWR | O_CREAT | O_EXCL, _SH_DENYNO, flags);\n+            }\n+            else static assert(\"Unsupported OS\");\n+\n+            if(fd == -1)\n+                continue;\n+\n+            File retval;\n+            retval.fdopen(fd, \"w+b\", filename);","path":"std/stdio.d","position":111,"original_position":111,"commit_id":"730595655ce3abe9cb6a1d65e378d49d1c14ad5c","original_commit_id":"730595655ce3abe9cb6a1d65e378d49d1c14ad5c","user":{"login":"klickverbot","id":19335,"avatar_url":"https://avatars.githubusercontent.com/u/19335?v=3","gravatar_id":"","url":"https://api.github.com/users/klickverbot","html_url":"https://github.com/klickverbot","followers_url":"https://api.github.com/users/klickverbot/followers","following_url":"https://api.github.com/users/klickverbot/following{/other_user}","gists_url":"https://api.github.com/users/klickverbot/gists{/gist_id}","starred_url":"https://api.github.com/users/klickverbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/klickverbot/subscriptions","organizations_url":"https://api.github.com/users/klickverbot/orgs","repos_url":"https://api.github.com/users/klickverbot/repos","events_url":"https://api.github.com/users/klickverbot/events{/privacy}","received_events_url":"https://api.github.com/users/klickverbot/received_events","type":"User","site_admin":false},"body":"Maybe document the open mode?","created_at":"2015-02-04T18:02:06Z","updated_at":"2015-02-04T18:02:06Z","html_url":"https://github.com/D-Programming-Language/phobos/pull/2956#discussion_r24104001","pull_request_url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/2956","_links":{"self":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/comments/24104001"},"html":{"href":"https://github.com/D-Programming-Language/phobos/pull/2956#discussion_r24104001"},"pull_request":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/2956"}}},"pull_request":{"url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/2956","id":28624364,"html_url":"https://github.com/D-Programming-Language/phobos/pull/2956","diff_url":"https://github.com/D-Programming-Language/phobos/pull/2956.diff","patch_url":"https://github.com/D-Programming-Language/phobos/pull/2956.patch","issue_url":"https://api.github.com/repos/D-Programming-Language/phobos/issues/2956","number":2956,"state":"open","locked":false,"title":"Implement issue# 13996. Add File.tempFile.","user":{"login":"jmdavis","id":579956,"avatar_url":"https://avatars.githubusercontent.com/u/579956?v=3","gravatar_id":"","url":"https://api.github.com/users/jmdavis","html_url":"https://github.com/jmdavis","followers_url":"https://api.github.com/users/jmdavis/followers","following_url":"https://api.github.com/users/jmdavis/following{/other_user}","gists_url":"https://api.github.com/users/jmdavis/gists{/gist_id}","starred_url":"https://api.github.com/users/jmdavis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmdavis/subscriptions","organizations_url":"https://api.github.com/users/jmdavis/orgs","repos_url":"https://api.github.com/users/jmdavis/repos","events_url":"https://api.github.com/users/jmdavis/events{/privacy}","received_events_url":"https://api.github.com/users/jmdavis/received_events","type":"User","site_admin":false},"body":"This adds a File.tempFile, which generates a random file name and\r\nreturns an open std.stdio.File for it. Unlike with File.tmpfile, it's a\r\nnormal file which is _not_ deleted when the file is closed, and you\r\nactually have access to the file's name, which is necessary in many\r\nsituations - particularly when writing unit tests that need to write to\r\na file and then read from it.","created_at":"2015-02-04T09:39:38Z","updated_at":"2015-02-04T18:02:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"49f890e3aad6049f251d24ade3ae7ae5ef277d80","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/2956/commits","review_comments_url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/2956/comments","review_comment_url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/comments/{number}","comments_url":"https://api.github.com/repos/D-Programming-Language/phobos/issues/2956/comments","statuses_url":"https://api.github.com/repos/D-Programming-Language/phobos/statuses/730595655ce3abe9cb6a1d65e378d49d1c14ad5c","head":{"label":"jmdavis:tempFile","ref":"tempFile","sha":"730595655ce3abe9cb6a1d65e378d49d1c14ad5c","user":{"login":"jmdavis","id":579956,"avatar_url":"https://avatars.githubusercontent.com/u/579956?v=3","gravatar_id":"","url":"https://api.github.com/users/jmdavis","html_url":"https://github.com/jmdavis","followers_url":"https://api.github.com/users/jmdavis/followers","following_url":"https://api.github.com/users/jmdavis/following{/other_user}","gists_url":"https://api.github.com/users/jmdavis/gists{/gist_id}","starred_url":"https://api.github.com/users/jmdavis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmdavis/subscriptions","organizations_url":"https://api.github.com/users/jmdavis/orgs","repos_url":"https://api.github.com/users/jmdavis/repos","events_url":"https://api.github.com/users/jmdavis/events{/privacy}","received_events_url":"https://api.github.com/users/jmdavis/received_events","type":"User","site_admin":false},"repo":{"id":1344978,"name":"phobos","full_name":"jmdavis/phobos","owner":{"login":"jmdavis","id":579956,"avatar_url":"https://avatars.githubusercontent.com/u/579956?v=3","gravatar_id":"","url":"https://api.github.com/users/jmdavis","html_url":"https://github.com/jmdavis","followers_url":"https://api.github.com/users/jmdavis/followers","following_url":"https://api.github.com/users/jmdavis/following{/other_user}","gists_url":"https://api.github.com/users/jmdavis/gists{/gist_id}","starred_url":"https://api.github.com/users/jmdavis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmdavis/subscriptions","organizations_url":"https://api.github.com/users/jmdavis/orgs","repos_url":"https://api.github.com/users/jmdavis/repos","events_url":"https://api.github.com/users/jmdavis/events{/privacy}","received_events_url":"https://api.github.com/users/jmdavis/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jmdavis/phobos","description":"Runtime library for the D programming language","fork":true,"url":"https://api.github.com/repos/jmdavis/phobos","forks_url":"https://api.github.com/repos/jmdavis/phobos/forks","keys_url":"https://api.github.com/repos/jmdavis/phobos/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jmdavis/phobos/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jmdavis/phobos/teams","hooks_url":"https://api.github.com/repos/jmdavis/phobos/hooks","issue_events_url":"https://api.github.com/repos/jmdavis/phobos/issues/events{/number}","events_url":"https://api.github.com/repos/jmdavis/phobos/events","assignees_url":"https://api.github.com/repos/jmdavis/phobos/assignees{/user}","branches_url":"https://api.github.com/repos/jmdavis/phobos/branches{/branch}","tags_url":"https://api.github.com/repos/jmdavis/phobos/tags","blobs_url":"https://api.github.com/repos/jmdavis/phobos/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jmdavis/phobos/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jmdavis/phobos/git/refs{/sha}","trees_url":"https://api.github.com/repos/jmdavis/phobos/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jmdavis/phobos/statuses/{sha}","languages_url":"https://api.github.com/repos/jmdavis/phobos/languages","stargazers_url":"https://api.github.com/repos/jmdavis/phobos/stargazers","contributors_url":"https://api.github.com/repos/jmdavis/phobos/contributors","subscribers_url":"https://api.github.com/repos/jmdavis/phobos/subscribers","subscription_url":"https://api.github.com/repos/jmdavis/phobos/subscription","commits_url":"https://api.github.com/repos/jmdavis/phobos/commits{/sha}","git_commits_url":"https://api.github.com/repos/jmdavis/phobos/git/commits{/sha}","comments_url":"https://api.github.com/repos/jmdavis/phobos/comments{/number}","issue_comment_url":"https://api.github.com/repos/jmdavis/phobos/issues/comments/{number}","contents_url":"https://api.github.com/repos/jmdavis/phobos/contents/{+path}","compare_url":"https://api.github.com/repos/jmdavis/phobos/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jmdavis/phobos/merges","archive_url":"https://api.github.com/repos/jmdavis/phobos/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jmdavis/phobos/downloads","issues_url":"https://api.github.com/repos/jmdavis/phobos/issues{/number}","pulls_url":"https://api.github.com/repos/jmdavis/phobos/pulls{/number}","milestones_url":"https://api.github.com/repos/jmdavis/phobos/milestones{/number}","notifications_url":"https://api.github.com/repos/jmdavis/phobos/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jmdavis/phobos/labels{/name}","releases_url":"https://api.github.com/repos/jmdavis/phobos/releases{/id}","created_at":"2011-02-09T04:09:04Z","updated_at":"2015-02-03T15:26:52Z","pushed_at":"2015-02-04T09:13:40Z","git_url":"git://github.com/jmdavis/phobos.git","ssh_url":"git@github.com:jmdavis/phobos.git","clone_url":"https://github.com/jmdavis/phobos.git","svn_url":"https://github.com/jmdavis/phobos","homepage":"d-programming-language.org","size":37853,"stargazers_count":2,"watchers_count":2,"language":"D","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":2,"default_branch":"master"}},"base":{"label":"D-Programming-Language:master","ref":"master","sha":"4de96a0620ddfc63518aacb328a8a26d8f48606d","user":{"login":"D-Programming-Language","id":565913,"avatar_url":"https://avatars.githubusercontent.com/u/565913?v=3","gravatar_id":"","url":"https://api.github.com/users/D-Programming-Language","html_url":"https://github.com/D-Programming-Language","followers_url":"https://api.github.com/users/D-Programming-Language/followers","following_url":"https://api.github.com/users/D-Programming-Language/following{/other_user}","gists_url":"https://api.github.com/users/D-Programming-Language/gists{/gist_id}","starred_url":"https://api.github.com/users/D-Programming-Language/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/D-Programming-Language/subscriptions","organizations_url":"https://api.github.com/users/D-Programming-Language/orgs","repos_url":"https://api.github.com/users/D-Programming-Language/repos","events_url":"https://api.github.com/users/D-Programming-Language/events{/privacy}","received_events_url":"https://api.github.com/users/D-Programming-Language/received_events","type":"Organization","site_admin":false},"repo":{"id":1257084,"name":"phobos","full_name":"D-Programming-Language/phobos","owner":{"login":"D-Programming-Language","id":565913,"avatar_url":"https://avatars.githubusercontent.com/u/565913?v=3","gravatar_id":"","url":"https://api.github.com/users/D-Programming-Language","html_url":"https://github.com/D-Programming-Language","followers_url":"https://api.github.com/users/D-Programming-Language/followers","following_url":"https://api.github.com/users/D-Programming-Language/following{/other_user}","gists_url":"https://api.github.com/users/D-Programming-Language/gists{/gist_id}","starred_url":"https://api.github.com/users/D-Programming-Language/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/D-Programming-Language/subscriptions","organizations_url":"https://api.github.com/users/D-Programming-Language/orgs","repos_url":"https://api.github.com/users/D-Programming-Language/repos","events_url":"https://api.github.com/users/D-Programming-Language/events{/privacy}","received_events_url":"https://api.github.com/users/D-Programming-Language/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/D-Programming-Language/phobos","description":"The standard library of the D programming language","fork":false,"url":"https://api.github.com/repos/D-Programming-Language/phobos","forks_url":"https://api.github.com/repos/D-Programming-Language/phobos/forks","keys_url":"https://api.github.com/repos/D-Programming-Language/phobos/keys{/key_id}","collaborators_url":"https://api.github.com/repos/D-Programming-Language/phobos/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/D-Programming-Language/phobos/teams","hooks_url":"https://api.github.com/repos/D-Programming-Language/phobos/hooks","issue_events_url":"https://api.github.com/repos/D-Programming-Language/phobos/issues/events{/number}","events_url":"https://api.github.com/repos/D-Programming-Language/phobos/events","assignees_url":"https://api.github.com/repos/D-Programming-Language/phobos/assignees{/user}","branches_url":"https://api.github.com/repos/D-Programming-Language/phobos/branches{/branch}","tags_url":"https://api.github.com/repos/D-Programming-Language/phobos/tags","blobs_url":"https://api.github.com/repos/D-Programming-Language/phobos/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/D-Programming-Language/phobos/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/D-Programming-Language/phobos/git/refs{/sha}","trees_url":"https://api.github.com/repos/D-Programming-Language/phobos/git/trees{/sha}","statuses_url":"https://api.github.com/repos/D-Programming-Language/phobos/statuses/{sha}","languages_url":"https://api.github.com/repos/D-Programming-Language/phobos/languages","stargazers_url":"https://api.github.com/repos/D-Programming-Language/phobos/stargazers","contributors_url":"https://api.github.com/repos/D-Programming-Language/phobos/contributors","subscribers_url":"https://api.github.com/repos/D-Programming-Language/phobos/subscribers","subscription_url":"https://api.github.com/repos/D-Programming-Language/phobos/subscription","commits_url":"https://api.github.com/repos/D-Programming-Language/phobos/commits{/sha}","git_commits_url":"https://api.github.com/repos/D-Programming-Language/phobos/git/commits{/sha}","comments_url":"https://api.github.com/repos/D-Programming-Language/phobos/comments{/number}","issue_comment_url":"https://api.github.com/repos/D-Programming-Language/phobos/issues/comments/{number}","contents_url":"https://api.github.com/repos/D-Programming-Language/phobos/contents/{+path}","compare_url":"https://api.github.com/repos/D-Programming-Language/phobos/compare/{base}...{head}","merges_url":"https://api.github.com/repos/D-Programming-Language/phobos/merges","archive_url":"https://api.github.com/repos/D-Programming-Language/phobos/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/D-Programming-Language/phobos/downloads","issues_url":"https://api.github.com/repos/D-Programming-Language/phobos/issues{/number}","pulls_url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls{/number}","milestones_url":"https://api.github.com/repos/D-Programming-Language/phobos/milestones{/number}","notifications_url":"https://api.github.com/repos/D-Programming-Language/phobos/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/D-Programming-Language/phobos/labels{/name}","releases_url":"https://api.github.com/repos/D-Programming-Language/phobos/releases{/id}","created_at":"2011-01-15T07:00:06Z","updated_at":"2015-02-04T04:01:56Z","pushed_at":"2015-02-04T04:01:55Z","git_url":"git://github.com/D-Programming-Language/phobos.git","ssh_url":"git@github.com:D-Programming-Language/phobos.git","clone_url":"https://github.com/D-Programming-Language/phobos.git","svn_url":"https://github.com/D-Programming-Language/phobos","homepage":"dlang.org/phobos","size":65921,"stargazers_count":559,"watchers_count":559,"language":"D","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":372,"mirror_url":null,"open_issues_count":40,"forks":372,"open_issues":40,"watchers":559,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/2956"},"html":{"href":"https://github.com/D-Programming-Language/phobos/pull/2956"},"issue":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/issues/2956"},"comments":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/issues/2956/comments"},"review_comments":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/2956/comments"},"review_comment":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/2956/commits"},"statuses":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/statuses/730595655ce3abe9cb6a1d65e378d49d1c14ad5c"}}}}