int diskutil_write2file (const char * file, const char * str) { int ret = OK; char tmpfile [] = "/tmp/euca-temp-XXXXXX"; int fd = safe_mkstemp (tmpfile); if (fd<0) { logprintfl (EUCAERROR, "{%u} error: failed to create temporary directory\n", (unsigned int)pthread_self()); unlink(tmpfile); return ERROR; } int size = strlen (str); if (write (fd, str, size) != size) { logprintfl (EUCAERROR, "{%u} error: failed to create temporary directory\n", (unsigned int)pthread_self()); ret = ERROR; } else { if (diskutil_cp (tmpfile, file) != OK) { logprintfl (EUCAERROR, "{%u} error: failed to copy temp file to destination (%s)\n", (unsigned int)pthread_self(), file); ret = ERROR; } } close (fd); unlink(tmpfile); return ret; }
//! //! //! //! @param[in] file //! @param[in] str //! //! @return EUCA_OK on success or the following error codes: //! \li EUCA_ACCESS_ERROR: if we fail to write the given data. //! \li EUCA_INVALID_ERROR: if any parameter does not meet the preconditions. //! \li EUCA_PERMISSION_ERROR: if we fail to create the temp or destination files. //! //! @pre Both file and str parameters must not be NULL. //! //! @post On success, the file is created and contains the str data. //! int diskutil_write2file(const char *file, const char *str) { int fd = -1; int ret = EUCA_OK; int size = 0; char tmpfile[] = "/tmp/euca-temp-XXXXXX"; if (file && str) { if ((fd = safe_mkstemp(tmpfile)) < 0) { LOGERROR("failed to create temporary directory\n"); unlink(tmpfile); return (EUCA_PERMISSION_ERROR); } size = strlen(str); if (write(fd, str, size) != size) { LOGERROR("failed to write to temporary directory\n"); ret = EUCA_ACCESS_ERROR; } else { if (diskutil_cp(tmpfile, file) != EUCA_OK) { LOGERROR("failed to copy temp file (%s)\n", file); ret = EUCA_PERMISSION_ERROR; } } close(fd); unlink(tmpfile); return (ret); } LOGERROR("failed to write to file. Bad params: file=%p, str=%p\n", file, str); return (EUCA_INVALID_ERROR); }