1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-26 04:00:59 +01:00
This commit is contained in:
Wouter den Breejen 2007-07-24 12:47:28 +00:00
parent a07ba681cc
commit dc4395b737
16 changed files with 164 additions and 168 deletions

View file

@ -6,8 +6,12 @@ pkginclude_HEADERS = snapshot.hh
#TODO linux kernel header
libext3cow_la_LIBADD = ../boost/format/libformat.la
libext3cow_la_LIBADD = ../libutil/libutil.la \
../boost/format/libformat.la
AM_CXXFLAGS = -Wall \
-I$(srcdir)/.. ${aterm_include}
-I$(srcdir)/.. ${aterm_include} \
-I$(srcdir)/../libutil
AM_CFLAGS = \
${aterm_include}

View file

@ -6,11 +6,14 @@
#include "ext3cow_tools.h"
#include "snapshot.hh"
#include "types.hh"
//using namespace nix;
namespace nix {
/////////////////////Original functions commented out:
/*
void snapshot_usage(void){
@ -18,7 +21,6 @@ void snapshot_usage(void){
}
int snapshot_main(int argc, char** argv){
int fd;
@ -56,13 +58,14 @@ int snapshot_main(int argc, char** argv){
}
*/
//End original function
unsigned int take_snapshot(const char* dir) //const string & file_or_dir)
/////////////////////End original functions
unsigned int take_snapshot(const string & dir2) //const string & file_or_dir)
{
//const char* dir = "test";
const char* dir = dir2.c_str();
int fd;
int ret;
unsigned int epoch = 0;
//char path[256] = ".\0";

View file

@ -1,10 +1,12 @@
#ifndef SNAPSHOT_H_
#define SNAPSHOT_H_
#include "types.hh"
namespace nix {
//unsigned int take_snapshot(const string & file_or_dir);
unsigned int take_snapshot(const char* dir);
unsigned int take_snapshot(const string & dir);
}

View file

@ -1812,10 +1812,6 @@ void DerivationGoal::computeClosure()
//Commit state
commitStatePathTxn(txn, statePath);
//Set first revision (if we committed something)
if(readRevisionNumber(statePath) == 1)
updateRevisionsRecursivelyTxn(txn, statePath);
//Shared state
Path sharedState = drv.stateOutputs.find("state")->second.sharedState;
if(sharedState != ""){

View file

@ -1,6 +1,9 @@
#include "db.hh"
#include "util.hh"
#include "pathlocks.hh"
#include "derivations.hh"
#include "local-store.hh"
#include "misc.hh"
#include <sys/types.h>
#include <sys/stat.h>
@ -595,24 +598,38 @@ void Database::setStateRevisions(const Transaction & txn, TableId table,
int root_revision = getNewRevisionNumber(txn, table, statePath);
//Sort based on statePath to RevisionNumbersClosure
RevisionNumbers sorted_revisions;
vector<Path> sortedStatePaths;
for (RevisionNumbersSet::const_iterator i = revisions.begin(); i != revisions.end(); ++i)
sortedStatePaths.push_back((*i).first);
sort(sortedStatePaths.begin(), sortedStatePaths.end());
for (vector<Path>::const_iterator i = sortedStatePaths.begin(); i != sortedStatePaths.end(); ++i)
sorted_revisions.push_back(revisions.at(*i));
vector<RevisionNumbers> sorted_revisions;
for (vector<Path>::const_iterator i = sortedStatePaths.begin(); i != sortedStatePaths.end(); ++i){
map<Path, unsigned int> ss_revisions = revisions.at(*i);
//Sort the set of paths that have revisions based on
vector<Path> sorted_ssp;
for (map<Path, unsigned int>::const_iterator j = ss_revisions.begin(); j != ss_revisions.end(); ++j)
sorted_ssp.push_back((*j).first);
sort(sorted_ssp.begin(), sorted_ssp.end());
//Insert into sorted_ss_revs based on the sorted order
RevisionNumbers sorted_ss_revs;
for (vector<Path>::const_iterator j = sorted_ssp.begin(); j != sorted_ssp.end(); ++j)
sorted_ss_revs.push_back(ss_revisions.at(*j));
//Insert ......
sorted_revisions.push_back(sorted_ss_revs);
}
//Debugging
for (vector<Path>::const_iterator i = sortedStatePaths.begin(); i != sortedStatePaths.end(); ++i)
printMsg(lvlError, format("Insert: %1% into %2%") % int2String(revisions.at(*i)) % *i);
//for (vector<Path>::const_iterator i = sortedStatePaths.begin(); i != sortedStatePaths.end(); ++i)
// printMsg(lvlError, format("Insert: %1% into %2%") % revisions.at(*i) % *i);
//Convert the int's into Strings
//Convert the vector<RevisionNumbers> into Strings
Strings data;
for (RevisionNumbers::const_iterator i = sorted_revisions.begin(); i != sorted_revisions.end(); ++i) {
int revisionnumber = *i;
data.push_back(int2String(revisionnumber));
}
for (vector<RevisionNumbers>::const_iterator i = sorted_revisions.begin(); i != sorted_revisions.end(); ++i)
data.push_back(packRevisionNumbers(*i));
//Create the key
string key = makeStatePathRevision(statePath, root_revision);
@ -651,14 +668,32 @@ bool Database::queryStateRevisions(const Transaction & txn, TableId table, const
//Convert the Strings into int's and match them to the sorted statePaths
for (vector<Path>::const_iterator i = sortedStatePaths.begin(); i != sortedStatePaths.end(); ++i){
string getRevisionS = data.front();
RevisionNumbers ss_revisions = unpackRevisionNumbers(data.front());
data.pop_front();
int getRevision;
bool succeed = string2Int(getRevisionS, getRevision);
if(!succeed)
throw Error(format("Cannot read revision number from db of path '%1%'") % statePath);
revisions[*i] = getRevision;
//query state versioined directorys/files
vector<Path> sortedPaths;
Derivation drv = derivationFromPath(queryStatePathDrvTxn(txn, statePath));
DerivationStateOutputs stateOutputs = drv.stateOutputs;
DerivationStateOutputDirs stateOutputDirs = drv.stateOutputDirs;
for (DerivationStateOutputDirs::const_iterator j = stateOutputDirs.begin(); j != stateOutputDirs.end(); ++j){
string thisdir = (j->second).path;
string fullstatedir = statePath + "/" + thisdir;
if(thisdir == "/") //exception for the root dir
fullstatedir = statePath + "/";
sortedPaths.push_back(fullstatedir);
}
sort(sortedPaths.begin(), sortedPaths.end()); //sort
//link
map<Path, unsigned int> revisions_ss;
for (vector<Path>::const_iterator j = sortedPaths.begin(); j != sortedPaths.end(); ++j){
revisions_ss[*j] = ss_revisions.front();
ss_revisions.pop_front();
}
revisions[*i] = revisions_ss;
}
if(!notempty)

View file

@ -1700,11 +1700,6 @@ void LocalStore::commitStatePath(const Path & statePath)
nix::commitStatePathTxn(noTxn, statePath);
}
void LocalStore::updateRevisionsRecursively(const Path & statePath)
{
nix::updateRevisionsRecursivelyTxn(noTxn, statePath);
}
void setSolidStateReferencesTxn(const Transaction & txn, const Path & statePath, const PathSet & paths)
{
Strings ss = Strings(paths.begin(), paths.end());

View file

@ -107,8 +107,6 @@ public:
bool queryAvailableStateRevisions(const Path & statePath, RevisionNumbers & revisions);
void commitStatePath(const Path & statePath);
void updateRevisionsRecursively(const Path & statePath);
};

View file

@ -492,10 +492,4 @@ void RemoteStore::commitStatePath(const Path & statePath)
}
//TODO
void RemoteStore::updateRevisionsRecursively(const Path & statePath)
{
}
}

View file

@ -96,8 +96,6 @@ public:
void commitStatePath(const Path & statePath);
void updateRevisionsRecursively(const Path & statePath);
private:
AutoCloseFD fdSocket;
FdSink to;

View file

@ -229,8 +229,6 @@ public:
virtual bool queryAvailableStateRevisions(const Path & statePath, RevisionNumbers & revisions) = 0;
virtual void commitStatePath(const Path & statePath) = 0;
virtual void updateRevisionsRecursively(const Path & statePath) = 0;
};

View file

@ -34,40 +34,17 @@ void createStateDirs(const DerivationStateOutputDirs & stateOutputDirs, const De
Path statePath = stateOutputs.find("state")->second.statepath;
string stateDir = statePath;
/*
string svnbin = nixSVNPath + "/svn";
string svnadminbin = nixSVNPath + "/svnadmin";
PathSet intervalPaths;
//check if we can create state and staterepos dirs
//check if we can create state dirs
//TODO
//Create a repository for this state location
string repos = getStateReposPath("stateOutput:staterepospath", stateDir);
printMsg(lvlTalkative, format("Adding statedir '%1%' from repository '%2%'") % stateDir % repos);
if(IsDirectory(repos))
printMsg(lvlTalkative, format("Repos %1% already exists, so we use that repository") % repos);
else{
Strings p_args;
p_args.push_back("create");
p_args.push_back(repos);
runProgram_AndPrintOutput(svnadminbin, true, p_args, "svnadmin"); //TODO create as nixbld.nixbld chmod 700... can you still commit then ??
}
string statedir_svn = stateDir + "/.svn/";
if( ! IsDirectory(statedir_svn) ){
Strings p_args;
p_args.push_back("checkout");
p_args.push_back("file://" + repos);
p_args.push_back(stateDir);
runProgram_AndPrintOutput(svnbin, true, p_args, "svn"); //TODO checkout as user
/*
if( ! IsDirectory( ....... ) ){
}
else
printMsg(lvlTalkative, format("Statedir %1% already exists, so dont check out its repository again") % statedir_svn);
printMsg(lvlTalkative, format("Statedir %1% already exists, so dont ........ ???? ") % ...);
*/
for (DerivationStateOutputDirs::const_reverse_iterator i = stateOutputDirs.rbegin(); i != stateOutputDirs.rend(); ++i){
DerivationStateOutputDir d = i->second;
@ -93,35 +70,21 @@ void createStateDirs(const DerivationStateOutputDirs & stateOutputDirs, const De
//Initialize the counters for the statePaths that have an interval to 0
vector<int> empty;
store->setStatePathsInterval(intervalPaths, empty, true);
*/
}
void commitStatePathTxn(const Transaction & txn, const Path & statePath)
{
//TODO: include code from: updateRevisionsRecursivelyTxn(txn, root_statePath);
if(!isValidStatePathTxn(txn, statePath))
throw Error(format("path `%1%' is not a valid state path") % statePath);
//queryDeriversStatePath??
Derivation drv = derivationFromPath(queryStatePathDrvTxn(txn, statePath));
//Extract the neccecary info from each Drv
DerivationStateOutputs stateOutputs = drv.stateOutputs;
//Path statePath = stateOutputs.find("state")->second.statepath;
DerivationStateOutputDirs stateOutputDirs = drv.stateOutputDirs;
//Specifiy the SVN binarys
string svnbin = nixSVNPath + "/svn";
string svnadminbin = nixSVNPath + "/svnadmin";
//Print
printMsg(lvlError, format("Committing statePath: %1%") % statePath);
//Vector includeing all commit scripts:
vector<string> subversionedpaths;
vector<bool> subversionedpathsCommitBoolean;
vector<string> nonversionedpaths; //of type none, no versioning needed
printMsg(lvlError, format("Snapshotting statePath: %1%") % statePath);
//Get all the inverals from the database at once
PathSet intervalPaths;
@ -135,10 +98,10 @@ void commitStatePathTxn(const Transaction & txn, const Path & statePath)
intervalPaths.insert(fullstatedir);
}
}
vector<int> intervals = store->getStatePathsInterval(intervalPaths); //TODO !!!!!!!!!!!!! txn ??
vector<int> intervals = store->getStatePathsInterval(intervalPaths); //TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! txn
int intervalAt=0;
for (DerivationStateOutputDirs::const_reverse_iterator i = stateOutputDirs.rbegin(); i != stateOutputDirs.rend(); ++i){
for (DerivationStateOutputDirs::const_iterator i = stateOutputDirs.begin(); i != stateOutputDirs.end(); ++i){
DerivationStateOutputDir d = i->second;
string thisdir = d.path;
@ -147,61 +110,50 @@ void commitStatePathTxn(const Transaction & txn, const Path & statePath)
fullstatedir = statePath + "/";
if(d.type == "none"){
nonversionedpaths.push_back(fullstatedir);
continue;
}
subversionedpaths.push_back(fullstatedir);
if(d.type == "interval"){
//Get the interval-counter from the database
int interval_counter = intervals[intervalAt];
int interval = d.getInterval();
subversionedpathsCommitBoolean.push_back(interval_counter % interval == 0);
//update the interval
intervals[intervalAt] = interval_counter + 1;
intervalAt++;
if(interval_counter % interval != 0){ return; }
}
else if(d.type == "full")
subversionedpathsCommitBoolean.push_back(true);
else if(d.type == "manual") //TODO !!!!!
subversionedpathsCommitBoolean.push_back(false);
else if(d.type == "full"){ }
else if(d.type == "manual"){ return; } //TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
else
throw Error(format("interval '%1%' is not handled in nix-state") % d.type);
throw Error(format("Type '%1%' is not handled in nix-state") % d.type);
//We got here so we need to commit
unsigned int epoch_time;
if(pathExists(fullstatedir) || FileExist(fullstatedir)){
epoch_time = take_snapshot(fullstatedir);
printMsg(lvlError, format("Snapshotted '%1%' with id '%2%'") % fullstatedir % epoch_time);
}
else
{
//TODO !!!!!!!!!!!!!!
}
//Get the a repository for this state location
string repos = getStateReposPath("stateOutput:staterepospath", statePath); //this is a copy from store-state.cc
string checkoutcommand = svnbin + " --ignore-externals checkout file://" + repos + " " + statePath;
//Call the commit script with the appropiate paramenters
string subversionedstatepathsarray;
for (vector<string>::iterator i = subversionedpaths.begin(); i != subversionedpaths.end(); ++i)
subversionedstatepathsarray += *(i) + " ";
string subversionedpathsCommitBooleansarray;
for (vector<bool>::iterator i = subversionedpathsCommitBoolean.begin(); i != subversionedpathsCommitBoolean.end(); ++i)
subversionedpathsCommitBooleansarray += bool2string(*i) + " ";
string nonversionedstatepathsarray;
for (vector<string>::iterator i = nonversionedpaths.begin(); i != nonversionedpaths.end(); ++i)
nonversionedstatepathsarray += *(i) + " ";
//make the call to the commit script
Strings p_args;
p_args.push_back(svnbin);
p_args.push_back(subversionedstatepathsarray);
p_args.push_back(subversionedpathsCommitBooleansarray);
p_args.push_back(nonversionedstatepathsarray);
p_args.push_back(checkoutcommand);
p_args.push_back(statePath);
runProgram_AndPrintOutput(nixLibexecDir + "/nix/nix-statecommit.sh", true, p_args, "svn");
//Put in database !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//TODO
}
//Update the intervals again
//setStatePathsIntervalTxn(txn, intervalPaths, intervals); //TODO!!!!!!!!!!!!!!!!!!!!!! uncomment and txn ??
//setStatePathsIntervalTxn(txn, intervalPaths, intervals); //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!! uncomment
}
/*
* This function takes all state requisites (references) and revision numbers and stores them ...
*/
/*
void updateRevisionsRecursivelyTxn(const Transaction & txn, const Path & statePath)
{
//Save all revisions for the call to
@ -229,13 +181,6 @@ void updateRevisionsRecursivelyTxn(const Transaction & txn, const Path & statePa
int readRevisionNumber(Path statePath)
{
string s = "/media/ext3cow/cca/";
const char* ss = s.c_str();
unsigned int i = take_snapshot(ss);
printMsg(lvlError, format("SS: '%1%'") % i);
////////
/*
string svnbin = nixSVNPath + "/svn";
RevisionNumbers revisions;
@ -257,8 +202,12 @@ int readRevisionNumber(Path statePath)
throw Error(format("Cannot read revision number of path '%1%'") % repos);
return revision;
*/
return 0;
}
*/
// string s = "/media/ext3cow/cca/";
// unsigned int i = take_snapshot(s);
// printMsg(lvlError, format("SS: '%1%'") % i);
}

