netatalk  4.4.0dev
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
directory.c File Reference
#include <assert.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <utime.h>
#include <bstrlib.h>
#include <atalk/adouble.h>
#include <atalk/afp.h>
#include <atalk/cnid.h>
#include <atalk/errchk.h>
#include <atalk/fce_api.h>
#include <atalk/globals.h>
#include <atalk/logger.h>
#include <atalk/netatalk_conf.h>
#include <atalk/unix.h>
#include <atalk/util.h>
#include <atalk/uuid.h>
#include <atalk/vfs.h>
#include "desktop.h"
#include "dircache.h"
#include "directory.h"
#include "file.h"
#include "filedir.h"
#include "fork.h"
#include "hash.h"
#include "mangle.h"
#include "unix.h"
#include "volume.h"

Functions

static int netatalk_mkdir (const struct vol *vol, const char *name)
static int deletedir (const struct vol *vol, int dirfd, char *dir)
static int copydir (struct vol *vol, struct dir *ddir, int dirfd, char *src, char *dst)
static int diroffcnt (struct dir *dir, struct stat *st)
static int invisible_dots (const struct vol *vol, const char *name)
static int set_dir_errors (struct path *path, const char *where, int err)
static int cname_mtouname (const struct vol *vol, struct dir *dir, struct path *ret, int toUTF8)
 Convert name in client encoding to server encoding.
static struct pathpath_from_dir (struct vol *vol, struct dir *dir, struct path *ret)
 Build struct path from struct dir.
int get_afp_errno (const int param)
struct dirdirlookup (const struct vol *vol, cnid_t did)
 Resolve a DID.
struct dirdir_new (const char *m_name, const char *u_name, const struct vol *vol, cnid_t pdid, cnid_t did, bstring path, struct stat *st)
 Construct struct dir.
void dir_free (struct dir *dir)
 Free a struct dir and all its members.
struct dirdir_add (struct vol *vol, const struct dir *dir, struct path *path, int len)
 Create struct dir from struct path.
void dir_free_invalid_q (void)
int dir_remove (const struct vol *vol, struct dir *dir)
 Remove a file/directory from dircache.
struct pathcname (struct vol *vol, struct dir *dir, char **cpath)
 Resolve a catalog node name path.
