Logo Search packages:      
Sourcecode: samba-doc-ja version File versions

int smbc_open ( const char *  furl,
int  flags,
mode_t  mode 

Open a file on an SMB server.

furl The smb url of the file to be opened.
flags Is one of O_RDONLY, O_WRONLY or O_RDWR which request opening the file read-only,write-only or read/write. flags may also be bitwise-or'd with one or more of the following: O_CREAT - If the file does not exist it will be created. O_EXCL - When used with O_CREAT, if the file already exists it is an error and the open will fail. O_TRUNC - If the file already exists it will be truncated. O_APPEND The file is opened in append mode
mode mode specifies the permissions to use if a new file is created. It is modified by the process's umask in the usual way: the permissions of the created file are (mode & ~umask)
Not currently use, but there for future use. We will map this to SYSTEM, HIDDEN, etc bits that reverses the mapping that smbc_fstat does.

Valid file handle, < 0 on error with errno set:
  • ENOMEM Out of memory
  • EINVAL if an invalid parameter passed, like no file, or smbc_init not called.
  • EEXIST pathname already exists and O_CREAT and O_EXCL were used.
  • EISDIR pathname refers to a directory and the access requested involved writing.
  • EACCES The requested access to the file is not allowed
  • ENODEV The requested share does not exist
  • ENOTDIR A file on the path is not a directory
  • ENOENT A directory component in pathname does not exist.
See also:
This call uses an underlying routine that may create a new connection to the server specified in the URL. If the credentials supplied in the URL, or via the auth_fn in the smbc_init call, fail, this call will try again with an empty username and password. This often gets mapped to the guest account on some machines.

Definition at line 603 of file libsmbclient.c.

References smbc_opendir().

Referenced by smbc_creat(), smbc_open_print_job(), and smbc_print_file().

      fstring server, share, user, password, workgroup;
      pstring path;
      struct smbc_server *srv = NULL;
      int fd;

      if (!smbc_initialized) {

            errno = EINVAL;  /* Best I can think of ... */
            return -1;


      if (!fname) {

            errno = EINVAL;
            return -1;


      smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */

      if (user[0] == (char)0) pstrcpy(user, smbc_user);

      pstrcpy(workgroup, lp_workgroup());

      srv = smbc_server(server, share, workgroup, user, password);

      if (!srv) {

            if (errno == EPERM) errno = EACCES;
            return -1;  /* smbc_server sets errno */

      /* Hmmm, the test for a directory is suspect here ... FIXME */

      if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
            fd = -1;

      else {
            int slot = 0;

            /* Find a free slot first */

            while (smbc_file_table[slot])

            if (slot > SMBC_MAX_FD) {

                  errno = ENOMEM; /* FIXME, is this best? */
                  return -1;


            smbc_file_table[slot] = (struct smbc_file *)malloc(sizeof(struct smbc_file));

            if (!smbc_file_table[slot]) {

                  errno = ENOMEM;
                  return -1;


            if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {

                  /* Handle the error ... */

                  errno = smbc_errno(&srv->cli);
                  return -1;


            /* Fill in file struct */

            smbc_file_table[slot]->cli_fd  = fd;
            smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
            smbc_file_table[slot]->fname   = strdup(fname);
            smbc_file_table[slot]->srv     = srv;
            smbc_file_table[slot]->offset  = 0;
            smbc_file_table[slot]->file    = True;

            return smbc_file_table[slot]->smbc_fd;


      /* Check if opendir needed ... */

      if (fd == -1) {
            int eno = 0;

            eno = smbc_errno(&srv->cli);
            fd = smbc_opendir(fname);
            if (fd < 0) errno = eno;
            return fd;


      return 1;  /* Success, with fd ... */


Generated by  Doxygen 1.6.0   Back to index