View file

@ -14,10 +14,10 @@ void createStateDirs(const DerivationStateOutputDirs & stateOutputDirs, const De
void commitStatePathTxn(const Transaction & txn, const Path & statePath);
/* TODO */
void updateRevisionsRecursivelyTxn(const Transaction & txn, const Path & statePath);
//void updateRevisionsRecursivelyTxn(const Transaction & txn, const Path & statePath);
/* TODO */
int readRevisionNumber(Path statePath);
//int readRevisionNumber(Path statePath);
}

View file

@ -60,7 +60,7 @@ typedef set<Path> PathSet;
//state types
typedef list<int> RevisionNumbers; //the Strings (list) of StateReferences and this list are connected by position
typedef map<Path, RevisionNumbers> RevisionNumbersSet; //We include to the paths to sort on
typedef map<Path, map<Path, unsigned int> > RevisionNumbersSet; //We include to the paths to sort on
typedef map<int, Strings> StateReferences;

View file

@ -918,6 +918,36 @@ Strings unpackStrings(const string & s)
return strings;
}
string packRevisionNumbers(const RevisionNumbers & revs)
{
string seperator = "|";
string d = "";
for (RevisionNumbers::const_iterator i = revs.begin();
i != revs.end(); ++i){
d += int2String(*i) + seperator;
}
return d;
}
RevisionNumbers unpackRevisionNumbers(const string & packed)
{
string seperator = "|";
Strings ss = tokenizeString(packed, seperator);
RevisionNumbers revs;
for (Strings::const_iterator i = ss.begin();
i != ss.end(); ++i){
int rev;
bool succeed = string2Int(*i, rev);
if(!succeed)
throw Error(format("Corrupted revisions db entry: `%1%'") % packed);
revs.push_back(rev);
}
return revs;
}
Strings tokenizeString(const string & s, const string & separators)
{

View file

@ -302,7 +302,7 @@ string time_t2string(const time_t & t);
bool FileExist(const string FileName);
bool IsDirectory(const string FileName);
bool IsDirectory(const string FileName); //TODO replace by pathexists
string getCallingUserName();
@ -312,6 +312,10 @@ PathSet pathSets_union(const PathSet & paths1, const PathSet & paths2);
/* TODO */
void pathSets_difference(const PathSet & oldpaths, const PathSet & newpaths, PathSet & addedpaths, PathSet & removedpaths);
string packRevisionNumbers(const RevisionNumbers & revs);
RevisionNumbers unpackRevisionNumbers(const string & packed);
}
#endif /* !__UTIL_H */

