10 #include <CommonCrypto/CommonDigest.h>
20 #include <mach-o/dyld.h>
43 const char FILE_SEPARATOR =
'/';
45 size_t separatorIndex = path.rfind(FILE_SEPARATOR);
46 string fileAndExtension;
47 if (separatorIndex != string::npos)
49 dir = path.substr(0, separatorIndex + 1);
50 if (separatorIndex < path.length())
51 fileAndExtension = path.substr(separatorIndex + 1, path.length());
55 fileAndExtension = path;
58 size_t dotIndex = fileAndExtension.rfind(
".");
59 if (dotIndex != string::npos)
61 file = fileAndExtension.substr(0, dotIndex);
62 extension = fileAndExtension.substr(dotIndex + 1, fileAndExtension.length());
66 file = fileAndExtension;
77 for (
int i = 0; i < path.length(); ++i)
79 while (i+1 < path.length() && path[i+1] ==
'/')
84 path.erase(path.length()-1);
97 return path1 == path2;
108 char *cwd = getcwd(NULL, 0);
111 VUserLog(
"Couldn't get current working directory: %s", strerror(errno));
115 string absolutePath = string(cwd) +
"/" + path;
129 if (directory.empty())
131 if (directory.at(directory.length()-1) !=
'/')
133 string suffix = (extension.empty() ?
"" : (
"." + extension));
134 string pathTemplate = directory + file +
"-XXXXXX" + suffix;
135 char *path = (
char *)calloc(pathTemplate.length() + 1,
sizeof(char));
136 strncpy(path, pathTemplate.c_str(), pathTemplate.length());
137 int fd = mkstemps(path, suffix.length());
139 string pathStr(path);
155 string pathTemplate =
getTmpDir() +
"/" + prefix +
".XXXXXX";
156 char *path = (
char *)calloc(pathTemplate.length() + 1,
sizeof(char));
157 strncpy(path, pathTemplate.c_str(), pathTemplate.length());
159 string pathStr(path);
190 char *cwd = getcwd(NULL, 0);
192 VUserLog(
"Error in getcwd(): %s", strerror(errno));
194 if (cwd && strstr(cwd,
"/Library/Containers/"))
206 char userTempDir[PATH_MAX];
207 if (confstr(_CS_DARWIN_USER_TEMP_DIR, userTempDir, PATH_MAX) > 0)
209 string userTempDirS(userTempDir);
226 throw VuoException(
"Couldn't create directory with empty path");
231 const char FILE_SEPARATOR =
'/';
232 size_t lastNonSeparator = path.find_last_not_of(FILE_SEPARATOR);
233 if (lastNonSeparator != string::npos)
234 path.resize(lastNonSeparator + 1);
236 string parentDir, file, ext;
242 int ret = mkdir(path.c_str(), 0777);
244 throw VuoException((
string(
"Couldn't create directory \"" + path +
"\": ") + strerror(errno)).c_str());
255 static string frameworkPath;
256 static dispatch_once_t once = 0;
257 dispatch_once(&once, ^{
260 const char *frameworkPathFragment =
"/Vuo.framework/Versions/";
261 uint32_t imageCount = _dyld_image_count();
262 for (uint32_t i = 0; i < imageCount; ++i)
264 const char *dylibPath = _dyld_get_image_name(i);
265 const char *found = strstr(dylibPath, frameworkPathFragment);
268 char *pathC = strndup(dylibPath, found - dylibPath);
269 string path = string(pathC) +
"/Vuo.framework";
273 frameworkPath = path;
280 char executablePath[PATH_MAX + 1];
281 uint32_t size =
sizeof(executablePath);
283 if (! _NSGetExecutablePath(executablePath, &size))
285 char cleanedExecutablePath[PATH_MAX + 1];
287 realpath(executablePath, cleanedExecutablePath);
288 string path = cleanedExecutablePath;
289 string dir, file, ext;
291 path = dir.substr(0, dir.length() - 1);
293 path = dir.substr(0, dir.length() - 1);
294 path +=
"/Frameworks/Vuo.framework";
298 frameworkPath = path;
306 return frameworkPath;
315 static string runnerFrameworkPath;
316 static dispatch_once_t once = 0;
317 dispatch_once(&once, ^{
322 runnerFrameworkPath = possibleFrameworkPath;
327 const char *frameworkPathFragment =
"/VuoRunner.framework/Versions/";
328 uint32_t imageCount = _dyld_image_count();
329 for (uint32_t i = 0; i < imageCount; ++i)
331 const char *dylibPath = _dyld_get_image_name(i);
332 const char *found = strstr(dylibPath, frameworkPathFragment);
335 char *pathC = strndup(dylibPath, found - dylibPath);
336 string path = string(pathC) +
"/VuoRunner.framework";
340 runnerFrameworkPath = path;
347 char executablePath[PATH_MAX + 1];
348 uint32_t size =
sizeof(executablePath);
350 if (! _NSGetExecutablePath(executablePath, &size))
352 char cleanedExecutablePath[PATH_MAX + 1];
354 realpath(executablePath, cleanedExecutablePath);
355 string path = cleanedExecutablePath;
356 string dir, file, ext;
358 path = dir.substr(0, dir.length() - 1);
360 path = dir.substr(0, dir.length() - 1);
361 path +=
"/Frameworks/VuoRunner.framework";
365 runnerFrameworkPath = path;
372 return runnerFrameworkPath;
385 string compositionDir, compositionFile, ext;
389 string parentDir, compositionDirName;
390 splitPath(compositionDir, parentDir, compositionDirName, ext);
393 string compositionBaseDir = (compositionDirName ==
"Modules" ? parentDir : compositionDir);
395 string compositionModulesDir = compositionBaseDir +
"/Modules";
398 return compositionModulesDir;
406 return string(getenv(
"HOME")) +
"/Library/Application Support/Vuo/Modules";
414 return "/Library/Application Support/Vuo/Modules";
422 return string(getenv(
"HOME")) +
"/Library/Caches/org.vuo/" + VUO_VERSION_AND_BUILD_STRING;
435 string dir, file, ext;
460 string bomUtf8 =
"\xEF\xBB\xBF";
461 string bomUtf16Be =
"\xFE\xFF";
462 string bomUtf16Le =
"\xFF\xFE";
463 string boms[] = { bomUtf8, bomUtf16Be, bomUtf16Le };
464 for (
int i = 0; i < 3; ++i)
467 for (
int j = 0; j < boms[i].length(); ++j)
468 if (boms[i][j] != s[j])
472 return boms[i].length();
486 while (getline(cin, line))
501 ifstream in(path.c_str(), ios::in | ios::binary);
503 throw VuoException(
string(
"Couldn't read file: ") + strerror(errno) +
" — " + path);
506 in.seekg(0, ios::end);
507 contents.resize(in.tellg());
508 in.seekg(0, ios::beg);
509 in.read(&contents[0], contents.size());
523 FILE *f = fopen(file.c_str(),
"wb");
525 throw VuoException(
string(
"Couldn't open file for writing: ") + strerror(errno) +
" — " + file);
527 size_t numBytesWritten = fwrite(data,
sizeof(
char), numBytes, f);
528 if (numBytesWritten != numBytes)
531 throw VuoException(
string(
"Couldn't write all data: ") + strerror(errno) +
" — " + file);
559 string dir, file, ext;
561 string tmpPath =
makeTmpFile(
"." + file, ext, dir);
579 return access(path.c_str(), 0) == 0;
594 int status = stat(path.c_str(), &st_buf);
595 return (! status && S_ISDIR(st_buf.st_mode));
604 int status = lstat(path.c_str(), &st_buf);
605 return (! status && S_ISLNK(st_buf.st_mode));
619 return access(path.c_str(), R_OK) == 0;
633 FILE *f = fopen(path.c_str(),
"rb");
637 fseek(f, 0, SEEK_END);
646 fseek(f, 0, SEEK_SET);
648 size_t ret = fread(&z, 1, 1, f);
658 FILE *f = fopen(path.c_str(),
"a");
667 int ret = remove(path.c_str());
669 VUserLog(
"Couldn't delete file: %s — %s", strerror(errno), path.c_str());
680 DIR *d = opendir(path.c_str());
683 VUserLog(
"Couldn't read directory: %s — %s", strerror(errno), path.c_str());
688 while( (de=readdir(d)) )
690 string fileName = de->d_name;
691 string filePath = path +
"/" + fileName;
693 if (fileName ==
"." || fileName ==
"..")
714 int ret = rename(fromPath.c_str(), toPath.c_str());
716 throw VuoException(
string(
"Couldn't move file: ") + strerror(errno) +
" — " + toPath);
742 int i = open(fromPath.c_str(), O_RDONLY);
744 throw VuoException(
string(
"Couldn't open copy source: ") + strerror(errno) +
" — " + fromPath);
748 int o = open(toPath.c_str(), O_WRONLY | O_CREAT, s.st_mode & 0777);
752 throw VuoException(
string(
"Couldn't open copy destination: ") + strerror(errno) +
" — " + toPath);
755 int ret = fcopyfile(i, o, NULL, COPYFILE_DATA | (preserveMetadata ? COPYFILE_STAT : 0));
756 char *e = strerror(errno);
761 throw VuoException(
string(
"Couldn't copy ") + fromPath +
" to " + toPath +
": " + e);
763 VDebugLog(
"%s -> %s", fromPath.c_str(), toPath.c_str());
780 char linkDestination[PATH_MAX + 1];
781 ssize_t len = readlink(fromPath.c_str(), linkDestination, PATH_MAX);
782 linkDestination[len] = 0;
783 if (symlink(linkDestination, toPath.c_str()) == -1)
784 throw VuoException(
string(
"Couldn't copy symlink \"") + fromPath +
"\" to \"" + toPath +
"\": " + strerror(errno));
786 VDebugLog(
"%s -> %s (symlink)", fromPath.c_str(), toPath.c_str());
797 for (
auto file : files)
799 string sourceFile = fromPath +
"/" + file->getRelativePath();
800 string targetFile = toPath +
"/" + file->getRelativePath();
813 int fd = open(path.c_str(), O_RDONLY);
815 throw VuoException(
"Error: Couldn't open \"" + path +
"\": " + strerror(errno));
819 if (fstat(fd, &stat) != 0)
820 throw VuoException(
"Error: Couldn't fstat \"" + path +
"\": " + strerror(errno));
824 void *data = mmap(
nullptr, stat.st_size, PROT_READ, MAP_PRIVATE | MAP_NOCACHE, fd, 0);
825 if (data == MAP_FAILED)
826 throw VuoException(
"Error: Couldn't mmap \"" + path +
"\": " + strerror(errno));
827 VuoDefer(^{ munmap(data, stat.st_size); });
829 unsigned char hash[CC_SHA256_DIGEST_LENGTH];
830 if (!CC_SHA256(data, stat.st_size, hash))
831 throw VuoException(
"Error: CC_SHA256 failed on file \"" + path +
"\"");
834 oss << setfill(
'0') << hex;
835 for (
int i = 0; i < CC_SHA256_DIGEST_LENGTH; ++i)
836 oss << setw(2) << (int)hash[i];
847 lstat(path.c_str(), &s);
848 return s.st_mtimespec.tv_sec;
857 lstat(path.c_str(), &s);
860 gettimeofday(&t, NULL);
862 return t.tv_sec - s.st_atimespec.tv_sec;
880 bool shouldSearchRecursively)
888 DIR *d = opendir(dirPath.c_str());
891 if (access(dirPath.c_str(), F_OK) != -1)
892 throw VuoException(
string(
"Couldn't read directory: ") + strerror(errno) +
" — " + dirPath);
897 while( (de=readdir(d)) )
899 string fileName = de->d_name;
900 string relativeFilePath = dirPath +
"/" + fileName;
902 if (fileName ==
"." || fileName ==
"..")
905 bool isArchive =
false;
906 for (set<string>::iterator archiveExtension = archiveExtensions.begin(); archiveExtension != archiveExtensions.end(); ++archiveExtension)
916 files.insert(fs.begin(), fs.end());
921 bool shouldSearchDir = shouldSearchRecursively
927 for (set<File *>::iterator i = filesInDir.begin(); i != filesInDir.end(); ++i)
930 f->dirPath = dirPath;
931 f->filePath = fileName +
"/" + f->filePath;
933 files.insert(filesInDir.begin(), filesInDir.end());
937 File *f =
new File(dirPath, fileName);
960 set<File *> matchingFiles;
962 for (set<File *>::iterator i = allFiles.begin(); i != allFiles.end(); ++i)
964 bool endsWithExtension =
false;
965 for (set<string>::iterator extension = extensions.begin(); extension != extensions.end(); ++extension)
967 endsWithExtension =
true;
970 matchingFiles.insert(*i);
975 return matchingFiles;
995 mz_uint numFiles = mz_zip_reader_get_num_files(archive->
zipArchive);
996 for (mz_uint i = 0; i < numFiles; ++i)
998 mz_zip_archive_file_stat file_stat;
999 bool success = mz_zip_reader_file_stat(archive->
zipArchive, i, &file_stat);
1002 VUserLog(
"Error: Couldn't read file '%u' in zip archive '%s'.", i, archive->
path.c_str());
1006 File *f =
new File(archive, file_stat.m_filename);
1028 set<File *> matchingFiles;
1030 for (set<File *>::iterator i = allFiles.begin(); i != allFiles.end(); ++i)
1034 string dir, file, ext;
1038 bool endsWithExtension =
false;
1039 for (set<string>::iterator extension = extensions.begin(); extension != extensions.end(); ++extension)
1041 endsWithExtension =
true;
1043 if (beginsWithDir && endsWithExtension)
1044 matchingFiles.insert(f);
1049 return matchingFiles;
1065 for (set<File *>::iterator i = archiveFiles.begin(); i != archiveFiles.end(); ++i)
1099 this->
zipArchive = (mz_zip_archive *)calloc(1,
sizeof(mz_zip_archive));
1101 bool success = mz_zip_reader_init_file(
zipArchive,
path.c_str(), 0);
1116 mz_zip_reader_end(zipArchive);
1137 this->fileDescriptor = -1;
1138 this->archive = NULL;
1150 this->fileDescriptor = -1;
1151 this->archive = archive;
1161 string newFilePath = basename() +
"." + extension;
1164 return File(archive, newFilePath);
1166 return File(dirPath, newFilePath);
1175 if (archive && --archive->referenceCount == 0)
1184 return (archive != NULL);
1194 return (archive ? archive->path :
"");
1213 throw VuoException(
"Can't return a simple absolute path for a file in an archive.");
1215 return dirPath +
"/" + filePath;
1226 throw VuoException(
"Can't return a simple absolute path for a file in an archive.");
1236 return filePath.substr(0, filePath.find_last_of(
'.'));
1244 return filePath.substr(filePath.find_last_of(
'.') + 1);
1253 return mz_zip_reader_locate_file(archive->zipArchive, filePath.c_str(),
nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE) != -1;
1275 string fullPath = (dirPath.empty() ?
"" : (dirPath +
"/")) + filePath;
1277 FILE *pFile = fopen ( fullPath.c_str() ,
"rb" );
1279 throw VuoException(
string(
"Couldn't open file: ") + strerror(errno) +
" — " + fullPath);
1282 fseek (pFile , 0 , SEEK_END);
1283 size_t lSize = ftell (pFile);
1287 buffer = (
char*) malloc (
sizeof(
char)*lSize);
1290 numBytes = fread (buffer,1,lSize,pFile);
1292 if (numBytes != lSize)
1295 throw VuoException(
string(
"Couldn't read file: ") + strerror(errno) +
" — " + fullPath);
1300 buffer = (
char *)mz_zip_reader_extract_file_to_heap(archive->zipArchive, filePath.c_str(), &numBytes, 0);
1314 char *buffer = getContentsAsRawData(numBytes);
1315 string s(buffer, numBytes);
1331 if (fileDescriptor < 0)
1332 fileDescriptor = open((dirPath +
"/" + filePath).c_str(), O_RDONLY);
1333 int ret = flock(fileDescriptor, LOCK_SH | (nonBlocking ? LOCK_NB : 0));
1348 if (fileDescriptor < 0)
1349 fileDescriptor = open((dirPath +
"/" + filePath).c_str(), O_RDONLY);
1350 int ret = flock(fileDescriptor, LOCK_EX | (nonBlocking ? LOCK_NB : 0));
1360 flock(fileDescriptor, LOCK_UN);
1361 close(fileDescriptor);
1362 fileDescriptor = -1;
1385 string binaryDir, binaryFile, binaryExt;
1386 splitPath(processAndArgs[0], binaryDir, binaryFile, binaryExt);
1388 string errorPrefix =
"Couldn't execute " + binaryFile +
": ";
1392 int ret = pipe(outputPipe);
1394 throw VuoException(errorPrefix +
"couldn't open pipe: " + strerror(ret));
1395 int pipeRead = outputPipe[0];
1396 int pipeWrite = outputPipe[1];
1398 posix_spawn_file_actions_t fileActions;
1399 posix_spawn_file_actions_init(&fileActions);
1400 posix_spawn_file_actions_adddup2(&fileActions, pipeWrite, STDOUT_FILENO);
1401 posix_spawn_file_actions_adddup2(&fileActions, pipeWrite, STDERR_FILENO);
1402 posix_spawn_file_actions_addclose(&fileActions, pipeRead);
1406 char **processAndArgsZ = (
char **)malloc(
sizeof(
char *) * (processAndArgs.size() + 1));
1409 for (
auto arg : processAndArgs)
1411 processAndArgsZ[argCount++] = strdup(arg.c_str());
1414 commandLine +=
'"' + arg +
'"';
1416 processAndArgsZ[argCount] =
nullptr;
1421 char **environmentZ = (
char **)malloc(
sizeof(
char *) * (environment.size() + 1));
1422 int environmentVarCount = 0;
1423 for (
auto environmentVar : environment)
1425 environmentZ[environmentVarCount++] = strdup(environmentVar.c_str());
1426 commandLine = environmentVar +
" " + commandLine;
1428 environmentZ[environmentVarCount] =
nullptr;
1434 ret = posix_spawn(&pid, processAndArgs[0].c_str(), &fileActions, NULL, (
char *
const *)processAndArgsZ, (
char *
const *)environmentZ);
1436 posix_spawn_file_actions_destroy(&fileActions);
1437 for (
int i = 0; i < argCount; ++i)
1438 free(processAndArgsZ[i]);
1448 ret = waitpid(pid, &status, 0);
1449 if (ret == -1 && errno == EINTR)
1454 throw VuoException(errorPrefix +
"waitpid failed: " + strerror(errno));
1455 else if (status != 0)
1460 while (read(pipeRead, &buf, 255) > 0)
1480 return extension ==
"vuo";
1506 return extensions.find(extension) != extensions.end();
1517 e.insert(
"vuoshader");
1518 e.insert(
"vuoobjectfilter");
1519 return e.find(extension) != e.end();
1527 return string() +
"the cache of " + (generated ?
"generated" :
"installed") +
" modules at '" + moduleCachePath +
"'";
1535 string moduleCachePathCanonical = moduleCachePath;
1538 return moduleCachePathCanonical +
"/libVuoModuleCache-" + (generated ?
"generated" :
"installed") +
".txt";
1548 string moduleCachePathCanonical = moduleCachePath;
1551 string partialPath = moduleCachePathCanonical +
"/libVuoModuleCache-" + (generated ?
"generated" :
"installed");
1553 return partialPath +
".dylib";
1566 unsigned long &lastModified)
1584 string dir, file, ext;
1586 string fileName = file +
"." + ext;
1588 vector< pair<string, unsigned long> > existingRevisions;
1594 delete existingFile;
1597 if (existingRevisions.empty())
1603 auto latest = std::max_element(existingRevisions.begin(), existingRevisions.end(),
1604 [](pair<string, unsigned long> p1, pair<string, unsigned long> p2) { return p1.second < p2.second; });
1606 lastModified = latest->second;
1607 return latest->first;
1615 string moduleCachePath, file, ext;
1616 splitPath(dylibPath, moduleCachePath, file, ext);
1617 string dylibFileName = file +
"." + ext;
1633 string dir1, dir2, file1, file2, ext;
1634 splitPath(dylibPath1, dir1, file1, ext);
1635 splitPath(dylibPath2, dir2, file2, ext);
1642 if (parts1.size() == 3 && parts2.size() == 3 && parts1.back() != parts2.back())
1646 return parts1 == parts2;