Skip to content

Commit 8e14bc6

Browse files
committed
move filesize logic to path.cpp
1 parent ec4aacc commit 8e14bc6

7 files changed

Lines changed: 51 additions & 73 deletions

File tree

Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ LIBOBJ = $(libcppdir)/valueflow.o \
231231
$(libcppdir)/ctu.o \
232232
$(libcppdir)/errorlogger.o \
233233
$(libcppdir)/errortypes.o \
234-
$(libcppdir)/filesettings.o \
235234
$(libcppdir)/forwardanalyzer.o \
236235
$(libcppdir)/fwdanalysis.o \
237236
$(libcppdir)/importproject.o \
@@ -582,9 +581,6 @@ $(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h li
582581
$(libcppdir)/errortypes.o: lib/errortypes.cpp lib/config.h lib/errortypes.h lib/utils.h
583582
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp
584583

585-
$(libcppdir)/filesettings.o: lib/filesettings.cpp lib/config.h lib/filesettings.h lib/path.h lib/platform.h lib/standards.h
586-
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/filesettings.cpp
587-
588584
$(libcppdir)/forwardanalyzer.o: lib/forwardanalyzer.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/forwardanalyzer.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueptr.h lib/vfvalue.h
589585
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp
590586

lib/cppcheck.vcxproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
<ClCompile Include="ctu.cpp" />
6767
<ClCompile Include="errorlogger.cpp" />
6868
<ClCompile Include="errortypes.cpp" />
69-
<ClCompile Include="filesettings.cpp" />
7069
<ClCompile Include="forwardanalyzer.cpp" />
7170
<ClCompile Include="fwdanalysis.cpp" />
7271
<ClCompile Include="importproject.cpp" />
@@ -140,7 +139,6 @@
140139
<ClInclude Include="errorlogger.h" />
141140
<ClInclude Include="errortypes.h" />
142141
<ClInclude Include="filesettings.h" />
143-
<ClInclude Include="filesettings.h" />
144142
<ClInclude Include="findtoken.h" />
145143
<ClInclude Include="forwardanalyzer.h" />
146144
<ClInclude Include="fwdanalysis.h" />

lib/filesettings.cpp

Lines changed: 0 additions & 62 deletions
This file was deleted.

lib/filesettings.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ class FileWithDetails
6060
return mSize;
6161
}
6262

63-
std::string updateSize();
63+
std::string updateSize()
64+
{
65+
ssize_t ssize = Path::fileSize(mPath);
66+
if (ssize < 0)
67+
return "could not stat file '" + mPath + "': (errno: " + std::to_string(errno) + ")";
68+
mSize = ssize;
69+
return "";
70+
}
6471

6572
private:
6673
std::string mPath;

lib/path.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,39 @@ std::string Path::join(const std::string& path1, const std::string& path2) {
435435
return path2;
436436
return ((path1.back() == '/') ? path1 : (path1 + "/")) + path2;
437437
}
438+
439+
#ifdef _WIN32
440+
441+
# ifdef _WIN64
442+
443+
ssize_t Path::fileSize(const std::string &filePath) {
444+
struct _stati64 buf;
445+
if (_stati64(filePath.c_str(), &buf) < 0) {
446+
return -1;
447+
}
448+
return buf.st_size;
449+
}
450+
451+
# else
452+
453+
ssize_t Path::fileSize(const std::string &filePath) {
454+
struct _stat buf;
455+
if (_stat(filePath.c_str(), &buf) < 0) {
456+
return -1;
457+
}
458+
return buf.st_size;
459+
}
460+
461+
# endif
462+
463+
#else
464+
465+
ssize_t Path::fileSize(const std::string &filePath) {
466+
struct stat buf;
467+
if (stat(filePath.c_str(), &buf) < 0) {
468+
return -1;
469+
}
470+
return buf.st_size;
471+
}
472+
473+
#endif

lib/path.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ class CPPCHECKLIB Path {
201201
* join 2 paths with '/' separators
202202
*/
203203
static std::string join(const std::string& path1, const std::string& path2);
204+
205+
/**
206+
* @brief Get the size of a file
207+
* @param filePath path to the file, or -1 if the file cannot be accessed
208+
* @return size of file
209+
*/
210+
static ssize_t fileSize(const std::string &filePath);
204211
};
205212

206213
/// @}

oss-fuzz/Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ LIBOBJ = $(libcppdir)/valueflow.o \
7474
$(libcppdir)/ctu.o \
7575
$(libcppdir)/errorlogger.o \
7676
$(libcppdir)/errortypes.o \
77-
$(libcppdir)/filesettings.o \
7877
$(libcppdir)/forwardanalyzer.o \
7978
$(libcppdir)/fwdanalysis.o \
8079
$(libcppdir)/importproject.o \
@@ -268,9 +267,6 @@ $(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml
268267
$(libcppdir)/errortypes.o: ../lib/errortypes.cpp ../lib/config.h ../lib/errortypes.h ../lib/utils.h
269268
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp
270269

271-
$(libcppdir)/filesettings.o: ../lib/filesettings.cpp ../lib/config.h ../lib/filesettings.h ../lib/path.h ../lib/platform.h ../lib/standards.h
272-
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/filesettings.cpp
273-
274270
$(libcppdir)/forwardanalyzer.o: ../lib/forwardanalyzer.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/color.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/forwardanalyzer.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/settings.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/suppressions.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/valueptr.h ../lib/vfvalue.h
275271
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp
276272

0 commit comments

Comments
 (0)