+
+/**
+ * Opens a file.
+ *
+ * @param path Path to a file.
+ * @param mode Mode.
+ * @return Stream handle.
+ * @throw ExtractFileFailed If error (other than EINTR) occurs.
+ */
+FILE* openFile(const std::string& path, const std::string& mode)
+{
+ FILE* result = NULL;
+
+ do
+ {
+ result = fopen(path.c_str(), mode.c_str());
+ } while ((NULL == result) && (EINTR == errno));
+
+ if (NULL == result)
+ {
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::InternalError,
+ "Could not open file " << path);
+ }
+
+ return result;
+}
+
+/**
+ * Reads bytes from a stream.
+ *
+ * @param buffer Buffer to read the bytes into.
+ * @param count Number of bytes to read.
+ * @param stream Stream to read from.
+ * @return Number of bytes read
+ * @throw ExtractFileFailed If error (other than EINTR) occurs.
+ */
+std::size_t readBytes(unsigned char* buffer, std::size_t count, FILE* stream)
+{
+ std::size_t result = std::fread(buffer,
+ sizeof(unsigned char),
+ count,
+ stream);
+
+ if (result != count)
+ {
+ int error = errno;
+ if (0 != std::ferror(stream))
+ {
+ if (EINTR != error)
+ {
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::InternalError,
+ "Error while reading data" <<
+ " [" << DPL::GetErrnoString(error) << "]");
+ }
+ }
+ }
+
+ return result;
+}
+
+/**
+ * Writes bytes to a stream.
+ *
+ * @param buffer Data to write.
+ * @param count Number of bytes.
+ * @param stream Stream to write to.
+ * @throw ExtractFileFailed If error (other than EINTR) occurs.
+ */
+void writeBytes(unsigned char* buffer, std::size_t count, FILE* stream)
+{
+ std::size_t bytesWritten = 0;
+ std::size_t bytesToWrite = 0;
+ do
+ {
+ bytesToWrite = count - bytesWritten;
+ bytesWritten = std::fwrite(buffer + bytesWritten,
+ sizeof(unsigned char),
+ count - bytesWritten,
+ stream);
+ if ((bytesWritten != bytesToWrite) && (EINTR != errno))
+ {
+ int error = errno;
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::InternalError,
+ "Error while writing data" <<
+ " [" << DPL::GetErrnoString(error) << "]");
+ }
+ } while ((bytesWritten != bytesToWrite) && (EINTR == errno));
+}