Platonides has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/70107


Change subject: Implementing verbose messages.
......................................................................

Implementing verbose messages.

The memory of localname and last.name is never freed. That is
expected and not a bug.

Change-Id: I2adfc7e59b9c3d4472322edf83df05f9b7426262
---
M src/take.cc
1 file changed, 57 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/labs/toollabs 
refs/changes/07/70107/1

diff --git a/src/take.cc b/src/take.cc
index 08dedc0..c496002 100755
--- a/src/take.cc
+++ b/src/take.cc
@@ -12,6 +12,7 @@
 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
+#include <pwd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -55,6 +56,48 @@
 
 gid_t  groups[256];
 int    ngroups;
+
+char *getname(uid_t uid) {
+       static struct {
+               uid_t uid;
+               char *name;
+               size_t len;
+       } last = { -0, NULL, 0 };
+
+       if (uid == last.uid && last.name) {
+               return last.name;
+       }
+
+       char *pointer;
+       struct passwd *p = getpwuid(uid);
+       if (!p) {
+               if (asprintf(&pointer, "unknown user #%d", uid)) {
+                       error("Problem reserving name for unknown user");
+                       exit(1);
+               }
+       }
+
+       size_t len = strlen(p->pw_name) + 1;
+       if (last.len >= len) {
+               last.uid = uid;
+               strcpy(last.name, p->pw_name);
+               return last.name;
+       }
+
+       pointer = (char*)realloc(last.name, len);
+       if (!pointer) {
+               error(p->pw_name);
+               exit(1);
+       }
+
+       last.name = pointer;
+       last.len = len;
+       strcpy(last.name, p->pw_name);
+       return last.name;
+}
+
+char *localname = strdup(getname(getuid()));
+
 
 bool takeover(const char* path, bool trustpath)
 {
@@ -139,12 +182,23 @@
        if (!recursive && S_ISDIR(sfile.st_mode))
                return refuse(path, "Is a directory, use -r to take 
recursively.");
 
-       // everything checks out; do it.  Do it!  Doooo it!
-       if(fchown(file, getuid(), -1))
-               return error(path);
+       if (sfile.st_uid == getuid()) {
+               if (verbose && !trustpath)
+                       printf("owner of `%s' retained as %s\n", path, 
localname);
+
+       } else {
+               if (verbose && !trustpath)
+                       printf("changing owner of `%s' from %s to %s\n", path, 
getname(sfile.st_uid), localname);
+
+               // everything checks out; do it.  Do it!  Doooo it!
+               if (fchown(file, getuid(), -1))
+                       return error(path);
+       }
 
        // recursion for fun and profit
        if(S_ISDIR(sfile.st_mode)) {
+               if (verbose && !trustpath)
+                       printf("taking files inside `%s'\n", path);
                if(DIR* df = fdopendir(file)) {
                        bool    ok = true;
 

-- 
To view, visit https://gerrit.wikimedia.org/r/70107
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2adfc7e59b9c3d4472322edf83df05f9b7426262
Gerrit-PatchSet: 1
Gerrit-Project: labs/toollabs
Gerrit-Branch: master
Gerrit-Owner: Platonides <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to