1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-26 04:00:59 +01:00

Implemented runtime --share= and --unshare options. Fixed some things.

This commit is contained in:
Wouter den Breejen 2007-10-02 15:52:50 +00:00
parent 86f0fd8341
commit d0458acb7c
14 changed files with 90 additions and 54 deletions

View file

@ -48,7 +48,7 @@ fi
--prefix=$nixstatepath \ --prefix=$nixstatepath \
--with-store-dir=/nix/store \ --with-store-dir=/nix/store \
--with-store-state-dir=/nix/state \ --with-store-state-dir=/nix/state \
--with-ext3cow-header=/nix/store/8nirllv1w6qv6c5srjgah2m82bfi1d6k-linux-2.6.21.5/lib/modules/2.6.21.5-default/build/include/linux/ext3cow_fs.h \ --with-ext3cow-header=/nix/store/v95qf520d6972pshykrah1dz3z53rkmj-linux-2.6.21.7/lib/modules/2.6.21.7-default/build/include/linux/ext3cow_fs.h \
--localstatedir=/nix/var --localstatedir=/nix/var

View file

@ -1726,7 +1726,7 @@ bool querySolidStateReferencesTxn(const Transaction & txn, const Path & statePat
return notempty; return notempty;
} }
void unShareStateTxn(const Transaction & txn, const Path & path, const bool copyFromOld) void unShareStateTxn(const Transaction & txn, const Path & path, const bool branch, const bool restoreOld) //TODO ADD BOOL YES/NO TO RESTORE OLD STATE (LAST REV.)
{ {
//Check if is statePath //Check if is statePath
if(!isValidStatePathTxn(txn, path)) if(!isValidStatePathTxn(txn, path))
@ -1739,24 +1739,32 @@ void unShareStateTxn(const Transaction & txn, const Path & path, const bool copy
//Remove Symlink //Remove Symlink
removeSymlink(path); removeSymlink(path);
//Remove earlier entries (unshare) (we must do this before revertToRevisionTxn)
nixDB.delPair(txn, dbSharedState, path);
//Touch dir with correct rights //Touch dir with correct rights
Derivation drv = derivationFromPathTxn(txn, queryStatePathDrvTxn(txn, path)); Derivation drv = derivationFromPathTxn(txn, queryStatePathDrvTxn(txn, path));
ensureStateDir(path, drv.stateOutputs.find("state")->second.username, "nixbld", "700"); ensureStateDir(path, drv.stateOutputs.find("state")->second.username, "nixbld", "700");
//Copy if necessary (TODO first snapshot?) if(branch && restoreOld)
if(copyFromOld){ throw Error(format("You cannot branch and restore the old state at the same time for path: '%1%' ") % path);
//Copy if necessary
if(branch){
copyContents(sharedWithOldPath, path); copyContents(sharedWithOldPath, path);
} }
//Remove earlier entries (unshare) //Restore the latest snapshot (non-recursive) made on this statepath
nixDB.delPair(txn, dbSharedState, path); if(restoreOld){
revertToRevisionTxn(txn, path, 0, false);
}
} }
void LocalStore::unShareState(const Path & path, const bool copyFromOld) void LocalStore::unShareState(const Path & path, const bool branch, const bool restoreOld)
{ {
Transaction txn(nixDB); Transaction txn(nixDB);
unShareStateTxn(txn, path, copyFromOld); unShareStateTxn(txn, path, branch, restoreOld);
txn.commit(); txn.commit();
} }
@ -1799,7 +1807,7 @@ void shareStateTxn(const Transaction & txn, const Path & from, const Path & to,
//If from was shared: unshare //If from was shared: unshare
Path empty; Path empty;
if(querySharedStateTxn(txn, from, empty)) if(querySharedStateTxn(txn, from, empty))
unShareStateTxn(txn, from, false); unShareStateTxn(txn, from, false, false);
//Remove state path and link //Remove state path and link
deletePathWrapped(from); deletePathWrapped(from);

View file

@ -118,7 +118,7 @@ public:
void shareState(const Path & from, const Path & to, const bool snapshot); void shareState(const Path & from, const Path & to, const bool snapshot);
void unShareState(const Path & path, const bool copyFromOld); void unShareState(const Path & path, const bool branch, const bool restoreOld);
}; };
@ -246,7 +246,7 @@ void setSolidStateReferencesTxn(const Transaction & txn, const Path & statePath,
bool querySolidStateReferencesTxn(const Transaction & txn, const Path & statePath, PathSet & paths); bool querySolidStateReferencesTxn(const Transaction & txn, const Path & statePath, PathSet & paths);
void shareStateTxn(const Transaction & txn, const Path & from, const Path & to, const bool snapshot); void shareStateTxn(const Transaction & txn, const Path & from, const Path & to, const bool snapshot);
void unShareStateTxn(const Transaction & txn, const Path & path, const bool copyFromOld); void unShareStateTxn(const Transaction & txn, const Path & path, const bool branch, const bool restoreOld);
PathSet toNonSharedPathSetTxn(const Transaction & txn, const PathSet & statePaths); PathSet toNonSharedPathSetTxn(const Transaction & txn, const PathSet & statePaths);
Path toNonSharedPathTxn(const Transaction & txn, const Path & statePath); Path toNonSharedPathTxn(const Transaction & txn, const Path & statePath);

View file

@ -540,11 +540,12 @@ void RemoteStore::shareState(const Path & from_arg, const Path & to_arg, const b
readInt(from); readInt(from);
} }
void RemoteStore::unShareState(const Path & path, const bool copyFromOld) void RemoteStore::unShareState(const Path & path, const bool branch, const bool restoreOld)
{ {
writeInt(wopUnShareState, to); writeInt(wopUnShareState, to);
writeString(path, to); writeString(path, to);
writeInt(copyFromOld ? 1 : 0, to); writeInt(branch ? 1 : 0, to);
writeInt(restoreOld ? 1 : 0, to);
processStderr(); processStderr();
readInt(from); readInt(from);
} }