int movecwd (const struct vol *vol, struct dir *dir)
int check_access (const AFPObj *obj, struct vol *vol, char *path, int mode)
int file_access (const AFPObj *obj, struct vol *vol, struct path *path, int mode)
void setdiroffcnt (struct dir *dir, struct stat *st, uint32_t count)
int dirreenumerate (struct dir *dir, struct stat *st)
int getdirparams (const AFPObj *obj, const struct vol *vol, uint16_t bitmap, struct path *s_path, struct dir *dir, char *buf, size_t *buflen)
int path_error (struct path *path, int error)
int afp_setdirparams (AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
int setdirparams (struct vol *vol, struct path *path, uint16_t d_bitmap, char *buf)
int afp_syncdir (AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
int afp_createdir (AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
int renamedir (struct vol *vol, int dirfd, char *src, char *dst, struct dir *newparent, char *newname)
int deletecurdir (struct vol *vol)
int afp_mapid (AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
int afp_mapname (AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
int afp_closedir (AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
int afp_opendir (AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)

Variables

int afp_errno
struct dir rootParent
struct dircurdir = &rootParent
struct path Cur_Path
q_tinvalid_dircache_entries

Function Documentation

◆ afp_closedir()

int afp_closedir ( AFPObj *obj _U_,
char *ibuf _U_,
size_t ibuflen _U_,
char *rbuf _U_,
size_t * rbuflen )

◆ afp_createdir()

int afp_createdir ( AFPObj * obj,
char * ibuf,
size_t ibuflen _U_,
char * rbuf,
size_t * rbuflen )

◆ afp_mapid()

int afp_mapid ( AFPObj * obj,
char * ibuf,
size_t ibuflen _U_,
char * rbuf,
size_t * rbuflen )

◆ afp_mapname()

int afp_mapname ( AFPObj * obj,
char * ibuf,
size_t ibuflen _U_,
char * rbuf,
size_t * rbuflen )

◆ afp_opendir()

int afp_opendir ( AFPObj *obj _U_,
char * ibuf,
size_t ibuflen _U_,
char * rbuf,
size_t * rbuflen )

◆ afp_setdirparams()

int afp_setdirparams ( AFPObj * obj,
char * ibuf,
size_t ibuflen _U_,
char *rbuf _U_,
size_t * rbuflen )

◆ afp_syncdir()

int afp_syncdir ( AFPObj *obj _U_,
char * ibuf,
size_t ibuflen _U_,
char *rbuf _U_,
size_t * rbuflen )

◆ check_access()

int check_access ( const AFPObj * obj,
struct vol * vol,
char * path,
int mode )

◆ cname()

struct path * cname ( struct vol * vol,
struct dir * dir,
char ** cpath )

Resolve a catalog node name path.

  1. Evaluate path type
  2. Move to start dir, if we can't, it might be e.g. because of EACCES, build path from dirname, so e.g. getdirparams has sth it can chew on. curdir is dir parent then. All this is done in path_from_dir().
  3. Parse next cnode name in path, cases:
  4. single "\0" -> do nothing
  5. two or more consecutive "\0" -> chdir("..") one or more times
  6. cnode name -> copy it to path.m_name
  7. Get unix name from mac name
  8. Special handling of request with did 1
  9. stat the cnode name
  10. If it's not there, it's probably an afp_createfile|dir, return with curdir = dir parent, struct path = dirname
  11. If it's there and it's a file, it must should be the last element of the requested path. Return with curdir = cnode name parent dir, struct path = filename
  12. Treat symlinks like files, don't follow them
  13. If it's a dir:
  14. Search the dircache for it
  15. If it's not in the cache, create a struct dir for it and add it to the cache
  16. chdir into the dir and
  17. set m_name to the mac equivalent of "."
  18. goto 3

◆ cname_mtouname()

int cname_mtouname ( const struct vol * vol,
struct dir * dir,
struct path * ret,
int toUTF8 )
static

Convert name in client encoding to server encoding.

Convert ret->m_name to ret->u_name from client encoding to server encoding. This only gets called from cname().

Returns
0 on success, -1 on error
Note
If the passed ret->m_name is mangled, we'll demangle it

◆ copydir()

int copydir ( struct vol * vol,
struct dir * ddir,
int dirfd,
char * src,
char * dst )
static

◆ deletecurdir()

int deletecurdir ( struct vol * vol)

◆ deletedir()

int deletedir ( const struct vol * vol,
int dirfd,
char * dir )
static

◆ dir_add()

struct dir * dir_add ( struct vol * vol,
const struct dir * dir,
struct path * path,
int len )

Create struct dir from struct path.

Create a new struct dir from struct path. Then add it to the cache.

  1. Open adouble file, get CNID from it.
  2. Search the database, hinting with the CNID from (1).
  3. Build fullpath and create struct dir.
  4. Add it to the cache.
Parameters
vol(r) pointer to struct vol, possibly modified in callee
dir(r) pointer to parrent directory
path(rw) pointer to struct path with valid path->u_name
len(r) strlen of path->u_name
Returns
Pointer to new struct dir or NULL on error.
Note
Function also assigns path->m_name from path->u_name.

◆ dir_free()

void dir_free ( struct dir * dir)

Free a struct dir and all its members.

Parameters
(rw)pointer to struct dir

◆ dir_free_invalid_q()

void dir_free_invalid_q ( void )

Free the queue with invalid struct dirs

This gets called at the end of every AFP func.

◆ dir_new()

struct dir * dir_new ( const char * m_name,
const char * u_name,
const struct vol * vol,
cnid_t pdid,
cnid_t did,
bstring path,
struct stat * st )

Construct struct dir.

Construct struct dir from parameters.

Parameters
m_name(r) directory name in UTF8-dec
u_name(r) directory name in server side encoding
vol(r) pointer to struct vol
pdid(r) Parent CNID
did(r) CNID
path(r) Full unix path to object
st(r) struct stat of object
Returns
pointer to new struct dir or NULL on error
Note
Most of the time mac name and unix name are the same.

◆ dir_remove()

int dir_remove ( const struct vol * vol,
struct dir * dir )

Remove a file/directory from dircache.

  1. Check the dir
  2. Remove it from the cache
  3. Queue it for removal
  4. If it's a request to remove curdir, mark curdir as invalid
  5. Mark it as invalid
Parameters
vol(r) volume pointer
dir(rw) directory/file entry to remove from cache
Returns
0 on success

◆ dirlookup()

struct dir * dirlookup ( const struct vol * vol,
cnid_t did )

Resolve a DID.

Resolve a DID, allocate a struct dir for it

  1. Check for special CNIDs 0 (invalid), 1 and 2. 2a. Check if the DID is in the cache. 2b. Check if it's really a dir because we cache files too.
  2. If it's not in the cache resolve it via the database.
  3. Build complete server-side path to the dir.
  4. Check if it exists and is a directory.
  5. Create the struct dir and populate it.
  6. Add it to the cache.
Parameters
vol(r) pointer to struct vol
did(r) DID to resolve
Returns
pointer to struct dir

◆ diroffcnt()

int diroffcnt ( struct dir * dir,
struct stat * st )
static

◆ dirreenumerate()

int dirreenumerate ( struct dir * dir,
struct stat * st )

◆ file_access()

int file_access ( const AFPObj * obj,
struct vol * vol,
struct path * path,
int mode )

◆ get_afp_errno()

int get_afp_errno ( const int param)

◆ getdirparams()

int getdirparams ( const AFPObj * obj,
const struct vol * vol,
uint16_t bitmap,
struct path * s_path,
struct dir * dir,
char * buf,
size_t * buflen )

◆ invisible_dots()

int invisible_dots ( const struct vol * vol,
const char * name )
static

◆ movecwd()

int movecwd ( const struct vol * vol,
struct dir * dir )

◆ netatalk_mkdir()

int netatalk_mkdir ( const struct vol * vol,
const char * name )
static

◆ path_error()

int path_error ( struct path * path,
int error )

◆ path_from_dir()

struct path * path_from_dir ( struct vol * vol,
struct dir * dir,
struct path * ret )
static

Build struct path from struct dir.

The final movecwd in cname failed, possibly with EPERM or ENOENT. We:

  1. move cwd into parent dir (we're often already there, but not always)
  2. set struct path to the dirname
  3. in case of AFPERR_ACCESS: the dir is there, we just can't chdir into it AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
    1. indicate there's no dir for this path
    2. remove the dir

◆ renamedir()

int renamedir ( struct vol * vol,
int dirfd,
char * src,
char * dst,
struct dir * newparent,
char * newname )

◆ set_dir_errors()

int set_dir_errors ( struct path * path,
const char * where,
int err )
static

◆ setdiroffcnt()

void setdiroffcnt ( struct dir * dir,
struct stat * st,
uint32_t count )

◆ setdirparams()

int setdirparams ( struct vol * vol,
struct path * path,
uint16_t d_bitmap,
char * buf )

Variable Documentation

◆ afp_errno

int afp_errno

◆ Cur_Path

struct path Cur_Path
Initial value:
= {
0,
"",
".",
0,
NULL,
0,
0,
{0}
}

◆ curdir

struct dir* curdir = &rootParent

◆ invalid_dircache_entries

q_t* invalid_dircache_entries

◆ rootParent

struct dir rootParent
Initial value:
= {
NULL, NULL, NULL, NULL,
NULL, 0, 0, 0,
0, 0, 0, 0,
0, 0
}