/*****************************************************************************\
* Copyright (c) 2002 Pelle Johansson.                                         *
* All rights reserved.                                                        *
*                                                                             *
* This file is part of the moftpd package. Use and distribution of            *
* this software is governed by the terms in the file LICENCE, which           *
* should have come with this package.                                         *
\*****************************************************************************/

/* $moftpd: file.h 1224 2004-10-28 22:42:00Z morth $ */

#ifndef _FILE_H
#define _FILE_H

typedef struct access access_t;

enum
{
  // These are allowed in allow and deny only.
  // They are checked automatically. Putting these in required will fail
  // those tests without errno being set (which is bad, obviously).
  acSearch     = 1 << 0,
  acReadFile   = 1 << 1,
  acListing    = 1 << 2,
  acCreateFile = 1 << 3,
  acCreateDir  = 1 << 4,
  acAppend     = 1 << 5,
  acOverwrite  = 1 << 6,
  acDelete     = 1 << 7,
  acRename     = 1 << 8,
  
  // These are also allowed in require.
  // They must be checked explicitly (interpretation up to program).
  acEncrypted  = 1 << 9,
  acSigned     = 1 << 10
};

enum
{
  tfType       = 1 << 0,
  tfUnique     = 1 << 1,
  tfModify     = 1 << 2,
  tfCreate     = 1 << 4, // N/A
  tfPerm       = 1 << 5,
  tfLang       = 1 << 6, // N/A
  tfSize       = 1 << 7,
  tfMediaType  = 1 << 8, // N/A
  tfCharset    = 1 << 9,
  
  // These are used internally.
  tfRootDir    = 1 << 30,
  tfPartOfList = 1 << 31
};

struct access
{
  char *path;
  int numMasks;
  char **masks;
  
  int deny, allow, require;
  unsigned int hidden:1;
  int hardlink:2;
  
  char *fakeUser, *fakeGroup, *fakeFile, *fakeDir;
  char *dirMsgFile;
  
  struct access *next;
};

const char *set_root(const char *root);

char *full_path(const char *path, const char *cwd, const char *home);
char *chroot_path(const char *path, const char *cwd, const char *home);
const char *strip_path (const char *path);
const char *print_path(const char *path);

char *make_file_list(const char *mask, const char *cwd, const char *home,
      int verbose, const char *prepend);
char *recurse_file_list(char *path, char *mask, int verbose, int dotFiles,
      const char *prepend, int recurse_level);
const char *tagged_file_data (const char *path, const char *name, int tags);
const char *tagged_file_list (const char *path, int tags);

const char *set_cwd(const char *dir, const char *home);
int open_file_reading(const char *path, long long offset);
int open_file_writing(const char *path, long long offset, int flags);
int open_file_appending(const char *path);
int open_temp_file(char *path);
DIR *open_dir_listing(const char *path);
int rename_file(const char *from, const char *to);
int delete_file(const char *path);
int delete_dir(const char *path);
int create_dir(const char *path);

void set_access (access_t *start, int defHardLink, int rFd, int wFd);
int check_access(const char *path, int tests);
int check_hidden(const char *path);
int check_hardlink (const char *path);
const char *dir_msg_file(const char *path);

int open_shared (const char *path);
void close_shared (int fd);

const char *scan_strings (int fd, const char *search);

int match_pattern(const char *pat, const char *str);

void *read_file (const char *file, size_t *sz);

#endif /*_FILE_H*/


syntax highlighted by Code2HTML, v. 0.9.1