16 #include <mach-o/dyld.h>
39 const char FILE_SEPARATOR =
'/';
41 size_t separatorIndex = path.rfind(FILE_SEPARATOR);
42 string fileAndExtension;
43 if (separatorIndex != string::npos)
45 dir = path.substr(0, separatorIndex + 1);
46 if (separatorIndex < path.length())
47 fileAndExtension = path.substr(separatorIndex + 1, path.length());
51 fileAndExtension = path;
54 size_t dotIndex = fileAndExtension.rfind(
".");
55 if (dotIndex != string::npos)
57 file = fileAndExtension.substr(0, dotIndex);
58 extension = fileAndExtension.substr(dotIndex + 1, fileAndExtension.length());
62 file = fileAndExtension;
73 for (
int i = 0; i < path.length(); ++i)
75 while (i+1 < path.length() && path[i+1] ==
'/')
80 path.erase(path.length()-1);
93 return path1 == path2;
104 char *cwd = getcwd(NULL, 0);
107 VUserLog(
"Couldn't get current working directory: %s", strerror(errno));
111 string absolutePath = string(cwd) +
"/" + path;
125 if (directory.empty())
127 if (directory.at(directory.length()-1) !=
'/')
129 string suffix = (extension.empty() ?
"" : (
"." + extension));
130 string pathTemplate = directory + file +
"-XXXXXX" + suffix;
131 char *path = (
char *)calloc(pathTemplate.length() + 1,
sizeof(char));
132 strncpy(path, pathTemplate.c_str(), pathTemplate.length());
133 int fd = mkstemps(path, suffix.length());
135 string pathStr(path);
151 string pathTemplate =
getTmpDir() +
"/" + prefix +
".XXXXXX";
152 char *path = (
char *)calloc(pathTemplate.length() + 1,
sizeof(char));
153 strncpy(path, pathTemplate.c_str(), pathTemplate.length());
155 string pathStr(path);
186 char *cwd = getcwd(NULL, 0);
187 if (strstr(cwd,
"/Library/Containers/"))
199 char userTempDir[PATH_MAX];
200 if (confstr(_CS_DARWIN_USER_TEMP_DIR, userTempDir, PATH_MAX) > 0)
202 string userTempDirS(userTempDir);
219 throw VuoException(
"Couldn't create directory with empty path");
224 const char FILE_SEPARATOR =
'/';
225 size_t lastNonSeparator = path.find_last_not_of(FILE_SEPARATOR);
226 if (lastNonSeparator != string::npos)
227 path.resize(lastNonSeparator + 1);
229 string parentDir, file, ext;
235 int ret = mkdir(path.c_str(), 0777);
237 throw VuoException((
string(
"Couldn't create directory \"" + path +
"\": ") + strerror(errno)).c_str());
248 static string frameworkPath;
249 static dispatch_once_t once = 0;
250 dispatch_once(&once, ^{
253 const char *frameworkPathFragment =
"/Vuo.framework/Versions/";
254 uint32_t imageCount = _dyld_image_count();
255 for (uint32_t i = 0; i < imageCount; ++i)
257 const char *dylibPath = _dyld_get_image_name(i);
258 const char *found = strstr(dylibPath, frameworkPathFragment);
261 char *pathC = strndup(dylibPath, found - dylibPath);
262 string path = string(pathC) +
"/Vuo.framework";
266 frameworkPath = path;
273 char executablePath[PATH_MAX + 1];
274 uint32_t size =
sizeof(executablePath);
276 if (! _NSGetExecutablePath(executablePath, &size))
278 char cleanedExecutablePath[PATH_MAX + 1];
280 realpath(executablePath, cleanedExecutablePath);
281 string path = cleanedExecutablePath;
282 string dir, file, ext;
284 path = dir.substr(0, dir.length() - 1);
286 path = dir.substr(0, dir.length() - 1);
287 path +=
"/Frameworks/Vuo.framework";
291 frameworkPath = path;
299 return frameworkPath;
308 static string runnerFrameworkPath;
309 static dispatch_once_t once = 0;
310 dispatch_once(&once, ^{
315 runnerFrameworkPath = possibleFrameworkPath;
320 const char *frameworkPathFragment =
"/VuoRunner.framework/Versions/";
321 uint32_t imageCount = _dyld_image_count();
322 for (uint32_t i = 0; i < imageCount; ++i)
324 const char *dylibPath = _dyld_get_image_name(i);
325 const char *found = strstr(dylibPath, frameworkPathFragment);
328 char *pathC = strndup(dylibPath, found - dylibPath);
329 string path = string(pathC) +
"/VuoRunner.framework";
333 runnerFrameworkPath = path;
340 char executablePath[PATH_MAX + 1];
341 uint32_t size =
sizeof(executablePath);
343 if (! _NSGetExecutablePath(executablePath, &size))
345 char cleanedExecutablePath[PATH_MAX + 1];
347 realpath(executablePath, cleanedExecutablePath);
348 string path = cleanedExecutablePath;
349 string dir, file, ext;
351 path = dir.substr(0, dir.length() - 1);
353 path = dir.substr(0, dir.length() - 1);
354 path +=
"/Frameworks/VuoRunner.framework";
358 runnerFrameworkPath = path;
365 return runnerFrameworkPath;
378 string compositionDir, compositionFile, ext;
382 string parentDir, compositionDirName;
383 splitPath(compositionDir, parentDir, compositionDirName, ext);
386 string compositionBaseDir = (compositionDirName ==
"Modules" ? parentDir : compositionDir);
388 string compositionModulesDir = compositionBaseDir +
"/Modules";
391 return compositionModulesDir;
399 return string(getenv(
"HOME")) +
"/Library/Application Support/Vuo/Modules";
407 return "/Library/Application Support/Vuo/Modules";
415 return string(getenv(
"HOME")) +
"/Library/Caches/org.vuo/" + VUO_VERSION_AND_BUILD_STRING;
428 string dir, file, ext;
453 string bomUtf8 =
"\xEF\xBB\xBF";
454 string bomUtf16Be =
"\xFE\xFF";
455 string bomUtf16Le =
"\xFF\xFE";
456 string boms[] = { bomUtf8, bomUtf16Be, bomUtf16Le };
457 for (
int i = 0; i < 3; ++i)
460 for (
int j = 0; j < boms[i].length(); ++j)
461 if (boms[i][j] != s[j])
465 return boms[i].length();
479 while (getline(cin, line))
494 ifstream in(path.c_str(), ios::in | ios::binary);
496 throw VuoException(
string(
"Couldn't read file: ") + strerror(errno) +
" — " + path);
499 in.seekg(0, ios::end);
500 contents.resize(in.tellg());
501 in.seekg(0, ios::beg);
502 in.read(&contents[0], contents.size());
516 FILE *f = fopen(file.c_str(),
"wb");
518 throw VuoException(
string(
"Couldn't open file for writing: ") + strerror(errno) +
" — " + file);
520 size_t numBytesWritten = fwrite(data,
sizeof(
char), numBytes, f);
521 if (numBytesWritten != numBytes)
524 throw VuoException(
string(
"Couldn't write all data: ") + strerror(errno) +
" — " + file);
552 string dir, file, ext;
554 string tmpPath =
makeTmpFile(
"." + file, ext, dir);
572 return access(path.c_str(), 0) == 0;
587 int status = stat(path.c_str(), &st_buf);
588 return (! status && S_ISDIR(st_buf.st_mode));
597 int status = lstat(path.c_str(), &st_buf);
598 return (! status && S_ISLNK(st_buf.st_mode));
612 return access(path.c_str(), R_OK) == 0;
626 FILE *f = fopen(path.c_str(),
"rb");
630 fseek(f, 0, SEEK_END);
639 fseek(f, 0, SEEK_SET);
641 size_t ret = fread(&z, 1, 1, f);
651 FILE *f = fopen(path.c_str(),
"a");
660 int ret = remove(path.c_str());
662 VUserLog(
"Couldn't delete file: %s — %s", strerror(errno), path.c_str());
673 DIR *d = opendir(path.c_str());
676 VUserLog(
"Couldn't read directory: %s — %s", strerror(errno), path.c_str());
681 while( (de=readdir(d)) )
683 string fileName = de->d_name;
684 string filePath = path +
"/" + fileName;
686 if (fileName ==
"." || fileName ==
"..")
707 int ret = rename(fromPath.c_str(), toPath.c_str());
709 throw VuoException(
string(
"Couldn't move file: ") + strerror(errno) +
" — " + toPath);
735 int i = open(fromPath.c_str(), O_RDONLY);
737 throw VuoException(
string(
"Couldn't open copy source: ") + strerror(errno) +
" — " + fromPath);
741 int o = open(toPath.c_str(), O_WRONLY | O_CREAT, s.st_mode & 0777);
745 throw VuoException(
string(
"Couldn't open copy destination: ") + strerror(errno) +
" — " + toPath);
748 int ret = fcopyfile(i, o, NULL, COPYFILE_DATA | (preserveMetadata ? COPYFILE_STAT : 0));
749 char *e = strerror(errno);
754 throw VuoException(
string(
"Couldn't copy ") + fromPath +
" to " + toPath +
": " + e);
771 char linkDestination[PATH_MAX + 1];
772 ssize_t len = readlink(fromPath.c_str(), linkDestination, PATH_MAX);
773 linkDestination[len] = 0;
774 if (symlink(linkDestination, toPath.c_str()) == -1)
775 throw VuoException(
string(
"Couldn't copy symlink \"") + fromPath +
"\" to \"" + toPath +
"\": " + strerror(errno));
785 for (
auto file : files)
787 string sourceFile = fromPath +
"/" + file->getRelativePath();
788 string targetFile = toPath +
"/" + file->getRelativePath();
801 lstat(path.c_str(), &s);
802 return s.st_mtimespec.tv_sec;
811 lstat(path.c_str(), &s);
814 gettimeofday(&t, NULL);
816 return t.tv_sec - s.st_atimespec.tv_sec;
833 bool shouldSearchRecursively)
841 DIR *d = opendir(dirPath.c_str());
844 if (access(dirPath.c_str(), F_OK) != -1)
845 throw VuoException(
string(
"Couldn't read directory: ") + strerror(errno) +
" — " + dirPath);
850 while( (de=readdir(d)) )
852 string fileName = de->d_name;
853 string relativeFilePath = dirPath +
"/" + fileName;
855 if (fileName ==
"." || fileName ==
"..")
858 bool isArchive =
false;
859 for (set<string>::iterator archiveExtension = archiveExtensions.begin(); archiveExtension != archiveExtensions.end(); ++archiveExtension)
869 files.insert(fs.begin(), fs.end());
874 bool shouldSearchDir = shouldSearchRecursively
880 for (set<File *>::iterator i = filesInDir.begin(); i != filesInDir.end(); ++i)
883 f->dirPath = dirPath;
884 f->filePath = fileName +
"/" + f->filePath;
886 files.insert(filesInDir.begin(), filesInDir.end());
890 File *f =
new File(dirPath, fileName);
913 set<File *> matchingFiles;
915 for (set<File *>::iterator i = allFiles.begin(); i != allFiles.end(); ++i)
917 bool endsWithExtension =
false;
918 for (set<string>::iterator extension = extensions.begin(); extension != extensions.end(); ++extension)
920 endsWithExtension =
true;
923 matchingFiles.insert(*i);
928 return matchingFiles;
948 mz_uint numFiles = mz_zip_reader_get_num_files(archive->
zipArchive);
949 for (mz_uint i = 0; i < numFiles; ++i)
951 mz_zip_archive_file_stat file_stat;
952 bool success = mz_zip_reader_file_stat(archive->
zipArchive, i, &file_stat);
955 VUserLog(
"Error: Couldn't read file '%u' in zip archive '%s'.", i, archive->
path.c_str());
959 File *f =
new File(archive, file_stat.m_filename);
981 set<File *> matchingFiles;
983 for (set<File *>::iterator i = allFiles.begin(); i != allFiles.end(); ++i)
987 string dir, file, ext;
991 bool endsWithExtension =
false;
992 for (set<string>::iterator extension = extensions.begin(); extension != extensions.end(); ++extension)
994 endsWithExtension =
true;
996 if (beginsWithDir && endsWithExtension)
997 matchingFiles.insert(f);
1002 return matchingFiles;
1018 for (set<File *>::iterator i = archiveFiles.begin(); i != archiveFiles.end(); ++i)
1052 this->
zipArchive = (mz_zip_archive *)calloc(1,
sizeof(mz_zip_archive));
1054 bool success = mz_zip_reader_init_file(
zipArchive,
path.c_str(), 0);
1069 mz_zip_reader_end(zipArchive);
1090 this->fileDescriptor = -1;
1091 this->archive = NULL;
1103 this->fileDescriptor = -1;
1104 this->archive = archive;
1114 string newFilePath = basename() +
"." + extension;
1117 return File(archive, newFilePath);
1119 return File(dirPath, newFilePath);
1128 if (archive && --archive->referenceCount == 0)
1137 return (archive != NULL);
1147 return (archive ? archive->path :
"");
1166 throw VuoException(
"Can't return a simple absolute path for a file in an archive.");
1168 return dirPath +
"/" + filePath;
1179 throw VuoException(
"Can't return a simple absolute path for a file in an archive.");
1189 return filePath.substr(0, filePath.find_last_of(
'.'));
1197 return filePath.substr(filePath.find_last_of(
'.') + 1);
1206 return mz_zip_reader_locate_file(archive->zipArchive, filePath.c_str(),
nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE) != -1;
1228 string fullPath = (dirPath.empty() ?
"" : (dirPath +
"/")) + filePath;
1230 FILE *pFile = fopen ( fullPath.c_str() ,
"rb" );
1232 throw VuoException(
string(
"Couldn't open file: ") + strerror(errno) +
" — " + fullPath);
1235 fseek (pFile , 0 , SEEK_END);
1236 size_t lSize = ftell (pFile);
1240 buffer = (
char*) malloc (
sizeof(
char)*lSize);
1243 numBytes = fread (buffer,1,lSize,pFile);
1245 if (numBytes != lSize)
1248 throw VuoException(
string(
"Couldn't read file: ") + strerror(errno) +
" — " + fullPath);
1253 buffer = (
char *)mz_zip_reader_extract_file_to_heap(archive->zipArchive, filePath.c_str(), &numBytes, 0);
1267 char *buffer = getContentsAsRawData(numBytes);
1268 string s(buffer, numBytes);
1284 if (fileDescriptor < 0)
1285 fileDescriptor = open((dirPath +
"/" + filePath).c_str(), O_RDONLY);
1286 int ret = flock(fileDescriptor, LOCK_SH | (nonBlocking ? LOCK_NB : 0));
1301 if (fileDescriptor < 0)
1302 fileDescriptor = open((dirPath +
"/" + filePath).c_str(), O_RDONLY);
1303 int ret = flock(fileDescriptor, LOCK_EX | (nonBlocking ? LOCK_NB : 0));
1313 flock(fileDescriptor, LOCK_UN);
1314 close(fileDescriptor);
1315 fileDescriptor = -1;
1336 string binaryDir, binaryFile, binaryExt;
1337 splitPath(processAndArgs[0], binaryDir, binaryFile, binaryExt);
1339 string errorPrefix =
"Couldn't execute " + binaryFile +
": ";
1343 int ret = pipe(outputPipe);
1345 throw VuoException(errorPrefix +
"couldn't open pipe: " + strerror(ret));
1346 int pipeRead = outputPipe[0];
1347 int pipeWrite = outputPipe[1];
1349 posix_spawn_file_actions_t fileActions;
1350 posix_spawn_file_actions_init(&fileActions);
1351 posix_spawn_file_actions_adddup2(&fileActions, pipeWrite, STDOUT_FILENO);
1352 posix_spawn_file_actions_adddup2(&fileActions, pipeWrite, STDERR_FILENO);
1353 posix_spawn_file_actions_addclose(&fileActions, pipeRead);
1357 char **processAndArgsZ = (
char **)malloc(
sizeof(
char *) * (processAndArgs.size() + 1));
1359 for (
auto arg : processAndArgs)
1361 processAndArgsZ[argCount++] = strdup(binaryFile.c_str());
1363 processAndArgsZ[argCount++] = strdup(arg.c_str());
1364 processAndArgsZ[argCount] =
nullptr;
1368 ret = posix_spawn(&pid, processAndArgs[0].c_str(), &fileActions, NULL, (
char *
const *)processAndArgsZ, NULL);
1370 posix_spawn_file_actions_destroy(&fileActions);
1371 for (
int i = 0; i < argCount; ++i)
1372 free(processAndArgsZ[i]);
1379 if (waitpid(pid, &status, 0) == -1)
1380 throw VuoException(errorPrefix +
"waitpid failed: " + strerror(errno));
1381 else if (status != 0)
1386 while (read(pipeRead, &buf, 255) > 0)
1402 return extension ==
"vuo";
1428 return extensions.find(extension) != extensions.end();
1439 e.insert(
"vuoshader");
1440 e.insert(
"vuoobjectfilter");
1441 return e.find(extension) != e.end();