/* 
   Netdrive Samba client plugin
   samba library wrappers
   Copyright (C) netlabs.org 2003-2012
   Copyright (C) bww bitwise works GmbH 2012-2016

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
*/

#ifndef _SMBWRP_H
#define _SMBWRP_H

#if !defined (O_RDONLY)
#define O_ACCMODE       0x03    /* mask */
#define O_RDONLY        0x00
#define O_WRONLY        0x01
#define O_RDWR          0x02
#define O_NONBLOCK      0x04
#define O_APPEND        0x08
#define O_TEXT          0x10

#define O_BINARY        0x0100
#define O_CREAT         0x0200
#define O_TRUNC         0x0400
#define O_EXCL          0x0800
#define O_SYNC          0x2000
#define O_NOCTTY        0x4000
#define O_SIZE          0x8000
#endif

#if !defined (DENY_NONE)
/* deny modes */
#define DENY_DOS 0
#define DENY_ALL 1
#define DENY_WRITE 2
#define DENY_READ 3
#define DENY_NONE 4
#define DENY_FCB 7
#endif


/* these define the attribute byte as seen by DOS */
#ifndef aRONLY
#define aRONLY (1L<<0)		/* 0x01 */
#define aHIDDEN (1L<<1)		/* 0x02 */
#define aSYSTEM (1L<<2)		/* 0x04 */
#define aVOLID (1L<<3)		/* 0x08 */
#define aDIR (1L<<4)		/* 0x10 */
#define aARCH (1L<<5)		/* 0x20 */
#endif

#pragma pack(1)

#ifdef CLI_BUFFER_SIZE
typedef	struct cli_state cli_state;
#else
typedef	unsigned long cli_state;
#endif

#define CAP_NOPATHINFO2 0x01000000

typedef struct smbwrp_server 
{
        char server_name[256];
        char share_name[256];
        char workgroup[256];
        char username[256];
        char password[256];
        char master[256];
        int ifmastergroup;
        int no_pathinfo2;
} smbwrp_server;

typedef struct smbwrp_file 
{
        int fd;
        unsigned long long offset;
        int openmode;
        int openattr;
        int denymode;
        unsigned long mtime;
        unsigned long ctime;
        int updatetime; 
        // 0 = time is not updated upon file close
        // 1 = modified time is updated to current time
        // 2 = create and modified time are updated according local file
        char fullname[261];
        char fname[261];
} smbwrp_file;

typedef struct smbwrp_fileinfo
{
        unsigned long long size;
        unsigned long attr;
        unsigned long ctime;
        unsigned long mtime;
        unsigned long atime;
        int easize;
        char fname[261];
} smbwrp_fileinfo;

typedef struct smbwrp_fileseek
{
        int whence;
        long offset;
        unsigned long result;
} smbwrp_fileseek;


#ifndef INCL_DOS
typedef struct _FSALLOCATE      /* fsalloc */
{
        unsigned long  idFileSystem;
        unsigned long  cSectorUnit;
        unsigned long  cUnit;
        unsigned long  cUnitAvail;
        unsigned short cbSector;
} FSALLOCATE;
#endif

typedef struct _Resource
{
        int rootlevel;
        smbwrp_server srv;
        int easupport;
        int krb5support;
        int cachetimeout;
        int cachedepth;
        int loglevel;
        char logfile[_MAX_PATH +1];
        FILE *logfileFH;
        char smb_logfile[_MAX_PATH +1];
        int ifL;
        int firstLogLine;
        int smb_initialised;
        struct DirectoryCache *pdc;
} Resource;

typedef struct _Connection
{
        Resource *pRes;
        cli_state* cli;
        smbwrp_file file;
} Connection;


typedef struct filelist_state
{
        unsigned long pipe;
        char * data;
        int datalen;
        int bufferlen;
        void ( * _System add_dir_entry)(void * st);
        char mask[ _MAX_PATH];
        char dir[ _MAX_PATH];
        char dir_mask[ _MAX_PATH];
        smbwrp_fileinfo finfo;
        Connection* pConn;
        void *plist;
        unsigned long ulAttribute;
        const char *fullpath;
} filelist_state;

#pragma pack()

int _System smbwrp_getclisize(void);
int _System smbwrp_init(Resource *pRes);
int _System smbwrp_connect(Resource * pRes, cli_state **);
void _System smbwrp_disconnect(Resource * pRes, cli_state *);
int _System smbwrp_open(Resource *pRes, cli_state * cli, smbwrp_file * file);
int _System smbwrp_read(Resource *pRes, cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result);
int _System smbwrp_write(Resource *pRes, cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result);
int _System smbwrp_lseek(Resource *pRes, cli_state * cli, smbwrp_file * file, int whence, long long offset);
int _System smbwrp_close(Resource *pRes, cli_state * cli, smbwrp_file * file);
int _System smbwrp_setattr(Resource *pRes, cli_state * cli, smbwrp_fileinfo *finfo);
int _System smbwrp_getattr(Resource *pRes, smbwrp_server *srv, cli_state * cli, smbwrp_fileinfo *finfo);
int _System smbwrp_fgetattr(Resource *pRes, cli_state * cli, smbwrp_file *file, smbwrp_fileinfo *finfo);
int _System smbwrp_filelist(Resource *pRes, smbwrp_server *srv, cli_state * cli, filelist_state * state);
int _System smbwrp_rename(Resource *pRes, cli_state * cli, char *oldname, char *newname);
int _System smbwrp_chdir(Resource *pRes, smbwrp_server *srv, cli_state * cli, char *fname);
int _System smbwrp_mkdir(Resource *pRes, cli_state * cli, char *fname);
int _System smbwrp_rmdir(Resource *pRes, cli_state * cli, char *fname);
int _System smbwrp_unlink(Resource *pRes, cli_state * cli, const char *fname);
int _System smbwrp_setfilesize(Resource *pRes, cli_state * cli, smbwrp_file * file, long long newsize);
int _System smbwrp_setea(Resource *pRes, cli_state * cli, char *fname, char * name, unsigned char * value, int size);
int _System smbwrp_fsetea(Resource *pRes, cli_state * cli, smbwrp_file *file, char * name, unsigned char * value, int size);
int _System smbwrp_listea(Resource *pRes, cli_state * cli, char *fname, void * buffer, unsigned long size);
int _System smbwrp_flistea(Resource *pRes, cli_state * cli, smbwrp_file *file, void * buffer, unsigned long size);
int _System smbwrp_dskattr(Resource *pRes, cli_state * cli, FSALLOCATE *pfsa);

/* Prototype the debug log helper. */
void debuglocal(Resource *pRes, int level, const char * fmt, ...);

void smbwrp_initthread(void);

/* wrappers around fsphAddFile32L */
int addFindInfoL(Resource *pRes, void *plist, smbwrp_fileinfo *finfo,
                 ULONG ulAttribute, char *mask);
void addFindInfoCachedL(void *plist, void* custom);

#endif /* _SMBWRP_H */
