FILE* FindFile( const vector<string>& includePath, const string& prependDir, const string& path, char openDelim, string& fullPath) { // If the opening delimiter was '"', then check the current // directory first: if (openDelim == '"') { if (prependDir.size()) { GetFileFullPath(prependDir, path, fullPath); } else { fullPath = path; } FILE* fp = fopen(fullPath.c_str(), "rb"); if (fp) { return fp; } } // Search the include path for the file: vector<string>::const_iterator first = includePath.begin(); vector<string>::const_iterator last = includePath.end(); for (; first != last; first++) { fullPath = *first; fullPath += '/'; fullPath += path; FILE* fp = fopen(fullPath.c_str(), "rb"); if (fp) { return fp; } } return NULL; }
/** * This function installs a package by calling ExtractPackage to extract it and InsertPkgDB to insert the package in PACKAGES and FILESPKG tables * @param name A package name * @return If no error ocurr, 0 is returned. If the package is already installed, 1 is returned (And a message is issued). In case of error, -1 (And an error message is issued) */ int InstallPkg(char *package) { char *ptr_name = NULL, *init_path = NULL, *input = NULL; char pkgfullpathname[PATH_MAX]; char PackageOrig[PATH_MAX]; int fd = 0; PkgData Data; /* Save locations and initialize the package structure */ strncpy(PackageOrig, package, PATH_MAX); PackageOrig[strlen(package)>PATH_MAX?PATH_MAX:strlen(package)] = '\0'; init_path = getcwd(malloc(PATH_MAX), PATH_MAX); memset(&Data, '\0', sizeof(PkgData)); /* Does it fisically exist? */ if ((fd = open(package, O_RDONLY)) < 0) { /* Ok, the package doesn't exists, let's go for a mirror */ if (DownloadPkg(package, pkgfullpathname) != 0) { free(init_path); return -1; } } else { close(fd); /* Get the full pathname instead of its relative name */ if (GetFileFullPath(package, pkgfullpathname)) { fprintf(stderr, "Failed to install %s\n", package); return -1; } } ptr_name = basename(pkgfullpathname); /* Switch to HOME_ROOT */ if (chdir(HOME_ROOT)) { fprintf(stderr, "Can't go to %s (%s)\n", HOME_ROOT, strerror(errno)); free(init_path); return -1; } /* Fill the package structure according to the package name */ if (FillPkgDataFromPackage(&Data, ptr_name)) { fprintf(stdout, "Wrong package name format. It should be \"" PACKAGE_NAME_FORMAT "\"\n"); chdir(init_path); return -1; } /* Open the database */ if (sqlite3_open(dbname, &Database)) { fprintf(stderr, "Failed to open database %s (%s)\n", dbname, sqlite3_errmsg(Database)); chdir(init_path); free(init_path); return -1; } /* Check if it is already installed */ if (ExistsPkg(&Data)) { fprintf(stderr, "Package %s already installed\n", PackageOrig); chdir(init_path); free(init_path); return 1; } /* Get the package hash so we can register it in the database */ if (GiveMeHash(pkgfullpathname, Data.crc) == -1) { chdir(init_path); free(init_path); return -1; } /* Extract the package itself */ if (ExtractPackage(pkgfullpathname, &Data) == -1) { chdir(init_path); free(init_path); return -1; } PostInstall(); /* Register the package in the database */ InsertPkgDB(&Data); /* Clean everything up */ sqlite3_close(Database); chdir(init_path); if (!noreadme) { printf("Press intro to continue"); getline(&input, (size_t *)&fd, stdin); free(input); } fprintf(stdout, "Package %s installed\n", PackageOrig); return 0; }