Changeset 94311 in vbox
- Timestamp:
- Mar 19, 2022 1:34:37 AM (3 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
-
include/iprt/file.h (modified) (4 diffs)
-
include/iprt/mangling.h (modified) (1 diff)
-
src/VBox/Runtime/generic/createtemp-generic.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/file.h
r94277 r94311 958 958 959 959 /** 960 * Creates a new file with a unique name using the given template. 960 * Creates a new file with a unique name using the given template, returning a 961 * handle to it. 961 962 * 962 963 * One or more trailing X'es in the template will be replaced by random alpha … … 971 972 * "/tmp/myprog-XXX-XXX.tmp" 972 973 * 974 * @returns IPRT status code. 975 * @param phFile Where to return the file handle on success. Set to 976 * NIL on failure. 977 * @param pszTemplate The file name template on input. The actual file 978 * name on success. Empty string on failure. 979 * @param fOpen The RTFILE_O_XXX flags to open the file with. 980 * RTFILE_O_CREATE is mandatory. 981 * @see RTFileCreateTemp 982 */ 983 RTDECL(int) RTFileCreateUnique(PRTFILE phFile, char *pszTemplate, uint64_t fOpen); 984 985 /** 986 * Creates a new file with a unique name using the given template. 987 * 988 * One or more trailing X'es in the template will be replaced by random alpha 989 * numeric characters until a RTFileOpen with RTFILE_O_CREATE succeeds or we 990 * run out of patience. 991 * For instance: 992 * "/tmp/myprog-XXXXXX" 993 * 994 * As an alternative to trailing X'es, it is possible to put 3 or more X'es 995 * somewhere inside the file name. In the following string only the last 996 * bunch of X'es will be modified: 997 * "/tmp/myprog-XXX-XXX.tmp" 998 * 973 999 * @returns iprt status code. 974 1000 * @param pszTemplate The file name template on input. The actual file … … 976 1002 * @param fMode The mode to create the file with. Use 0600 unless 977 1003 * you have reason not to. 1004 * @see RTFileCreateUnique 978 1005 */ 979 1006 RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode); … … 998 1025 * @param pszTemplate The file name template on input. The actual 999 1026 * file name on success. Empty string on failure. 1027 * @see RTFileCreateUnique 1000 1028 */ 1001 1029 RTDECL(int) RTFileCreateTempSecure(char *pszTemplate); -
trunk/include/iprt/mangling.h
r94300 r94311 968 968 # define RTFileCopyPartEx RT_MANGLER(RTFileCopyPartEx) 969 969 # define RTFileCopyPartPrep RT_MANGLER(RTFileCopyPartPrep) 970 # define RTFileCreateUnique RT_MANGLER(RTFileCreateUnique) 970 971 # define RTFileCreateTemp RT_MANGLER(RTFileCreateTemp) 971 972 # define RTFileCreateTempSecure RT_MANGLER(RTFileCreateTempSecure) -
trunk/src/VBox/Runtime/generic/createtemp-generic.cpp
r93115 r94311 158 158 159 159 160 RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode) 161 { 160 RTDECL(int) RTFileCreateUnique(PRTFILE phFile, char *pszTemplate, uint64_t fOpen) 161 { 162 /* 163 * Validate input. 164 */ 165 *phFile = NIL_RTFILE; 166 AssertReturn((fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE, VERR_INVALID_FLAGS); 162 167 char *pszX = NULL; 163 168 unsigned cXes = 0; 164 169 int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes); 165 if (RT_FAILURE(rc)) 166 { 167 *pszTemplate = '\0'; 168 return rc; 169 } 170 171 /* 172 * Try ten thousand times. 173 */ 174 int i = 10000; 175 while (i-- > 0) 176 { 177 uint64_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE | RTFILE_O_NOT_CONTENT_INDEXED 178 | fMode << RTFILE_O_CREATE_MODE_SHIFT; 179 rtCreateTempFillTemplate(pszX, cXes); 180 RTFILE hFile = NIL_RTFILE; 181 rc = RTFileOpen(&hFile, pszTemplate, fOpen); 182 if (RT_SUCCESS(rc)) 183 { 184 RTFileClose(hFile); 185 return rc; 186 } 187 /** @todo Anything else to consider? */ 188 if (rc != VERR_ALREADY_EXISTS) 189 { 190 *pszTemplate = '\0'; 191 return rc; 192 } 193 } 194 195 /* we've given up. */ 196 *pszTemplate = '\0'; 197 return VERR_ALREADY_EXISTS; 170 if (RT_SUCCESS(rc)) 171 { 172 /* 173 * Try ten thousand times. 174 */ 175 int i = 10000; 176 while (i-- > 0) 177 { 178 rtCreateTempFillTemplate(pszX, cXes); 179 RTFILE hFile = NIL_RTFILE; 180 rc = RTFileOpen(&hFile, pszTemplate, fOpen); 181 if (RT_SUCCESS(rc)) 182 { 183 *phFile = hFile; 184 return rc; 185 } 186 /** @todo Anything else to consider? */ 187 if (rc != VERR_ALREADY_EXISTS) 188 { 189 *pszTemplate = '\0'; 190 return rc; 191 } 192 } 193 194 /* we've given up. */ 195 rc = VERR_ALREADY_EXISTS; 196 } 197 *pszTemplate = '\0'; 198 return rc; 199 } 200 RT_EXPORT_SYMBOL(RTFileCreateUnique); 201 202 203 RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode) 204 { 205 RTFILE hFile = NIL_RTFILE; 206 int rc = RTFileCreateUnique(&hFile, pszTemplate, 207 RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE | RTFILE_O_NOT_CONTENT_INDEXED 208 | fMode << RTFILE_O_CREATE_MODE_SHIFT); 209 if (RT_SUCCESS(rc)) 210 RTFileClose(hFile); 211 return rc; 198 212 } 199 213 RT_EXPORT_SYMBOL(RTFileCreateTemp);
Note:
See TracChangeset
for help on using the changeset viewer.