View file

@ -104,7 +104,7 @@ public:
void shareState(const Path & from, const Path & to, const bool snapshot); void shareState(const Path & from, const Path & to, const bool snapshot);
void unShareState(const Path & path, const bool copyFromOld); void unShareState(const Path & path, const bool branch, const bool restoreOld);
private: private:
AutoCloseFD fdSocket; AutoCloseFD fdSocket;

View file

@ -250,7 +250,7 @@ public:
virtual void shareState(const Path & from, const Path & to, const bool snapshot) = 0; virtual void shareState(const Path & from, const Path & to, const bool snapshot) = 0;
/* TODO */ /* TODO */
virtual void unShareState(const Path & path, const bool copyFromOld) = 0; virtual void unShareState(const Path & path, const bool branch, const bool restoreOld) = 0;
}; };

View file

@ -108,6 +108,12 @@ void revertToRevisionTxn(const Transaction & txn, const Path & statePath, const
RevisionClosureTS getTimestamps; RevisionClosureTS getTimestamps;
queryStateRevisionsTxn(txn, statePath_ns, getRivisions, getTimestamps, revision_arg); queryStateRevisionsTxn(txn, statePath_ns, getRivisions, getTimestamps, revision_arg);
//TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//include recursive
//lookup 0 (latest) to the real revision
//Revert each statePath in the list //Revert each statePath in the list
for (RevisionClosure::iterator i = getRivisions.begin(); i != getRivisions.end(); ++i){ for (RevisionClosure::iterator i = getRivisions.begin(); i != getRivisions.end(); ++i){
Path statePath = (*i).first; Path statePath = (*i).first;
@ -180,7 +186,7 @@ void revertToRevisionTxn(const Transaction & txn, const Path & statePath, const
} }
//*** Now also revert state references to the specific revision (the revision is already converted to a timestamp here) //*** Now also revert _state references_ to the specific revision (the revision is already converted to a timestamp here)
//Query the references of the old revision (already converted to a timestamp) //Query the references of the old revision (already converted to a timestamp)
PathSet state_references; PathSet state_references;

View file

@ -25,6 +25,7 @@ namespace nix {
void scanAndUpdateAllReferencesRecusivelyTxn(const Transaction & txn, const Path & statePath); void scanAndUpdateAllReferencesRecusivelyTxn(const Transaction & txn, const Path & statePath);
/* revision 0 == latest ????? */
void revertToRevisionTxn(const Transaction & txn, const Path & statePath, const int revision_arg, const bool recursive); void revertToRevisionTxn(const Transaction & txn, const Path & statePath, const int revision_arg, const bool recursive);
@ -54,7 +55,7 @@ namespace nix {
bool queryStateReferences(Database & nixDB, const Transaction & txn, TableId references_table, TableId revisions_table, bool queryStateReferences(Database & nixDB, const Transaction & txn, TableId references_table, TableId revisions_table,
const Path & statePath, Strings & references, const unsigned int revision = 0, const unsigned int timestamp = 0); const Path & statePath, Strings & references, const unsigned int revision = 0, const unsigned int timestamp = 0);
/* Set the revision number of the statePath and the revision numbers of all state paths in the references closure */ /* Get the revision number of the statePath and the revision numbers of all state paths in the references closure */
void setStateRevisions(Database & nixDB, const Transaction & txn, TableId revisions_table, TableId revisions_comments, void setStateRevisions(Database & nixDB, const Transaction & txn, TableId revisions_table, TableId revisions_comments,
TableId snapshots_table, const RevisionClosure & revisions, const Path & rootStatePath, const string & comment); TableId snapshots_table, const RevisionClosure & revisions, const Path & rootStatePath, const string & comment);

View file

@ -1295,15 +1295,15 @@ void symlinkPath(const Path & existingDir, const Path & newLinkName) //TODO bool
*/ */
runProgram_AndPrintOutput("/bin/sh", true, p_args, "sh-ln"); runProgram_AndPrintOutput("/bin/sh", true, p_args, "sh-ln");
executeShellCommand("whoami"); //executeShellCommand("whoami");
executeShellCommand("pwd"); //executeShellCommand("pwd");
printMsg(lvlError, format("ln -sf %1% %2%") % existingDir % newLinkName); printMsg(lvlError, format("ln -sf %1% %2%") % existingDir % newLinkName);
} }
void removeSymlink(const string & path) void removeSymlink(const string & path)
{ {
if(path[path.length() - 1] != '/') if(path[path.length() - 1] == '/')
throw Error(format("We dont want to remove the enitre directory, only the symlink, but a / is given for `%1%'") % path); throw Error(format("We dont want to remove the symlink, not the enitre directory, but a / is given for `%1%'") % path);
deletePath(path); deletePath(path);
} }
@ -1316,8 +1316,6 @@ void ensureStateDir(const Path & statePath, const string & user, const string &
void copyContents(const Path & from, const Path & to) //TODO bool shellexpansion, bool failure for nix-env void copyContents(const Path & from, const Path & to) //TODO bool shellexpansion, bool failure for nix-env
{ {
//executeShellCommand("whoami");
//TODO Could be a symlink (to a non-existing dir) //TODO Could be a symlink (to a non-existing dir)
/* /*
if(!DirectoryExist(from)) if(!DirectoryExist(from))
@ -1326,33 +1324,21 @@ void copyContents(const Path & from, const Path & to) //TODO bool shellexpansion
throw Error(format("Path `%1%' doenst exist ...") % to); throw Error(format("Path `%1%' doenst exist ...") % to);
*/ */
//executeShellCommand("ls -l "+from); //TODO do a Rsync instead of a cp.
//executeShellCommand("ls -l "+from+"/"); //rsync -avHx --delete from to
//executeShellCommand("ls -l "+to);
//executeShellCommand("ls -l "+to+"/");
//Copy all files + dirs recursively //Copy all files + dirs recursively
Strings p_args; Strings p_args;
p_args.push_back("-c"); //we call the shell (/bin/sh -c) so it expands the * to all files p_args.push_back("-c"); //we call the shell (/bin/sh -c) so it expands the * to all files
p_args.push_back("cp -R "+from + "/* "+to); p_args.push_back("cp -R "+from + "/* "+to);
/*
p_args.push_back("cp");
p_args.push_back("-R");
p_args.push_back(from + "/*");
p_args.push_back(to);
*/
runProgram_AndPrintOutput("/bin/sh", true, p_args, "sh-cp"); runProgram_AndPrintOutput("/bin/sh", true, p_args, "sh-cp");
//Also copy the hidden files (but not the ../ dir) //Also copy the hidden files (but not the ../ dir)
p_args.empty(); p_args.empty();
p_args.push_back("-c"); p_args.push_back("-c");
p_args.push_back("cp " + from + "/.[a-zA-Z0-9]* " +to); p_args.push_back("cp " + from + "/.[a-zA-Z0-9]* " +to);
/*
p_args.push_back(cp");
p_args.push_back(from + "/.[a-zA-Z0-9]*");
p_args.push_back(to);
*/
runProgram_AndPrintOutput("/bin/sh", true, p_args, "sh-cp"); runProgram_AndPrintOutput("/bin/sh", true, p_args, "sh-cp");
} }
} }

View file

@ -639,7 +639,7 @@ static void installDerivations(Globals & globals,
PathSet comparePaths; PathSet comparePaths;
comparePaths.insert(statePath); comparePaths.insert(statePath);
comparePaths.insert(read_statePath); comparePaths.insert(read_statePath);
if(store->toNonSharedPathSet(comparePaths).size() != 1) //TODO !!!!!!!!!!!!! only copy when not already a symlink to a statePath !!!!!!!!! if(store->toNonSharedPathSet(comparePaths).size() != 1) //TODO !!!!!!!!!!!!!??
copyContents(externalState, statePath); copyContents(externalState, statePath);
} }
else else

