Helix Core is now P4. Product user interfaces will be updated soon. Learn more >>

C/C++ API Documentation | 2025.1

# Performing file I/O

The default client file I/O implementation returns a `FileSys` object, which is described in `filesys.h`. To intercept client workspace file I/O, replace the `FileSys` `*ClientUser::File()` method by subclassing `ClientUser`.

The following example illustrates how you can override `FileSys`.

Copy

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    #include "p4/clientapi.h"
    class MyFileSys : public FileSys {
        public:

        MyFileSys();
        ~MyFileSys();

        virtual void    Open( FileOpenMode mode, Error *e );
        virtual void    Write( const char *buf, int len, Error *e );
        virtual int     Read( char *buf, int len, Error *e );
        virtual int     ReadLine( StrBuf *buf, Error *e );
        virtual void    Close( Error *e );
        virtual int     Stat();
        virtual int     StatModTime();
        virtual void    Truncate( Error *e );
        virtual void    Unlink( Error *e = 0 );
        virtual void    Rename( FileSys *target, Error *e );
        virtual void    Chmod( FilePerm perms, Error *e );

        protected:
        int nchars;
    } ;

    MyFileSys::MyFileSys()
    {
        nchars = 0;
    }

    MyFileSys::~MyFileSys()
    {
        printf( "Number of characters transferred = %d\n", nchars );
    }

    void MyFileSys::Open( FileOpenMode mode, Error *e )
    {
        printf( "In MyFileSys::Open()\n" );
    }

    void MyFileSys::Write( const char *buf, int len, Error *e )
    {
        printf( "In MyFileSys::Write()\n" );
        printf( "%s", buf );
        nchars = nchars + len;
    }

    int MyFileSys::Read( char *buf, int len, Error *e )
    {
        printf( "In MyFileSys::Read()\n" );
        return 0;
    }

    int MyFileSys::ReadLine( StrBuf *buf, Error *e )
    {
        printf( "In MyFileSys::ReadLine()\n" );
        return 0;
    }

    void MyFileSys::Close( Error *e )
    {
        printf( "In MyFileSys::Close()\n" );
    }

    int MyFileSys::Stat()
    {
        printf( "In MyFileSys::Stat()\n" );
        return 0;
    }

    int MyFileSys::StatModTime()
    {
        printf( "In MyFileSys::StatModTime()\n" );
        return 0;
    }

    void MyFileSys::Truncate( Error *e )
    {
        printf( "In MyFileSys::Truncate()\n" );
    }

    void MyFileSys::Unlink( Error *e = 0 )
    {
        printf( "In MyFileSys::Unlink()\n" );
    }

    void MyFileSys::Rename( FileSys *target, Error *e )
    {
        printf( "In MyFileSys::Rename()\n" );
    }

    void MyFileSys::Chmod( FilePerm perms, Error *e )
    {
        printf( "In MyFileSys::Chmod()\n" );
    }

Copy

    1
    2
    3
    4
    5
    6
    7
    8
    9
    class ClientUserSubclass : public ClientUser {
        public:
        virtual FileSys *File( FileSysType type );
    };

    FileSys *ClientUserSubclass::File( FileSysType type )
    {
        return new MyFileSys;
    }

Copy

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    int main( int argc, char **argv )
    {
        ClientUserSubclass ui;
        ClientApi client;
        Error e;

        char force[] = "-f";
        char file[] = "hello.c";
        char *args[2] = { &force[0], &file[0] };

        // Connect to server

        client.Init( &e );
        e.Abort();

        // Run the command "sync -f hello.c"

        client.SetArgv( 2, &args[0] );
        client.Run( "sync", &ui );

        // Close connection

        client.Final( &e );
        e.Abort();
        return 0;
    }

The preceding program produces the following output when you run it.

Copy

    % ls -l hello.c
    -r--r--r--    1 member   team           41 Jul 30 16:57 hello.c
    % cat hello.c
    main()
    {
      printf( "Hello World!\n" );
    }
    % samplefilesys
    //depot/main/hello.c#1 - refreshing /work/main/hello.c
    In MyFileSys::Stat()
    In MyFileSys::Open()
    In MyFileSys::Write()
    main()
    {
       printf( "Hello World!\n" );
    }
    In MyFileSys::Close()
    Number of characters transferred = 41

Your search for returned results