]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/vfs/unix.c
Merge symlink branch
[netatalk.git] / libatalk / vfs / unix.c
index eaec6aab576d184eb0566d50dc4c3ae501338b83..991d04a01a8f6b4c99ced2c7bb8a9cb9ff89e7e6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: unix.c,v 1.2 2009-10-15 12:06:08 franklahm Exp $
+ * $Id: unix.c,v 1.9 2010-02-10 14:05:37 franklahm Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -26,7 +26,7 @@
 
 /* -----------------------------
    a dropbox is a folder where w is set but not r eg:
-   rwx-wx-wx or rwx-wx-- 
+   rwx-wx-wx or rwx-wx--
    rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
 */
 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
@@ -60,8 +60,8 @@ int stickydirmode(const char *name, const mode_t mode, const int dropbox, const
      *  Ignore EPERM errors:  We may be dealing with a directory that is
      *  group writable, in which case chmod will fail.
      */
-    if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM && 
-               !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )  
+    if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
+         !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
     {
         LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
         retval = -1;
@@ -79,33 +79,36 @@ int dir_rx_set(mode_t mode)
 /* --------------------- */
 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
 {
-struct stat sb;
-mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;  /* rwx for owner group and other, by default */
+    struct stat sb;
+    mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;  /* rwx for owner group and other, by default */
 
     if (!st) {
-        if (stat(name, &sb) != 0)
+        if (lstat(name, &sb) != 0)
             return -1;
         st = &sb;
     }
-   
-   mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
-   if ( chmod( name,  mode & ~v_umask ) < 0 && errno != EPERM ) {
-       return -1;
-   }
-   return 0;
+
+    if (S_ISLNK(st->st_mode))
+        return 0; /* we don't want to change link permissions */
+    
+    mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
+
+    if ( chmod( name,  mode & ~v_umask ) < 0 && errno != EPERM ) {
+        return -1;
+    }
+    return 0;
 }
 
 /* -------------------
    system rmdir with afp error code.
-   ENOENT is not an error.
- */
-int netatalk_rmdir(const char *name)
+*/
+int netatalk_rmdir_all_errors(const char *name)
 {
     if (rmdir(name) < 0) {
         switch ( errno ) {
         case ENOENT :
-            break;
-        case ENOTEMPTY : 
+            return AFPERR_NOOBJ;
+        case ENOTEMPTY :
             return AFPERR_DIRNEMPT;
         case EPERM:
         case EACCES :
@@ -119,10 +122,22 @@ int netatalk_rmdir(const char *name)
     return AFP_OK;
 }
 
+/* -------------------
+   system rmdir with afp error code.
+   ENOENT is not an error.
+*/
+int netatalk_rmdir(const char *name)
+{
+    int ret = netatalk_rmdir_all_errors(name);
+    if (ret == AFPERR_NOOBJ)
+        return AFP_OK;
+    return ret;
+}
+
 /* -------------------
    system unlink with afp error code.
    ENOENT is not an error.
- */
+*/
 int netatalk_unlink(const char *name)
 {
     if (unlink(name) < 0) {
@@ -160,7 +175,8 @@ int copy_file(const char *src, const char *dst, mode_t mode)
     int    ret = 0;
     int    sfd = -1;
     int    dfd = -1;
-    size_t cc;
+    ssize_t cc;
+    size_t  buflen;
     char   filebuf[8192];
 
     if ((sfd = open(src, O_RDONLY)) < 0) {
@@ -186,8 +202,9 @@ int copy_file(const char *src, const char *dst, mode_t mode)
             goto exit;
         }
 
-        while (cc > 0) {
-            if ((cc -= write(dfd, filebuf, cc)) < 0) {
+        buflen = cc;
+        while (buflen > 0) {
+            if ((cc = write(dfd, filebuf, buflen)) < 0) {
                 if (errno == EINTR)
                     continue;
                 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
@@ -195,14 +212,67 @@ int copy_file(const char *src, const char *dst, mode_t mode)
                 ret = -1;
                 goto exit;
             }
+            buflen -= cc;
         }
     }
 
 exit:
     if (sfd != -1)
         close(sfd);
-    if (dfd != -1)
-        close(dfd);
+
+    if (dfd != -1) {
+        int err;
+
+        err = close(dfd);
+        if (!ret && err) {
+            /* don't bother to report an error if there's already one */
+            LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
+                src, dst, dst, strerror(errno));
+            ret = -1;
+        }
+    }
 
     return ret;
 }
+
+/* This is equivalent of unix rename(). */
+int unix_rename(const char *oldpath, const char *newpath)
+{
+#if 0
+    char pd_name[PATH_MAX+1];
+    int i;
+    struct stat pd_stat;
+    uid_t uid;
+#endif
+
+    if (rename(oldpath, newpath) < 0)
+        return -1;
+#if 0
+    for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++)
+        pd_name[i] = newpath[i];
+    pd_name[i] = '\0';
+
+    while (i > 0 && pd_name[i] != '/') i--;
+    if (pd_name[i] == '/') i++;
+
+    pd_name[i++] = '.'; pd_name[i++] = '\0';
+
+    if (stat(pd_name, &pd_stat) < 0) {
+        LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s",
+            pd_name, geteuid(), strerror(errno));
+        return 0;
+    }
+
+    /* So we have SGID bit set... */
+    if ((S_ISGID & pd_stat.st_mode) != 0) {
+        uid = geteuid();
+        if (seteuid(0) < 0)
+            LOG(log_error, logtype_afpd, "seteuid() failed: %s", strerror(errno));
+        if (recursive_chown(newpath, uid, pd_stat.st_gid) < 0)
+            LOG(log_error, logtype_afpd, "chown() of parent dir failed: newpath=%s, uid=%d: %s",
+                pd_name, geteuid(), strerror(errno));
+        seteuid(uid);
+    }
+#endif
+    return 0;
+}