View file

@ -1,4 +1,4 @@
Usage: nix-state [OPTIONS...] [ARGUMENTS...] Usage: nix-state [OPTIONS...] [ARGUMENTS...] [NIX Store/State-Path]
`nix-state' is a tool to manipulate the Nix state-store and to run programs. `nix-state' is a tool to manipulate the Nix state-store and to run programs.
@ -26,12 +26,13 @@ Information:
Share state Operations: Share state Operations:
--unshare: unshare this path --unshare: unshare this path
--share-with a b: make the path a point to the path b --share-with=a b: make the statepath b point to a
--copy-from=a b: make the contents of statepath b equal to the contents of a
Share state Options: Share state Options:
--unshare-branch-state: --unshare-and-branch-state:
--unshare-restore-old-state: --unshare-and-restore-old-state: (default)
Revert state Operations: Revert state Operations:

File diff suppressed because one or more lines are too long

View file

@ -33,7 +33,9 @@ bool r_commit = true;
bool r_run = true; bool r_run = true;
bool revert_recursively = false; bool revert_recursively = false;
bool unshare_branch = false; bool unshare_branch = false;
bool unshare_restore_old = false; bool unshare_restoreOld = true;
string share_with;
string copy_from;
/************************* Build time Functions ******************************/ /************************* Build time Functions ******************************/
@ -267,12 +269,27 @@ static void opShowSharedPaths(Strings opFlags, Strings opArgs)
static void opUnshare(Strings opFlags, Strings opArgs) static void opUnshare(Strings opFlags, Strings opArgs)
{ {
Path statePath = *(opArgs.begin());
if(!store->isValidStatePath(statePath))
throw UsageError(format("Path '%1%' is not a valid state path.") % statePath);
store->unShareState(statePath, unshare_branch, unshare_restoreOld);
} }
static void opShareWith(Strings opFlags, Strings opArgs) static void opShareWith(Strings opFlags, Strings opArgs)
{ {
Path statePath = *(opArgs.begin());
if(!store->isValidStatePath(statePath))
throw UsageError(format("Path '%1%' is not a valid state path.") % statePath);
store->shareState(statePath, share_with, false);
}
static void opCopyFrom(Strings opFlags, Strings opArgs)
{
//TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//copy_from
throw UsageError(format("TODO !!!!!!!!!!!!"));
} }
@ -307,10 +324,14 @@ static void opRunComponent(Strings opFlags, Strings opArgs)
if(*i == "--help" || *i == "--version"){ if(*i == "--help" || *i == "--version"){
printMsg(lvlError, format("Usage: try --statehelp for extended state help options")); printMsg(lvlError, format("Usage: try --statehelp for extended state help options"));
printMsg(lvlError, format("%1%") % padd("", '-', 54)); printMsg(lvlError, format("%1%") % padd("", '-', 54));
r_scanforReferences = false;
r_commit = false;
} }
else if(*i == "--statehelp"){ else if(*i == "--statehelp"){
printMsg(lvlError, format("%1%") % padd("", '-', 100)); printMsg(lvlError, format("%1%") % padd("", '-', 100));
printHelp(); printHelp();
r_scanforReferences = false;
r_commit = false;
} }
//printMsg(lvlError, format("ARG %1%") % *i); //printMsg(lvlError, format("ARG %1%") % *i);
@ -555,6 +576,11 @@ void run(Strings args)
/* test */ /* test */
if(args.size() == 1 && ( *(args.begin()) == "--help" || *(args.begin()) == "--statehelp")){
printHelp();
return;
}
for (Strings::iterator i = args.begin(); i != args.end(); ) { for (Strings::iterator i = args.begin(); i != args.end(); ) {
string arg = *i++; string arg = *i++;
@ -612,17 +638,23 @@ void run(Strings args)
op = opShowSharedPaths; op = opShowSharedPaths;
else if (arg == "--unshare") else if (arg == "--unshare")
op = opUnshare; op = opUnshare;
else if (arg == "--unshare-branch-state") else if (arg == "--unshare-and-branch-state")
unshare_branch = true; unshare_branch = true;
else if (arg == "--unshare-restore-old-state") else if (arg == "--unshare-and-restore-old-state") //default true
unshare_restore_old = true; unshare_restoreOld = true;
else if (arg == "--share-with") else if (arg.substr(0,13) == "--share-with="){
op = opShareWith; op = opShareWith;
share_with = arg.substr(13,arg.length());
}
else if (arg.substr(0,12) == "--copy-from="){
op = opCopyFrom;
copy_from = arg.substr(12,arg.length());
}
/* /*
--share-from
--unshare --copy-from
*/ */

View file

@ -615,9 +615,10 @@ static void performOp(Source & from, Sink & to, unsigned int op)
case wopUnShareState: { case wopUnShareState: {
Path path = readString(from); Path path = readString(from);
bool copyFromOld = readInt(from) == 1; bool branch = readInt(from) == 1;
bool restoreOld = readInt(from) == 1;
startWork(); startWork();
store->unShareState(path, copyFromOld); store->unShareState(path, branch, restoreOld);
stopWork(); stopWork();
writeInt(1, to); writeInt(1, to);
break; break;