Skip to content

Commit edea4db

Browse files
committedAug 28, 2024
Fix "too many open files" on MacOS
Patch borrowed from sass/libsass#3183 by @tom-un See See gohugoio/hugo#12649
1 parent ba6510a commit edea4db

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
 

‎libsass_src/src/file.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1313
#else
1414
# include <unistd.h>
15+
# include <fcntl.h>
1516
#endif
1617
#include <cstdio>
1718
#include <vector>
@@ -469,6 +470,20 @@ namespace Sass {
469470
CloseHandle(hFile);
470471
// just convert from unsigned char*
471472
char* contents = (char*) pBuffer;
473+
#elif __APPLE__
474+
// On OSX `fopen` can fail with "too many open files" but succeeds using `open`.
475+
struct stat st;
476+
if (stat(path.c_str(), &st) == -1 || S_ISDIR(st.st_mode)) return 0;
477+
int file = open(path.c_str(), O_RDONLY);
478+
char* contents = 0;
479+
if (file != -1) {
480+
size_t size = st.st_size;
481+
contents = (char*) malloc((size+2)*sizeof(char));
482+
read(file, contents, size);
483+
contents[size+0] = '\0';
484+
contents[size+1] = '\0';
485+
close(file);
486+
}
472487
#else
473488
// Read the file using `<cstdio>` instead of `<fstream>` for better portability.
474489
// The `<fstream>` header initializes `<locale>` and this buggy in GCC4/5 with static linking.

0 commit comments

Comments
 (0)