View file

@ -252,19 +252,9 @@ static void revertToRevision(Strings opFlags, Strings opArgs)
//Revert each statePath in the list
for (RevisionNumbersSet::iterator i = getRivisions.begin(); i != getRivisions.end(); ++i){
Path statePath = (*i).first;
int revision = (*i).second;
string repos = getStateReposPath("stateOutput:staterepospath", statePath); //this is a copy from store-state.cc
map<Path, unsigned int> revisioned_paths = (*i).second;
printMsg(lvlError, format("Reverting statePath '%1%' to revision: %2%") % statePath % int2String(revision));
Strings p_args;
p_args.push_back(nixSVNPath + "/svn");
p_args.push_back(int2String(revision));
p_args.push_back("file://" + repos);
p_args.push_back(statePath);
runProgram_AndPrintOutput(nixLibexecDir + "/nix/nix-restorerevision.sh", true, p_args, "svn"); //run
//TODO !!!!!!!!!!!!!!!!!!!!! do a commit
//TODO !!!!!!!!!!!!!!!!!!!!! check if statePath is a working copy
//TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
@ -364,7 +354,10 @@ void scanAndUpdateAllReferencesRecusivelyTxn(const Transaction & txn, const Path
//We dont need to sort since the db does that
//call scanForAllReferences again on all statePaths
for (PathSet::iterator i = statePaths.begin(); i != statePaths.end(); ++i){
int revision = readRevisionNumber(*i);
//TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int revision = 0;
//Get last revision number from DB !!!!!!!!!!
//Scan, update, call recursively
PathSet newFoundComponentReferences;
@ -419,18 +412,15 @@ static void opRunComponent(Strings opFlags, Strings opArgs)
//******************* With everything in place, we call the commit script on all statePaths (in)directly referenced **********************
//Commit all statePaths
for (PathSet::iterator i = statePaths.begin(); i != statePaths.end(); ++i) //TODO first commit own state path?
commitStatePathTxn(txn, *i);
//Start transaction TODO
//Scan for new references, and update with revision number
//Scan for new references if neccecary
if(scanforReferences)
scanAndUpdateAllReferencesRecusivelyTxn(txn, root_statePath);
//Get new revision number
updateRevisionsRecursivelyTxn(txn, root_statePath);
//Commit all statePaths
for (PathSet::iterator i = statePaths.begin(); i != statePaths.end(); ++i) //TODO first commit own state path?
commitStatePathTxn(txn, *i);
//Commit transaction
//txn.commit();
@ -439,7 +429,7 @@ static void opRunComponent(Strings opFlags, Strings opArgs)
RevisionNumbersSet getRivisions;
bool b = store->queryStateRevisions(root_statePath, getRivisions, -1);
for (RevisionNumbersSet::iterator i = getRivisions.begin(); i != getRivisions.end(); ++i){
printMsg(lvlError, format("State %1% has revision %2%") % (*i).first % int2String((*i).second));
//printMsg(lvlError, format("State %1% has revision %2%") % (*i).first % int2String((*i).second));
}