Skip to content

Commit 6ff89b8

Browse files
committed
Specify mode while creating file instead of later chmod in xsrc
1 parent cbb1bf8 commit 6ff89b8

File tree

7 files changed

+53
-19
lines changed

7 files changed

+53
-19
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ find_lib() {
5555
}
5656

5757
clean_dir() {
58-
cd "$@"
58+
cd "$@"
5959
[ -f ./Makefile ] && make clean
6060
[ -d ./obj ] && rm -rf ./obj
6161
[ -d ./build ] && rm -rf ./build

src/data/buf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ int XByteBuffer_AddByte(xbyte_buffer_t *pBuffer, uint8_t nByte)
256256
return (int)pBuffer->nUsed;
257257
}
258258

259+
uint8_t XByteBuffer_GetByte(xbyte_buffer_t *pBuffer, size_t nIndex)
260+
{
261+
if (nIndex >= pBuffer->nUsed) return 0;
262+
return pBuffer->pData[nIndex];
263+
}
264+
259265
int XByteBuffer_NullTerm(xbyte_buffer_t *pBuffer)
260266
{
261267
if (XByteBuffer_Reserve(pBuffer, 1) <= 0) return XSTDERR;

src/data/buf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef struct XByteBuffer {
3232
uint8_t *XByteData_Dup(const uint8_t *pBuff, size_t nSize);
3333
xbool_t XByteBuffer_HasData(xbyte_buffer_t *pBuffer);
3434
xbyte_buffer_t* XByteBuffer_New(size_t nSize, int nFastAlloc);
35+
uint8_t XByteBuffer_GetByte(xbyte_buffer_t *pBuffer, size_t nIndex);
3536
void XByteBuffer_Free(xbyte_buffer_t **pBuffer);
3637
void XByteBuffer_Clear(xbyte_buffer_t *pBuffer);
3738
void XByteBuffer_Reset(xbyte_buffer_t *pBuffer);

src/sys/xfs.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ int xstat(const char *pPath, xstat_t *pStat)
7575

7676
int XFile_ParseFlags(const char *pFlags)
7777
{
78+
if (pFlags == NULL) return 0;
79+
7880
size_t i, nLen = strnlen(pFlags, XFILE_FLAGS_LEN);
7981
int nFlags = 0;
8082

@@ -129,10 +131,30 @@ int XFile_ParseFlags(const char *pFlags)
129131
return nFlags;
130132
}
131133

134+
int XFile_OpenM(xfile_t *pFile, const char *pPath, const char *pFlags, xmode_t nMode)
135+
{
136+
if (pFile == NULL || pPath == NULL) return XSTDERR;
137+
pFile->nFlags = XFile_ParseFlags(pFlags);
138+
pFile->nBlockSize = XFILE_BUF_SIZE;
139+
pFile->nMode = nMode;
140+
pFile->bEOF = XFALSE;
141+
pFile->nSize = 0;
142+
pFile->nFD = -1;
143+
144+
#ifdef _WIN32
145+
_sopen_s(&pFile->nFD, pPath, pFile->nFlags, _SH_DENYNO, pFile->nMode);
146+
#else
147+
pFile->nFD = open(pPath, pFile->nFlags, pFile->nMode);
148+
#endif
149+
150+
pFile->nPosit = pFile->nAlloc = 0;
151+
return pFile->nFD;
152+
}
153+
132154
int XFile_Open(xfile_t *pFile, const char *pPath, const char *pFlags, const char *pPerms)
133155
{
134156
if (pFile == NULL || pPath == NULL) return XSTDERR;
135-
pFile->nFlags = (pFlags != NULL) ? XFile_ParseFlags(pFlags) : 0;
157+
pFile->nFlags = XFile_ParseFlags(pFlags);
136158
pFile->nBlockSize = XFILE_BUF_SIZE;
137159
pFile->bEOF = XFALSE;
138160
pFile->nSize = 0;

src/sys/xfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ int xunlink(const char* pPath);
8585
int xrmdir(const char* pPath);
8686
int xclose(int nFD);
8787

88+
int XFile_OpenM(xfile_t *pFile, const char *pPath, const char *pFlags, xmode_t nMode);
8889
int XFile_Open(xfile_t *pFile, const char *pPath, const char *pFlags, const char *pPerms);
8990
int XFile_Reopen(xfile_t *pFile, const char *pPath, const char *pFlags, const char *pPerms);
9091
xfile_t* XFile_Alloc(const char *pPath, const char *pFlags, const char *pPerms);

src/xver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define XUTILS_VERSION_MAX 2
1414
#define XUTILS_VERSION_MIN 7
15-
#define XUTILS_BUILD_NUMBER 20
15+
#define XUTILS_BUILD_NUMBER 21
1616
#define XUTILS_RELEASE_DATE "30Jan2025"
1717

1818
#ifdef __cplusplus

tools/xsrc.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
527527

528528
xfile_t file;
529529
xbool_t bDirty = XFALSE;
530+
530531
char sLine[XLINE_MAX];
532+
size_t nLinesProcessed = 0;
531533

532534
xbyte_buffer_t buffer;
533535
XByteBuffer_Init(&buffer, XLINE_MAX, XSTDNON);
@@ -541,7 +543,7 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
541543
{
542544
xbool_t bDropLine = XFALSE;
543545
xbool_t bModified = XFALSE;
544-
int nFirstSpace = XSTDERR;
546+
int nFirstSpace = -1;
545547
int nPosit = 0;
546548

547549
while (nPosit < nLength)
@@ -559,7 +561,8 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
559561
if (nFirstSpace >= 0 && nFirstSpace < nLength)
560562
{
561563
nNextLength = XFile_GetLine(&file, sNextLine, sizeof(sNextLine));
562-
bDropLine = !nFirstSpace && nNextLength <= 0 ? XTRUE : XFALSE;
564+
bDropLine = !nFirstSpace && nNextLength <= 0 && nLinesProcessed &&
565+
sLine[nLength - 1] != '\n' ? XTRUE : XFALSE;
563566

564567
if (nFirstSpace == 0 ||
565568
nNextLength <= 0 ||
@@ -579,31 +582,33 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
579582
XByteBuffer_Clear, &buffer, XFile_Close, &file, XSTDERR);
580583
}
581584

582-
if (pArgs->bFinalNewline && nNextLength <= 0 &&
583-
nLength > 0 && sLine[nLength - 1] != '\n')
584-
{
585-
XASSERT_CALL2((XByteBuffer_Add(&buffer, (const uint8_t*)"\n", 1) > 0),
586-
XByteBuffer_Clear, &buffer, XFile_Close, &file, XSTDERR);
587-
588-
bDirty = XTRUE;
589-
}
590-
591585
if (bModified)
592586
{
593-
xstrncpy(sLine, sizeof(sLine), sNextLine);
587+
if (nNextLength > 0)
588+
xstrncpy(sLine, sizeof(sLine), sNextLine);
589+
594590
nLength = nNextLength;
591+
nLinesProcessed++;
595592
continue;
596593
}
597594

598595
nLength = XFile_GetLine(&file, sLine, sizeof(sLine));
596+
nLinesProcessed++;
599597
}
600598

601599
// Finished reading
602600
XFile_Close(&file);
603601

604-
if (!bDirty)
602+
if (pArgs->bFinalNewline && (!buffer.nUsed || buffer.pData[buffer.nUsed - 1] != '\n'))
603+
{
604+
XASSERT_CALL((XByteBuffer_Add(&buffer, (const uint8_t*)"\n", 1) > 0),
605+
XByteBuffer_Clear, &buffer, XSTDERR);
606+
607+
bDirty = XTRUE;
608+
}
609+
610+
if (!bDirty || !buffer.nUsed)
605611
{
606-
// No need to change anything
607612
XByteBuffer_Clear(&buffer);
608613
return XSTDNON;
609614
}
@@ -616,7 +621,7 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
616621

617622
if (XPath_Exists(sEntryBackup))
618623
{
619-
if (XFile_Open(&file, sEntry, "cwt", NULL) < 0)
624+
if (XFile_OpenM(&file, sEntry, "cwt", pEntry->nMode) < 0)
620625
{
621626
xloge("Failed to open file: %s (%s)", sEntry, XSTRERR);
622627
rename(sEntryBackup, sEntry);
@@ -636,7 +641,6 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
636641

637642
XFile_Close(&file);
638643
unlink(sEntryBackup);
639-
chmod(sEntry, pEntry->nMode);
640644
}
641645

642646
XByteBuffer_Clear(&buffer);

0 commit comments

Comments
 (0)