Skip to content

Commit 62366be

Browse files
committed
Fix "too many open files" on MacOS
Patch borrowed from sass/libsass#3183 by @tom-un See See gohugoio/hugo#12649
1 parent e882218 commit 62366be

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"*.gotmpl": "html",
4+
"string": "cpp"
5+
}
6+
}

libsass_src/src/file.cpp

Lines changed: 15 additions & 0 deletions
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)