MCPcopy Create free account
hub / github.com/apache/cloudberry / OpenTemporaryFileInTablespace

Function OpenTemporaryFileInTablespace

src/backend/storage/file/fd.c:1746–1812  ·  view source on GitHub ↗

* Open a temporary file in a specific tablespace. * Subroutine for OpenTemporaryFile, which see for details. */

Source from the content-addressed store, hash-verified

1744 * Subroutine for OpenTemporaryFile, which see for details.
1745 */
1746static File
1747OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError,
1748 const char *filename, bool makenameunique, bool create)
1749{
1750 char tempdirpath[MAXPGPATH];
1751 char tempfilepath[MAXPGPATH];
1752 File file;
1753 int flags;
1754
1755 TempTablespacePath(tempdirpath, tblspcOid);
1756
1757 /*
1758 * Generate a tempfile name that should be unique within the current
1759 * database instance.
1760 */
1761 if (filename == NULL)
1762 {
1763 Assert (makenameunique);
1764 filename = "";
1765 }
1766
1767 if (makenameunique)
1768 {
1769 Assert(create);
1770 snprintf(tempfilepath, sizeof(tempfilepath), "%s/%s%s%d.%ld",
1771 tempdirpath, PG_TEMP_FILE_PREFIX, filename, MyProcPid, tempFileCounter++);
1772 }
1773 else
1774 snprintf(tempfilepath, sizeof(tempfilepath), "%s/%s_%s",
1775 tempdirpath, PG_TEMP_FILE_PREFIX, filename);
1776
1777 /*
1778 * Open the file. Note: we don't use O_EXCL, in case there is an orphaned
1779 * temp file that can be reused.
1780 */
1781 flags = O_RDWR | PG_BINARY;
1782 if (create)
1783 flags |= O_CREAT | O_TRUNC;
1784 file = PathNameOpenFile(tempfilepath,
1785 flags);
1786 if (file <= 0)
1787 {
1788 /*
1789 * We might need to create the tablespace's tempfile directory, if no
1790 * one has yet done so.
1791 *
1792 * Don't check for an error from MakePGDirectory; it could fail if
1793 * someone else just did the same thing. If it doesn't work then
1794 * we'll bomb out on the second create attempt, instead.
1795 */
1796 (void) MakePGDirectory(tempdirpath);
1797
1798 file = PathNameOpenFile(tempfilepath,
1799 flags);
1800 if (file <= 0 && rejectError)
1801 {
1802 if (create)
1803 elog(ERROR, "could not create temporary file \"%s\": %m",

Callers 1

OpenTemporaryFileFunction · 0.85

Calls 3

TempTablespacePathFunction · 0.85
PathNameOpenFileFunction · 0.85
MakePGDirectoryFunction · 0.85

Tested by

no test coverage detected