20#include <osl/process.h>
21#include <rtl/strbuf.hxx>
22#include <rtl/ustring.hxx>
23#include <osl/thread.h>
24#include <osl/file.hxx>
56 if ((
index=sFileName.lastIndexOf(
'/')) > 0)
57 return sFileName.copy(0,
index);
59 if ((
index=sFileName.lastIndexOf(
'\\')) > 0)
60 return sFileName.copy(0,
index);
66 const OString& typeName,
67 const OString& postfix )
71 sal_Int32
length = destination.getLength();
73 bool bWithPoint =
false;
82 bool bWithSeparator =
false;
83 if (!(destination.endsWith(
"\\") || destination.endsWith(
"/"))
84 && !(
type.startsWith(
"\\") ||
type.startsWith(
"/")))
87 bWithSeparator =
true;
90 OStringBuffer fileNameBuf(
length);
93 fileNameBuf.append(
'.');
95 fileNameBuf.append(destination);
98 fileNameBuf.append(
"/");
102 OString fileName(fileNameBuf.makeStringAndClear());
106 fileName = fileName.replace(
'\\',
'/');
109 fileName = fileName.replace(
'/',
'\\');
113 OStringBuffer buffer(
length);
122 if (buffer.isEmpty() || std::string_view(
".") == buffer)
124 buffer.append(token);
129 if (mkdir(buffer.getStr(), 0777) == -1)
131 if (mkdir(buffer.getStr()) == -1)
134 if ( errno == ENOENT )
138 buffer.append(token);
141 OUString uSysFileName;
142 OSL_VERIFY( FileBase::getSystemPathFromFileURL(
149 FILE *f= fopen(fileName.getStr(),
"r");
162 FILE *target = fopen(targetFileName.getStr(),
"r");
163 FILE *tmp = fopen(tmpFileName.getStr(),
"r");
164 bool bFindChanges =
false;
166 if (target !=
nullptr && tmp !=
nullptr)
168 char buffer1[1024+1];
169 char buffer2[1024+1];
173 while ( !bFindChanges && !feof(target) && !feof(tmp))
175 n1 = fread(buffer1,
sizeof(
char), 1024, target);
176 n2 = fread(buffer2,
sizeof(
char), 1024, tmp);
181 if ( memcmp(buffer1, buffer2,
n2) != 0 )
186 if (target) fclose(target);
187 if (tmp) fclose(tmp);
197 if ( !unlink(targetFileName.getStr()) )
198 if ( !rename(tmpFileName.getStr(), targetFileName.getStr()) )
207 if ( rename(tmpFileName.getStr(), targetFileName.getStr()) ) {
218 return unlink(fileName.getStr()) == 0;
223 if ( fileName.startsWith(
"file://") )
225 return OStringToOUString(fileName, osl_getThreadTextEncoding());
228 OUString uUrlFileName;
229 OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
230 if ( fileName.startsWith(
".") || fileName.indexOf(
SEPARATOR) < 0 )
232 OUString uWorkingDir;
233 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
237 if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
244 if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
265 osl_closeFile(m_file);
268bool FileStream::isValid()
const
270 return m_file !=
nullptr;
273void FileStream::createTempFile(
const OString& sPath)
279 if (!sPath.isEmpty())
284 if (osl_createTempFile(sTmpPath.pData, &m_file, &sTmpName.pData) == osl_File_E_None) {
286 sal_uInt64
const uAttr = osl_File_Attribute_OwnWrite |
287 osl_File_Attribute_OwnRead |
288 osl_File_Attribute_GrpWrite |
289 osl_File_Attribute_GrpRead |
290 osl_File_Attribute_OthRead;
291 if (osl_setFileAttributes(sTmpName.pData, uAttr) != osl_File_E_None) {
296 OUString sSysTmpName;
297 FileBase::getSystemPathFromFileURL(sTmpName, sSysTmpName);
303void FileStream::close()
307 osl_closeFile(m_file);
313bool FileStream::write(
void const * buffer, sal_uInt64 size) {
316 if (osl_writeFile(m_file, buffer, size, &written) != osl_File_E_None) {
319 OSL_ASSERT(written <= size);
321 buffer =
static_cast< char const *
>(buffer) + written;
327 sal_uInt64 writtenBytes;
328 OString s = OString::number(
static_cast<sal_Int32
>(
i));
329 osl_writeFile(o.m_file, s.getStr(), s.getLength() *
sizeof(
char), &writtenBytes);
333 sal_uInt64 writtenBytes;
334 osl_writeFile(o.m_file, s, strlen(s), &writtenBytes);
338 sal_uInt64 writtenBytes;
339 osl_writeFile(o.m_file, s->getStr(), s->getLength() *
sizeof(
char), &writtenBytes);
343 sal_uInt64 writtenBytes;
344 osl_writeFile(o.m_file, s.getStr(), s.getLength() *
sizeof(
char), &writtenBytes);
349 sal_uInt64 writtenBytes;
350 osl_writeFile(o.m_file, s->getStr(), s->getLength() *
sizeof(
char), &writtenBytes);
354 sal_uInt64 writtenBytes;
356 o.m_file, s.getStr(), s.getLength() *
sizeof(
char), &writtenBytes);
~CannotDumpException() noexcept
FileStream(const Filepath_char_t *filename)
FileStream & operator<<(FileStream &o, sal_uInt32 i)
static bool checkFileContent(const OString &targetFileName, const OString &tmpFileName)
OString getTempDir(const OString &sFileName)
bool removeTypeFile(const OString &fileName)
OString createFileNameFromType(const OString &destination, const OString &typeName, const OString &postfix)
OUString convertToFileUrl(const OString &fileName)
bool fileExists(const OString &fileName)
bool makeValidTypeFile(const OString &targetFileName, const OString &tmpFileName, bool bFileCheck)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)