Skip to content

Commit

Permalink
Fix "too many open files" on MacOS
Browse files Browse the repository at this point in the history
Patch borrowed from sass/libsass#3183 by @tom-un

See See gohugoio/hugo#12649
  • Loading branch information
bep committed Aug 28, 2024
1 parent e882218 commit 62366be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"*.gotmpl": "html",
"string": "cpp"
}
}
15 changes: 15 additions & 0 deletions libsass_src/src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#else
# include <unistd.h>
# include <fcntl.h>
#endif
#include <cstdio>
#include <vector>
Expand Down Expand Up @@ -469,6 +470,20 @@ namespace Sass {
CloseHandle(hFile);
// just convert from unsigned char*
char* contents = (char*) pBuffer;
#elif __APPLE__
// On OSX `fopen` can fail with "too many open files" but succeeds using `open`.
struct stat st;
if (stat(path.c_str(), &st) == -1 || S_ISDIR(st.st_mode)) return 0;
int file = open(path.c_str(), O_RDONLY);
char* contents = 0;
if (file != -1) {
size_t size = st.st_size;
contents = (char*) malloc((size+2)*sizeof(char));
read(file, contents, size);
contents[size+0] = '\0';
contents[size+1] = '\0';
close(file);
}
#else
// Read the file using `<cstdio>` instead of `<fstream>` for better portability.
// The `<fstream>` header initializes `<locale>` and this buggy in GCC4/5 with static linking.
Expand Down

0 comments on commit 62366be

Please sign in to comment.