unlink()--Remove Link to File


  Syntax
 #include <unistd.h>

 int unlink(const char *path);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The unlink() function removes a directory entry that refers to a file. This unlink() deletes the link named by path and decrements the link count for the file itself.

If the link count becomes zero and no job currently has the file open, the file itself is deleted. The space occupied by the file is freed for new use, and the current contents of the file are lost. If one or more jobs have the file open when the last link is removed, unlink() removes the link, but the file itself is not removed until the last job closes the file.

unlink() cannot be used to remove a directory; use rmdir() instead.

If path refers to a symbolic link, unlink() removes the symbolic link but not a file or directory named by the contents of the symbolic link.

If unlink() succeeds, the change and modification times for the parent directory are updated. If the link count of the file is not zero, the change time for the file is also updated. If unlink() fails, the link is not removed.

If the file is checked out, unlink() fails with the [EBUSY] error. If the file is marked "read-only", unlink() fails with the [EROOBJ] error.


Parameters

path
(Input) A pointer to the null-terminated path name of the file to be unlinked.

This parameter is assumed to be represented in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.

See QlgUnlink()--Remove Link to File (using NLS-enabled path name) for a description and an example of supplying the path in any CCSID.


Authorities

Note: Adopted authority is not used.

Authorization Required for unlink() (excluding QSYS.LIB, independent ASP QSYS.LIB, QDLS and QOPT)

Object Referred to Authority Required errno
Each directory in the path name preceding the object to be unlinked *X EACCES
Parent directory of the object to be unlinked *WX EACCES
Object to be unlinked *OBJEXIST EACCES
Parent directory of the object to be unlinked has the S_ISVTX mode bit set to binary one (see Note). *ALLOBJ, or owner of the object to be unlinked, or owner of the parent directory of the object to be unlinked EPERM
Note: The S_ISVTX mode bit (which is equivalent to the 'Restricted rename and unlink' object attribute) restriction only applies to objects in the "root" (/), QOpenSys, and user-defined file systems.

Authorization Required for unlink() in the QSYS.LIB and independent ASP QSYS.LIB File Systems

Object Referred to Authority Required errno
Each directory in the path name preceding the object to be unlinked *X EACCES
Parent directory of the object to be unlinked See Note EACCES
Object to be unlinked See Note EACCES
Note: The required authorization varies for each object type. See the DLTxxx commands in the Security reference topic collection for details.

Authorization Required for unlink() in the QDLS File System

Object Referred to Authority Required errno
Each directory in the path name preceding the object to be unlinked *X EACCES
Parent directory of the object to be unlinked *X EACCES
Object to be unlinked *ALL EACCES

Authorization Required for unlink() in the QOPT File System

Object Referred to Authority Required errno
Volume authorization list *CHANGE EACCES
Each directory in the path name preceding the object to be unlinked if volume media format is Universal Disk Format (UDF) *X EACCES
Parent directory of the object to be unlinked if volume media format is Universal Disk Format (UDF) *WX EACCES
Object to be unlinked if volume media format is Universal Disk Format (UDF) *W EACCES
Object to be unlinked and parent directories if volume media format is not Universal Disk Format (UDF) None None


Return Value

0
unlink() was successful.
-1
unlink() was not successful. The errno global variable is set to indicate the error.

Error Conditions

If unlink() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

Error condition Additional information
[EACCES]

If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.

[EAGAIN]  
[EBADFID]  
[EBADNAME]  
[EBUSY]

The file may be checked out.

[ECONVERT]  
[EDAMAGE]  
[EDATALINK]  
[EEXIST]  
[EFAULT]  
[EFILECVT]  
[EINTR]  
[EINVAL]  
[EIO]  
[EJRNDAMAGE]  
[EJRNENTTOOLONG]  
[EJRNINACTIVE]  
[EJRNRCVSPC]  
[ELOOP]  
[ENAMETOOLONG]  
[ENEWJRN]  
[ENEWJRNRCV]  
[ENOENT]  
[ENOMEM]  
[ENOSPC]  
[ENOTAVAIL]  
[ENOTSAFE]  
[ENOTDIR]  
[ENOTSUP]  
[EPERM]

unlink() is not permitted on directories in this part of the directory hierarchy, or unlink() is permitted but the user does not have sufficient authority.

[EROOBJ]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[EUNKNOWN]  
[EXDEV]  

If interaction with a file server is required to access the object, errno could indicate one of the following errors:

Error condition Additional information
[EADDRNOTAVAIL]  
[ECONNABORTED]  
[ECONNREFUSED]  
[ECONNRESET]  
[EHOSTDOWN]  
[EHOSTUNREACH]  
[ENETDOWN]  
[ENETRESET]  
[ENETUNREACH]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[ETIMEDOUT]  
[EUNATCH]  


Error Messages

The following messages may be sent from this function:

Message ID Error Message Text
CPE3418 E Possible APAR condition or hardware failure.
CPFA0D4 E File system error occurred. Error number &1.
CPF3CF2 E Error(s) occurred during running of &1 API.
CPF9872 E Program or service program &1 in library &2 ended. Reason code &3.


Usage Notes

  1. This function will fail with error code [ENOTSAFE] when all the following conditions are true:
  2. QSYS.LIB and Independent ASP QSYS.LIB File System Differences

    The link to a file cannot be removed when a job has the file open.

    The following object types cannot be unlinked when there are secondary threads active in the job: *CFGL, *CNNL, *COSD, *CTLD, *DEVD, *IPXD, *LIND, *MODD, *NTBD, *NWID, *NWSD. The operation will fail with error code [ENOTSAFE].

  3. QDLS File System Differences

    The link to a document cannot be removed when a job has the document open (returns the [EBUSY] error).

  4. QOPT File System Differences

    The change and modification times of the parent directory are not updated.

    The link to a file cannot be removed when a job has the file open.

  5. The link to a file cannot be removed if the file is a DataLink column in an SQL table and a row in that SQL table references this file.

Related Information


Example

The following example removes a link to a file.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

main() {
  int file_descriptor;
  char fn[]="unlink.file";

  if ((file_descriptor = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    close(file_descriptor);
    if (unlink(fn) != 0)
      perror("unlink() error");
  }
}


API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]