]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/fork.c
Merge master
[netatalk.git] / etc / afpd / fork.c
index f2c42b4f10e8cb9436808630c97f40fd5a56eb1a..4ba0caf9de0f5e44c66d246f6cf0f7bf3b854e85 100644 (file)
@@ -267,10 +267,8 @@ int afp_openfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, si
         return  AFPERR_BADTYPE;
     }
 
-    LOG(log_debug, logtype_afpd,
-        "afp_openfork(\"%s\", %s)",
-        abspath(s_path->u_name),
-        (fork & OPENFORK_RSCS) ? "OPENFORK_RSCS" : "OPENFORK_DATA");
+    LOG(log_debug, logtype_afpd, "afp_openfork(\"%s\", %s)",
+        fullpathname(s_path->u_name), (fork & OPENFORK_RSCS) ? "RSRC" : "DATA");
 
     /* stat() data fork st is set because it's not a dir */
     switch ( s_path->st_errno ) {
@@ -325,7 +323,8 @@ int afp_openfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, si
     ret = AFPERR_NOOBJ;
     if (access & OPENACC_WR) {
         /* try opening in read-write mode */
-        if (ad_open(ofork->of_ad, upath, adflags, O_RDWR, O_RDWR) < 0) {
+        if (ad_open(ofork->of_ad, upath,
+                    adflags | ADFLAGS_RDWR | ADFLAGS_SETSHRMD) < 0) {
             switch ( errno ) {
             case EROFS:
                 ret = AFPERR_VLOCK;
@@ -335,14 +334,16 @@ int afp_openfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, si
             case ENOENT:
                 if (fork == OPENFORK_DATA) {
                     /* try to open only the data fork */
-                    if (ad_open(ofork->of_ad, upath, ADFLAGS_DF, O_RDWR) < 0) {
+                    if (ad_open(ofork->of_ad, upath,
+                                ADFLAGS_DF | ADFLAGS_RDWR | ADFLAGS_SETSHRMD) < 0) {
                         goto openfork_err;
                     }
                     adflags = ADFLAGS_DF;
                 } else {
                     /* here's the deal. we only try to create the resource
                      * fork if the user wants to open it for write acess. */
-                    if (ad_open(ofork->of_ad, upath, adflags, O_RDWR | O_CREAT, 0666, O_RDWR | O_CREAT, 0666) < 0)
+                    if (ad_open(ofork->of_ad, upath,
+                                adflags | ADFLAGS_RDWR | ADFLAGS_SETSHRMD | ADFLAGS_CREATE, 0666) < 0)
                         goto openfork_err;
                     ofork->of_flags |= AFPFORK_OPEN;
                 }
@@ -370,17 +371,18 @@ int afp_openfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, si
     } else {
         /* try opening in read-only mode */
         ret = AFPERR_NOOBJ;
-        if (ad_open(ofork->of_ad, upath, adflags, O_RDONLY, O_RDONLY) < 0) {
+        /* we need w access for setting deny mode fcntl excl lock */
+        if (ad_open(ofork->of_ad, upath, adflags | ADFLAGS_RDONLY | ADFLAGS_SETSHRMD) < 0) {
             switch ( errno ) {
             case EROFS:
                 ret = AFPERR_VLOCK;
+                goto openfork_err;
             case EACCES:
                 goto openfork_err;
-                break;
             case ENOENT:
                 /* see if client asked for a read only data fork */
                 if (fork == OPENFORK_DATA) {
-                    if (ad_open(ofork->of_ad, upath, ADFLAGS_DF, O_RDONLY) < 0) {
+                    if (ad_open(ofork->of_ad, upath, ADFLAGS_DF | ADFLAGS_RDONLY | ADFLAGS_SETSHRMD) < 0) {
                         goto openfork_err;
                     }
                     adflags = ADFLAGS_DF;
@@ -405,7 +407,7 @@ int afp_openfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, si
                 break;
             default:
                 LOG(log_error, logtype_afpd, "afp_openfork(\"%s\"): %s",
-                    abspath(s_path->m_name), strerror(errno) );
+                    fullpathname(s_path->m_name), strerror(errno) );
                 goto openfork_err;
                 break;
             }
@@ -834,9 +836,8 @@ static int read_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, si
     reqcount = get_off_t(&ibuf, is64);
 
     LOG(log_debug, logtype_afpd,
-         "afp_read(\"%s\", off: %" PRIu64 ", size: %" PRIu64 ", fork: %s)",
-         cfrombstr(ofork->of_ad->ad_fullpath), offset, reqcount,
-         (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
+        "afp_read(off: %" PRIu64 ", size: %" PRIu64 ", fork: %s)", offset, reqcount,
+        (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
 
     if (is64) {
         nlmask = nlchar = 0;
@@ -869,6 +870,9 @@ static int read_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, si
         goto afp_read_err;
     }
 
+    LOG(log_debug, logtype_afpd, "afp_read(name: \"%s\", offset: %jd, reqcount: %jd)",
+        of_name(ofork), (intmax_t)offset, (intmax_t)reqcount);
+
     savereqcount = reqcount;
     saveoff = offset;
     if (ad_tmplock(ofork->of_ad, eid, ADLOCK_RD, saveoff, savereqcount,ofork->of_refnum) < 0) {
@@ -876,11 +880,15 @@ static int read_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, si
         goto afp_read_err;
     }
 
-#define min(a,b)    ((a)<(b)?(a):(b))
-    *rbuflen = min( reqcount, *rbuflen );
+    *rbuflen = MIN(reqcount, *rbuflen);
+    LOG(log_debug, logtype_afpd, "afp_read(name: \"%s\", offset: %jd, reqcount: %jd): reading %jd bytes from file",
+        of_name(ofork), (intmax_t)offset, (intmax_t)reqcount, (intmax_t)*rbuflen);
+
     err = read_file(ofork, eid, offset, nlmask, nlchar, rbuf, rbuflen, xlate);
     if (err < 0)
         goto afp_read_done;
+    LOG(log_debug, logtype_afpd, "afp_read(name: \"%s\", offset: %jd, reqcount: %jd): got %jd bytes from file",
+        of_name(ofork), (intmax_t)offset, (intmax_t)reqcount, (intmax_t)*rbuflen);
 
     /* dsi can stream requests. we can only do this if we're not checking
      * for an end-of-line character. oh well. */
@@ -913,6 +921,7 @@ static int read_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, si
             int fd;
 
             fd = ad_readfile_init(ofork->of_ad, eid, &offset, 0);
+
             if (dsi_stream_read_file(dsi, fd, offset, dsi->datasize) < 0) {
                 if (errno == EINVAL || errno == ENOSYS)
                     goto afp_read_loop;
@@ -1005,9 +1014,7 @@ int afp_flushfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U
         return( AFPERR_PARAM );
     }
 
-    LOG(log_debug, logtype_afpd,
-        "afp_flushfork(\"%s\", fork: %s)",
-        cfrombstr(ofork->of_ad->ad_fullpath),
+    LOG(log_debug, logtype_afpd, "afp_flushfork(fork: %s)",
         (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
 
     if ( flushfork( ofork ) < 0 ) {
@@ -1039,9 +1046,7 @@ int afp_syncfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_
         return( AFPERR_PARAM );
     }
 
-    LOG(log_debug, logtype_afpd,
-        "afp_syncfork(\"%s\", fork: %s)",
-        cfrombstr(ofork->of_ad->ad_fullpath),
+    LOG(log_debug, logtype_afpd, "afp_syncfork(fork: %s)",
         (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
 
     if ( flushfork( ofork ) < 0 ) {
@@ -1108,9 +1113,7 @@ int afp_closefork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U
         return( AFPERR_PARAM );
     }
 
-    LOG(log_debug, logtype_afpd,
-        "afp_closefork(\"%s\", fork: %s)",
-        cfrombstr(ofork->of_ad->ad_fullpath),
+    LOG(log_debug, logtype_afpd, "afp_closefork(fork: %s)",
         (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
 
     if ( of_closefork( ofork ) < 0 ) {
@@ -1167,7 +1170,7 @@ static ssize_t write_file(struct ofork *ofork, int eid,
 static int write_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
 {
     struct ofork    *ofork;
-    off_t               offset, saveoff, reqcount;
+    off_t               offset, saveoff, reqcount, oldsize, newsize;
     int             endflag, eid, xlate = 0, err = AFP_OK;
     uint16_t       ofrefnum;
     ssize_t             cc;
@@ -1188,10 +1191,8 @@ static int write_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, s
         goto afp_write_err;
     }
 
-    LOG(log_debug, logtype_afpd,
-        "afp_write(\"%s\", off: %" PRIu64 ", size: %" PRIu64 ", fork: %s)",
-        cfrombstr(ofork->of_ad->ad_fullpath), offset, reqcount,
-        (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
+    LOG(log_debug, logtype_afpd, "afp_write(off: %" PRIu64 ", size: %" PRIu64 ", fork: %s)",
+        offset, reqcount, (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
 
     if ((ofork->of_flags & AFPFORK_ACCWR) == 0) {
         err = AFPERR_ACCESS;
@@ -1212,8 +1213,9 @@ static int write_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, s
         goto afp_write_err;
     }
 
+    oldsize = ad_size(ofork->of_ad, eid);
     if (endflag)
-        offset += ad_size(ofork->of_ad, eid);
+        offset += oldsize;
 
     /* handle bogus parameters */
     if (reqcount < 0 || offset < 0) {
@@ -1221,6 +1223,8 @@ static int write_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, s
         goto afp_write_err;
     }
 
+    newsize = ((offset + reqcount) > oldsize) ? (offset + reqcount) : oldsize;
+
     /* offset can overflow on 64-bit capable filesystems.
      * report disk full if that's going to happen. */
     if (sum_neg(is64, offset, reqcount)) {
@@ -1307,7 +1311,7 @@ static int write_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, s
     ofork->of_flags |= AFPFORK_MODIFIED;
 
     /* update write count */
-    ofork->of_vol->v_written += reqcount;
+    ofork->of_vol->v_appended += (newsize > oldsize) ? (newsize - oldsize) : 0;
 
     *rbuflen = set_off_t (offset, rbuf, is64);
     return( AFP_OK );